Exemplo n.º 1
0
        public ActionResult EditProduct(int id)
        {
            var model = new AddProductModelView()
            {
                Product  = _productService.GetProduct(id),
                Category = _productService.GetCategories()
            };

            return(View(model));
        }
        /// <summary>
        /// Adds a new instance of Product.
        /// </summary>
        /// <param name="addProductMV">AddProductModelView with the product information</param>
        /// <returns>GetProductModelView with the created product, null if the product was not created</returns>
        public GetProductModelView addProduct(AddProductModelView addProductMV)
        {
            Product newProduct = CreateProductService.create(addProductMV);

            newProduct = PersistenceContext.repositories().createProductRepository().save(newProduct);
            //an entity will be null after save if an equal entity was found in the repository
            if (newProduct == null)
            {
                throw new ArgumentException(ERROR_UNABLE_TO_SAVE_PRODUCT);
            }

            return(ProductModelViewService.fromEntity(newProduct));
        }
        public ActionResult addProduct([FromBody] AddProductModelView addProductMV)
        {
            if (addProductMV == null)
            {
                return(BadRequest(new SimpleJSONMessageService(INVALID_PRODUCT_DATA)));
            }

            try {
                GetProductModelView createdProductMV = new core.application.ProductController().addProduct(addProductMV);
                return(CreatedAtRoute("GetProduct", new { id = createdProductMV.productId }, createdProductMV));
            } catch (ArgumentException e) {
                return(BadRequest(new SimpleJSONMessageService(e.Message)));
            } catch (Exception) {
                return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
            }
        }
Exemplo n.º 4
0
 public ActionResult EditProduct(AddProductModelView newProduct)
 {
     return(RedirectToAction("Index"));
 }
Exemplo n.º 5
0
        //TODO: use ProductBuilder here

        /// <summary>
        /// Creates a new instance of Product and saves it to the Repository.
        /// </summary>
        /// <param name="addProductMV">AddProductModelView containing the new Product's information.</param>
        /// <returns>Created instance of Product.</returns>
        /// <exception cref="System.ArgumentException">Throw </exception>
        public static Product create(AddProductModelView addProductMV)
        {
            string reference         = addProductMV.reference;
            string designation       = addProductMV.designation;
            string modelFilename     = addProductMV.modelFilename;
            long   productCategoryId = addProductMV.categoryId;

            List <AddMeasurementModelView> measurementModelViews = addProductMV.measurements;

            //NOTE: these checks are made here in order to avoid making requests to repositories unnecessarily
            if (measurementModelViews == null || !measurementModelViews.Any())
            {
                throw new ArgumentException(ERROR_NO_MEASUREMENTS_DEFINED);
            }

            List <AddProductMaterialModelView> materialViews = addProductMV.materials;

            //NOTE: these checks are made here in order to avoid making requests to repositories unnecessarily
            if (materialViews == null || !materialViews.Any())
            {
                throw new ArgumentException(ERROR_NO_MATERIALS_DEFINED);
            }

            ProductCategoryRepository categoryRepository = PersistenceContext.repositories().createProductCategoryRepository();

            ProductCategory category = categoryRepository.find(productCategoryId);

            if (category == null)
            {
                throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND);
            }

            if (!categoryRepository.isLeaf(category))
            {
                throw new ArgumentException(ERROR_CATEGORY_NOT_LEAF);
            }

            List <Material>    materials          = new List <Material>();
            MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository();

            foreach (AddProductMaterialModelView materialModelView in materialViews)
            {
                if (materialModelView == null)
                {
                    throw new ArgumentException(ERROR_NULL_MATERIAL);
                }

                long materialId = materialModelView.materialId;

                Material material = materialRepository.find(materialId);
                if (material == null)
                {
                    throw new ArgumentException(string.Format(ERROR_MATERIAL_NOT_FOUND, materialId));
                }
                materials.Add(material);
            }

            IEnumerable <Measurement> measurements = MeasurementModelViewService.fromModelViews(addProductMV.measurements);

            List <AddComponentModelView>  componentModelViews = addProductMV.components;
            AddProductSlotWidthsModelView slotWidthsModelView = addProductMV.slotWidths;

            bool hasComponents = componentModelViews != null && componentModelViews.Any();
            bool hasSlots      = slotWidthsModelView != null;

            Product product = null;

            if (hasSlots)
            {
                ProductSlotWidths slotWidths = ProductSlotWidthsModelViewService.fromModelView(slotWidthsModelView);
                if (hasComponents)
                {
                    product = new Product(reference, designation, modelFilename, category, materials, measurements, slotWidths);
                    return(addComplementaryProducts(product, componentModelViews));
                }
                else
                {
                    return(new Product(reference, designation, modelFilename, category, materials, measurements, slotWidths));
                }
            }
            else
            {
                if (hasComponents)
                {
                    product = new Product(reference, designation, modelFilename, category, materials, measurements);
                    return(addComplementaryProducts(product, componentModelViews));
                }
                else
                {
                    return(new Product(reference, designation, modelFilename, category, materials, measurements));
                }
            }
        }