Exemplo n.º 1
0
        public bool AddSubCategory(int departmentId, int categoryId, AddSubCategoryBM categoryBM)
        {
            var category = this.Context
                           .Departments.Find(departmentId)
                           .Categories.FirstOrDefault(c => c.Id == categoryId);

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

            category.SubCategories.Add(new SubCategory {
                Name = categoryBM.Name
            });
            this.Context.SaveChanges();

            return(true);
        }
        public ActionResult Add(int departmentId, int categoryId, AddSubCategoryBM subCategoryBM)
        {
            if (this.service.IsExistSubCategoryWithName(departmentId, categoryId, subCategoryBM.Name))
            {
                this.ModelState.AddModelError(
                    nameof(subCategoryBM.Name),
                    $"The subcategory with name {subCategoryBM.Name} already exist!");
            }

            if (!this.ModelState.IsValid)
            {
                return(this.PartialView("Partials/Add", Mapper.Map <AddSubCategoryVM>(subCategoryBM)));
            }

            if (!this.service.AddSubCategory(departmentId, categoryId, subCategoryBM))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            return(this.RedirectToAction("SubCategoriesList", new { departmentId, categoryId }));
        }