public void AddNewCategory(AddNewCategoryBindingModel model) { if (!Contex.Categories.Any(x => x.Name == model.Name)) { var category = new Category(); category.Name = model.Name; category.Topics = new List <Topic>(); Contex.Categories.Add(category); Contex.SaveChanges(); } }
public async Task <IActionResult> CategoryAddNew(AddNewCategoryBindingModel input) { if (!this.ModelState.IsValid) { return(await this.CategoryAddNew()); } await this.categoryService.AddNewCategoryByName(input); return(this.Redirect("/Admin/CategoryInfo")); }
public async Task AddNewCategoryByNameTests() { //Arrange var mapperConfig = new MapperConfiguration(x => x.AddProfile(new MappingProfile())); var mapper = mapperConfig.CreateMapper(); var categoryService = new CategoryService(this.context, mapper); var categoryModel = new AddNewCategoryBindingModel { Name = "CategoryName" }; //Act Assert.Equal(0, this.context.Categories.Count()); await categoryService.AddNewCategoryByName(categoryModel); //Assert Assert.Equal(1, this.context.Categories.Count()); }