public void Save(Product product)
 {
     if (product.ProductID == 0)
         context.Products.Add(product);
     else
     {
         context.Entry(product).State = System.Data.EntityState.Modified;
     }
     context.SaveChanges();
 }
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                _repo.Save(product);
                return RedirectToAction("Index");
            }

            return View(product);
        }
        public void ProductController_Edit_CanSaveValidProduct()
        {
            //Arrange
            Product p = new Product { ProductID = 1, Name = "Test" };
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            var target = new ProductController(mock.Object);

            //Act
            var result = target.Edit(p);

            //Assert
            mock.Verify(r => r.Save(p), Times.Once());
        }
        public void ProductController_Edit_CannotSaveInvalidProduct()
        {
            //Arrange
            Product p = new Product { ProductID = 1, Name = "Test" };
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            var target = new ProductController(mock.Object);
            target.ModelState.AddModelError("error", "error");

            //Act
            var result = target.Edit(p);

            //Assert
            mock.Verify(r => r.Save(It.IsAny<Product>()), Times.Never());
        }
 public void Save(Product p)
 {
     if (p.ProductID == 0)
     {
         products.Add(p);
     }
     else
     {
         var existing = products.FirstOrDefault(prod => prod.ProductID == p.ProductID);
         if (null != existing)
             products[products.IndexOf(existing)] = p;
         else
             products.Add(p);
     }
 }
 public void Delete(Product p)
 {
     products.Remove(p);
 }
 public void Delete(Product product)
 {
     context.Products.Remove(product);
     context.SaveChanges();
 }