public async Task <IActionResult> Add()
        {
            var subCategoryCreateViewModel = new SubCategoryCreateInputModel()
            {
                Categories = await this.categoryService.GetAllAsync <CategoryDisplayInputModel>(),
            };

            return(this.View(subCategoryCreateViewModel));
        }
Esempio n. 2
0
        public IActionResult Create()
        {
            var categories = this.categoriesService.GetAllCategories <CategoriesDropDownViewModel>();
            var viewModel  = new SubCategoryCreateInputModel()
            {
                CategoriesDropDown = categories,
            };

            return(this.View(viewModel));
        }
Esempio n. 3
0
        public async Task CreateSubCategoryAsync(SubCategoryCreateInputModel inputModel)
        {
            var subCategory = new SubCategory()
            {
                Title      = inputModel.Title,
                CategoryId = inputModel.CategoryId,
            };

            await this.subCategoryRepository.AddAsync(subCategory);

            await this.subCategoryRepository.SaveChangesAsync();
        }
        public async Task <IActionResult> Add(SubCategoryCreateInputModel subCategoryCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.RedirectToAction("Add"));
            }

            if (!await this.subCategoryService.CreateAsync(subCategoryCreateInputModel))
            {
                this.TempData["Error"] = ValidationMessages.SubCategoryUniqieNameErrorMessage;
                return(await this.Add());
            }

            return(this.RedirectToAction("All"));
        }
Esempio n. 5
0
        public async Task CreateCategoryAsync_ReturnsTrueAndCreates_IfNameIsUnique()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var subCategoryRepository = new EfDeletableEntityRepository <SubCategory>(context);
            var subCategoryService    = new SubCategoryService(subCategoryRepository);

            var subCategoryCreateInputModel = new SubCategoryCreateInputModel()
            {
                Name       = "SubCategory1",
                CategoryId = "categoryId1",
            };

            var shouldReturnTrue = await subCategoryService.CreateAsync(subCategoryCreateInputModel);

            Assert.True(shouldReturnTrue);
        }
Esempio n. 6
0
        public async Task CreateAsync_ThrowsException_IfNameIsNullOrWhiteSpace()
        {
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var subCategoryRepository = new EfDeletableEntityRepository <SubCategory>(context);
            var subCategoryService    = new SubCategoryService(subCategoryRepository);


            var subcategoryCreateInputModel = new SubCategoryCreateInputModel()
            {
                Name       = null,
                CategoryId = null,
            };

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await subCategoryService.CreateAsync(subcategoryCreateInputModel);
            });
        }
Esempio n. 7
0
        public async Task CreateAsync_ReturnsFalse_IfNameIsNotUnique()
        {
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var subCategoryRepository = new EfDeletableEntityRepository <SubCategory>(context);
            var subCategoryService    = new SubCategoryService(subCategoryRepository);
            var subCategoryTestSeeder = new SubCategoryTestSeeder();

            await subCategoryTestSeeder.SeedSubCategories(context);

            var subCategoryCreateInputModel = new SubCategoryCreateInputModel()
            {
                Name       = "SubCategory1",
                CategoryId = "categoryId1",
            };

            var shouldReturnfalse = await subCategoryService.CreateAsync(subCategoryCreateInputModel);

            Assert.False(shouldReturnfalse);
        }
Esempio n. 8
0
        public async Task <bool> CreateAsync(SubCategoryCreateInputModel subCategoryCreateInputModel)
        {
            if (string.IsNullOrEmpty(subCategoryCreateInputModel.Name) || string.IsNullOrWhiteSpace(subCategoryCreateInputModel.Name))
            {
                throw new ArgumentNullException("SubCategory name was null or whitespace!");
            }

            if (await this.SubCategoryNameIsNotUnique(subCategoryCreateInputModel.Name))
            {
                return(false);
            }

            var subCategory = subCategoryCreateInputModel.To <SubCategory>();

            await this.subCategoryRepository.AddAsync(subCategory);

            await this.subCategoryRepository.SaveChangesAsync();

            return(true);
        }
Esempio n. 9
0
        public async Task <IActionResult> Create(SubCategoryCreateInputModel inputModel)
        {
            inputModel.CategoriesDropDown = this.categoriesService.GetAllCategories <CategoriesDropDownViewModel>();
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            if (await this.subCategoriesService.SubCategoryExistsByName(inputModel.Title))
            {
                this.ModelState.AddModelError(inputModel.Title, $"Title - {inputModel.Title} already exists");
                return(this.View(inputModel));
            }

            if (!await this.categoriesService.CategoryExistsById(inputModel.CategoryId))
            {
                this.ModelState.AddModelError("Category", "Select appropriate category!");
                return(this.View(inputModel));
            }

            await this.subCategoriesService.CreateSubCategoryAsync(inputModel);

            return(this.RedirectToAction(nameof(this.Active)));
        }