public async Task <IActionResult> Create() { var categories = await this.categoryService.GetCategories(); var model = new SubCategoryBindingModel { Categories = categories.ToList() }; return(this.View(model)); }
public async Task <bool> AddSubCategory(SubCategoryBindingModel model) { var categoryExists = await this.CategoryExists(model.CategoryId); if (!categoryExists) { return(false); } var subCategory = AutoMapper.Mapper.Map <SubCategory>(model); await this.context.SubCategories.AddAsync(subCategory); var result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> Create(SubCategoryBindingModel model) { var categories = await this.categoryService.GetCategories(); model.Categories = categories.ToList(); if (!this.ModelState.IsValid) { return(this.View(model)); } await this.subCategoryService.AddSubCategory(model); this.TempData["Success"] = $"Successully created {model.Name} sub-category."; return(this.RedirectToAction("Create")); }
public async Task AddSubCategory_WithNonExistingMainCategory_ShouldReturnFalse() { var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var subCategoryService = new SubCategoryService(context); string categoryId = "fakeCategoryId"; SubCategoryBindingModel model = new SubCategoryBindingModel { CategoryId = categoryId, Name = "NewSubCategory", }; var methodResult = await subCategoryService.AddSubCategory(model); Assert.False(methodResult, "The method did not return false upon wrong data input for creation."); }
public async Task AddSubCategory_WithCorrectData_ShouldCreateSubCategorySuccessfully() { var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var subCategoryService = new SubCategoryService(context); // Seed only one main category var categoryId = await this.SeedCategory(context); SubCategoryBindingModel model = new SubCategoryBindingModel { CategoryId = categoryId, Name = "NewSubCategory", }; var methodResult = await subCategoryService.AddSubCategory(model); Assert.True(methodResult, "The method returned false on correct input for creation."); var subCategoryFromDatabase = await context.SubCategories.FirstOrDefaultAsync(c => c.Name == model.Name); AssertExtensions.NotNullWithMessage(subCategoryFromDatabase, "The subCategory was not found in the database."); }