Exemplo n.º 1
0
        public async Task <bool> EditSubCategory(SubCategoryEditInputModel subCategoryEditInputModel)
        {
            var subCategory = this.subCategoryRepository.All().SingleOrDefault(x => x.Id == subCategoryEditInputModel.Id);

            if (subCategory == null)
            {
                throw new ArgumentNullException("SubCategory was null!");
            }


            if (subCategory.Name != subCategoryEditInputModel.Name &&
                await this.SubCategoryNameIsNotUnique(subCategoryEditInputModel.Name))
            {
                return(false);
            }
            else
            {
                subCategory.Name = subCategoryEditInputModel.Name;

                if (subCategory.CategoryId != subCategoryEditInputModel.CategoryId)
                {
                    subCategory.CategoryId = subCategoryEditInputModel.CategoryId;
                }
            }

            await this.subCategoryRepository.SaveChangesAsync();

            return(true);
        }
        public async Task <IActionResult> Edit(SubCategoryEditInputModel subCategoryEditInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(subCategoryEditInputModel));
            }

            if (!await this.subCategoryService.EditSubCategory(subCategoryEditInputModel))
            {
                this.TempData["Error"] = ValidationMessages.SubCategoryUniqieNameErrorMessage;
                return(this.RedirectToAction("Edit", "SubCategories", new { subCategoryId = subCategoryEditInputModel.Id }));
            }

            return(this.RedirectToAction("All"));
        }
Exemplo n.º 3
0
        public async Task EditSubCategory_ReturnsTrue_IfNameIsUnique()
        {
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var subCategoryRepository = new EfDeletableEntityRepository <SubCategory>(context);
            var subCategoryService    = new SubCategoryService(subCategoryRepository);
            var subCategoryTestSeeder = new SubCategoryTestSeeder();

            await subCategoryTestSeeder.SeedSubCategories(context);

            var subCategoryEditInputModel = new SubCategoryEditInputModel()
            {
                Id   = "1",
                Name = "Unique",
            };

            var shouldBetrue = await subCategoryService.EditSubCategory(subCategoryEditInputModel);

            Assert.True(shouldBetrue);
        }
Exemplo n.º 4
0
        public async Task EditSubCategory_ThrowsExceotion_IfMOdelIsIncorect()
        {
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var subCategoryRepository = new EfDeletableEntityRepository <SubCategory>(context);
            var subCategoryService    = new SubCategoryService(subCategoryRepository);
            var subCategoryTestSeeder = new SubCategoryTestSeeder();

            await subCategoryTestSeeder.SeedSubCategories(context);

            var subCategoryEditInputModel = new SubCategoryEditInputModel()
            {
                Id   = "Incorrect",
                Name = "SubCategory2",
            };

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await subCategoryService.EditSubCategory(subCategoryEditInputModel);
            });
        }