Пример #1
0
        public bool EditProduct(EditProductBM productBM)
        {
            var product = this.GetProduct(productBM.Id);

            var manufakcturer = this.Context.Manufacturers.Find(productBM.ManufacturerId);

            if (product == null || manufakcturer == null)
            {
                return(false);
            }

            if (productBM.Image != null && productBM.Image.ContentLength > 0)
            {
                SaveProductImage(product, productBM.Image);
            }


            product.Name         = productBM.Name;
            product.Description  = productBM.Description;
            product.Price        = productBM.Price;
            product.Quantity     = productBM.Quantity;
            product.Manufacturer = manufakcturer;
            this.Context.SaveChanges();

            return(true);
        }
Пример #2
0
        public ActionResult Update(EditProductBM productBM, int id)
        {
            if (this.service.IsExistOtherProductWithName(productBM))
            {
                this.ModelState.AddModelError(
                    nameof(productBM.Name),
                    $"Other product with name {productBM.Name} already exist in subcategory!");
            }

            if (!this.ModelState.IsValid)
            {
                var productVM = Mapper.Map <EditProductVM>(productBM);
                productVM.Manufacturers =
                    this.service.GetManufacturersSelectList(productVM.ManufacturerId);

                return(this.View("Edit", productVM));
            }


            if (!this.service.EditProduct(productBM))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            return(this.RedirectToAction(
                       "ProductsList",
                       new
            {
                productBM.DepartmentId,
                productBM.CategoryId,
                productBM.SubCategoryId
            }));
        }
Пример #3
0
        public bool IsExistOtherProductWithName(EditProductBM productBM)
        {
            var result = this.Context.Departments.Find(productBM.DepartmentId)
                         ?.Categories.FirstOrDefault(c => c.Id == productBM.CategoryId)
                         ?.SubCategories.FirstOrDefault(sc => sc.Id == productBM.SubCategoryId)
                         ?.Products.Any(p => p.Name.Equals(productBM.Name) && p.Id != productBM.Id);

            return(result ?? false);
        }