Esempio n. 1
0
        public ActionResult CreateProduct(Product newProduct, FormCollection formCollection)
        {
            if (_productApplicationService.FindProductByGtinInOwnDatabase(newProduct.GlobalTradeItemNumber, false) != null)
            {
                ModelState.AddModelError("GlobalTradeItemNumber", "Product already exists");
                InitViewData();
                ViewData["Ingredients"] = _ingredientApplicationService.GetAllIngredients();
                return(View(newProduct));
            }
            if (!newProduct.GlobalTradeItemNumber.IsGtin())
            {
                ModelState.AddModelError("GlobalTradeItemNumber", "Not a proper GTIN");
                InitViewData();
                ViewData["Ingredients"] = _ingredientApplicationService.GetAllIngredients();
                return(View(newProduct));
            }

            try
            {
                newProduct = _productApplicationService.CreateProduct(newProduct);

                var certificationMarks = formCollection.GetValues("CertificationMarks") ?? new string[] { };
                _productApplicationService.RemoveAllCertificationMarksFromProduct(newProduct.Id);
                foreach (var certificationMark in certificationMarks)
                {
                    int certificationMarkId;
                    if (int.TryParse(certificationMark, out certificationMarkId))
                    {
                        _productApplicationService.AddCertificationMarkToProduct(newProduct.Id, int.Parse(certificationMark));
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
            }


            InitViewData();
            ViewData["Ingredients"] = _ingredientApplicationService.GetAllIngredients();
            return(View(newProduct));
        }