Example #1
0
        public bool AddProduct(AddProductBM productBM)
        {
            var subCategory = this.Context.Departments.Find(productBM.DepartmentId)
                              ?.Categories.FirstOrDefault(c => c.Id == productBM.CategoryId)
                              ?.SubCategories.FirstOrDefault(sc => sc.Id == productBM.SubCategoryId);
            var manufakcturer = this.Context.Manufacturers.Find(productBM.ManufacturerId);

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

            var product = Mapper.Map <Product>(productBM);

            product.Manufacturer = manufakcturer;
            subCategory.Products.Add(product);
            this.Context.SaveChanges();

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


            return(true);
        }
        public ActionResult Add(AddProductBM productBM)
        {
            if (this.service.IsExistProductWithName(productBM))
            {
                this.ModelState.AddModelError(
                    nameof(productBM.Name),
                    $"The product with name {productBM.Name} already exist in subcategory!");
            }

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

                return(this.View(productVM));
            }

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

            return(this.RedirectToAction(
                       "ProductsList",
                       new
            {
                productBM.DepartmentId,
                productBM.CategoryId,
                productBM.SubCategoryId
            }));
        }
Example #3
0
        public bool IsExistProductWithName(AddProductBM productBM)
        {
            var products = this.Context.Departments.Find(productBM.DepartmentId)
                           ?.Categories.FirstOrDefault(c => c.Id == productBM.CategoryId)
                           ?.SubCategories.FirstOrDefault(sc => sc.Id == productBM.SubCategoryId)
                           ?.Products;

            if (products == null)
            {
                return(false);
            }

            return(products.Any(p => p.Name.Equals(productBM.Name)));
        }