public void SaveCategory(Category category) { if (category.CategoryId == 0) { context.Categories.Add(category); } else { context.Entry(category).State = EntityState.Modified; } context.SaveChanges(); }
public void CanDeleteValidCategories() { // Arrange - create a category Category category = new Category { CategoryId = 2, Name = "Test"}; // Arrange - create a local mock repository var localMock = new Mock<IProductRepository>(); localMock.Setup(m => m.Categories).Returns(new Category[] { new Category {CategoryId = 1, Name = "C1"}, category, new Category {CategoryId = 3, Name = "C3"} }.AsQueryable()); // Arrange - create a controller var controller = new AdminController(localMock.Object); // Action - delete the category controller.DeleteCategory(category.CategoryId); // assert - ensure that the repository DeleteCategory method was called with the correct category localMock.Verify(m => m.DeleteCategory(category)); }
public ActionResult EditCategory(Category category) { if (ModelState.IsValid) { _productRepository.SaveCategory(category); TempData["message"] = string.Format("Category {0} has been saved", category.Name); return RedirectToAction("IndexCategory"); } else { // there is something wrong with the data values return View(category); } }
public void DeleteCategory(Category category) { context.Categories.Remove(category); context.SaveChanges(); }