コード例 #1
0
ファイル: Cart.cs プロジェクト: pumaatlarge/MyStore
 public void AddItem(Product product, int quantity)
 {
     Cartline line = lineCollection.Where(p => p.Product.ProductID == product.ProductID).FirstOrDefault();
     if (line == null)
     {
         lineCollection.Add(new Cartline { Product = product, Quantity = quantity });
     }
     else
     {
         line.Quantity += quantity;
     }
 }
コード例 #2
0
ファイル: CartTests.cs プロジェクト: pumaatlarge/MyStore
 public void Can_Add_New_Lines()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductID = 1, Name = "P1" };
     Product p2 = new Product { ProductID = 2, Name = "P2" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     CartLine[] results = target.Lines.ToArray();
     // Assert
     Assert.AreEqual(results.Length, 2);
     Assert.AreEqual(results[0].Product, p1);
     Assert.AreEqual(results[1].Product, p2);
 }
コード例 #3
0
 public ActionResult Edit(Product product, HttpPostedFileBase image = null)
 {
     if (ModelState.IsValid)
     {
         if (image != null)
         {
             product.ImageMimeType = image.ContentType;
             product.ImageData = new byte[image.ContentLength];
             image.InputStream.Read(product.ImageData, 0, image.ContentLength);
         }
         repository.SaveProduct(product);
         TempData["message"] = string.Format("{0} has been save", product.Name);
         return RedirectToAction("Index");
     }
     else
     {
         return View(product);
     }
 }
コード例 #4
0
 public void SaveProduct(Product product)
 {
     if (product.ProductID == 0)
     {
         context.Products.Add(product);
     }
     else
     {
         Product dbEntry = context.Products.Find(product.ProductID);
         if (dbEntry != null)
         {
             dbEntry.Name = product.Name;
             dbEntry.Description = product.Description;
             dbEntry.Price = product.Price;
             dbEntry.Category = product.Category;
             dbEntry.ImageData = product.ImageData;
             dbEntry.ImageMimeType = product.ImageMimeType;
         }
     }
     context.SaveChanges();
 }
コード例 #5
0
ファイル: Cart.cs プロジェクト: pumaatlarge/MyStore
 public void RemoveLine(Product product)
 {
     lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
 }