Пример #1
0
 public AddProductCommand(string cartId, Product product, int quantity)
 {
     _cartId = cartId;
     _quantity = quantity;
     Product = product;
     Id = Guid.NewGuid();
 }
 public void DeleteProduct(Product prod)
 {
     if (prod != null)
     {
         context.Products.Remove(prod);
         context.SaveChanges();
     }
 }
Пример #3
0
 public void EditProduct(Product p)
 {
     if (p == null)
     {
         throw new ArgumentException("product");
     }
     db.Entry(p).State = EntityState.Modified;
     db.SaveChanges();
 }
Пример #4
0
 public Product AddProduct(Product p)
 {
     if (p == null)
     {
         throw new ArgumentException("product");
     }
     db.Products.Add(p);
     db.SaveChanges();
     return p;
 }
Пример #5
0
 public ActionResult Edit(Product product)
 {
     if (ModelState.IsValid)
     {
         repository.SaveProduct(product);
         TempData["message"] = string.Format("{0} has been saved", product.Name);
         return RedirectToAction("Index");
     }
     return View(product);
 }
Пример #6
0
 public void AddCart(Product product, int count)
 {
     Cart cart=cartColection.Where(x => x.Product.ProductId == product.ProductId).FirstOrDefault();
        if (cart == null)
        {
        cartColection.Add(new Cart { Product = product, Count = count });
        }
        else
        {
        cart.Count += count;
        }
 }
Пример #7
0
        public void Cannot_Save_Invalid_Changes()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            var target = new AdminController(mock.Object);
            var product = new Product { Name = "Test" };
            target.ModelState.AddModelError("error","error");

            var result = target.Edit(product, null);

            mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never);
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Пример #8
0
 public void AddItem(Product product, int quantity)
 {
     var line = _lineCollection.FirstOrDefault(p => p.Product.ProductID == product.ProductID);
     if (line == null)
     {
         _lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
     }
     else
     {
         line.Quantity += quantity;
     }
 }
Пример #9
0
 public ActionResult Edit(Product product)
 {
     if (ModelState.IsValid)
     {
         repository.SaveProduct(product);
         TempData["message"] = string.Format("{0} has been saved", product.Name);
         return RedirectToAction("Index");
     }
     else
     { // there is something wrong with the data values
         return View(product);
     }
 }
Пример #10
0
        public void SaveProduct(Product product)
        {
            if(product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                context.Entry(product).State = System.Data.EntityState.Modified;
            }

            int result = context.SaveChanges();
        }
Пример #11
0
        public void AddItem(Product product,int quantity)
        {
            CartLine line = lineCollection.FirstOrDefault(x => x.Product.ProductID == product.ProductID);

            if (line == null)
            {
                lineCollection.Add(new CartLine() {Product = product,Quantity = quantity});
            }
            else
            {
                line.Quantity +=quantity;
            }
        }
Пример #12
0
 public void AddProduct(Product product, int quantity)
 {
     CartItem cartItem = repository.CartItems.FirstOrDefault(x => x.CustomerId == customerId && x.ProductId == product.ProductID);// cartItem = _cart.FirstOrDefault(x => x.Product.ProductID == product.ProductID);
     if (cartItem != null)
     {
         cartItem.Quantity += quantity;
     }
     else
     {
         cartItem = new CartItem { Quantity = quantity, CustomerId = customerId , ProductId = product.ProductID};
     }
     repository.Save(cartItem);
 }
Пример #13
0
        public void Canlcalate_Cart_Total()
        {
            var p1 = new Product { ProductID = 1, Name = "p1", Price = 100M };
            var p2 = new Product { ProductID = 2, Name = "p2", Price = 50M };

            var target = new Cart();
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);
            var result = target.ComputeTotalValue();

            Assert.AreEqual(result, 450M);
        }
Пример #14
0
        public void Can_Save_Valid_Changes()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            AdminController target = new AdminController(mock.Object);

            var product = new Product {Name = "Test"};

            ActionResult result = target.Edit(product);

            // Assert - check that the repository was called
            mock.Verify(m => m.SaveProduct(product));

            // Assert - check the method result type
            Assert.IsNotInstanceOfType(typeof(ViewResult), result);
        }
Пример #15
0
        public void Can_Delete_Valid_Products()
        {
            var prod = new Product {ProductID = 2, Name = "Test"};
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
            {
                new Product{ProductID = 1, Name = "p1"},
                prod,
                new Product{ProductID = 3, Name = "p3"}
            }.AsQueryable());

            var target = new AdminController(mock.Object);
            target.Delete(prod.ProductID);
            mock.Verify(m => m.DeleteProduct(prod.ProductID));
        }
Пример #16
0
 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;
     }
 }
Пример #17
0
 public ActionResult Edit(Product product, HttpPostedFileBase image)
 {
     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 saved", product.Name);
         return RedirectToAction("Index");
     }
     return View(product);
 }
Пример #18
0
 public void DeleteProduct(Product product)
 {
     //Удаляем связанные с Product Pictures
     if (product.Pictures.Count != 0)
     {
         //Удаляем картинки
         var delpic = from d in product.Pictures
                      select d;
         for (var i = 0; i < product.Pictures.Count; i++)
         {
             var currentpic = delpic.First();
             _context.Pictures.Remove(currentpic);//DeletePicture(currentpic);
         }
     }
     _context.Products.Remove(product);
     _context.SaveChanges();
 }
Пример #19
0
        public void Can_Add_New_Lines()
        {
            var p1 = new Product{ ProductID = 1, Name = "P1"};
            var p2 = new Product {ProductID = 2, Name = "P2"};

            var target = new Cart();

            // Действие
            target.AddItem(p1,1);
            target.AddItem(p2,1);

            var results = target.Lines.ToArray();

            Assert.AreEqual(results.Length,2);
            Assert.AreEqual(results[0].Product,p1);
            Assert.AreEqual(results[1].Product, p2);
        }
Пример #20
0
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            var p1 = new Product { ProductID = 1, Name = "P1" };
            var p2 = new Product { ProductID = 2, Name = "P2" };

            var target = new Cart();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1,10);

            var results = target.Lines.OrderBy(c => c.Product.ProductID).ToArray();

            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, 11);
            Assert.AreEqual(results[1].Quantity, 1);
        }
Пример #21
0
 public void RemoveLine(Product product)
 {
     foreach (Cart g in cartColection)
     {
         if (g.Product.ProductId == product.ProductId)
         {
             if (g.Count != 1)
             {
                 g.Count--;
             }
             else
             {
                 cartColection.Remove(g);
                 break;
             }
         }
     }
 }
 public void SaveProduct(Product prod)
 {
     if (prod.ProductId == 0)
     {
         context.Products.Add(prod);
         context.SaveChanges();
     }
     else
     {
         Product product = context.Products.FirstOrDefault(x => x.ProductId == prod.ProductId);
         product.CategoryId = prod.CategoryId;
         product.Description = prod.Description;
         product.Name = prod.Name;
         product.Price = prod.Price;
         product.ImageMimeType = prod.ImageMimeType;
         product.ImageData = prod.ImageData;
         context.SaveChanges();
     }
 }
Пример #23
0
 public void SaveProduct(Product product)
 {
     if (product.ProductId == 0)
     {
         _context.Products.Add(product);
     }
     else //Извлекаем и модифицируем
     {
         var prod = _context.Products.Single(x => x.ProductId == product.ProductId);
         prod.Name = product.Name;
         prod.AmazonId = product.AmazonId;
         prod.Description = product.Description;
         prod.Thumbnail = product.Thumbnail;
         prod.Width = product.Width;
         prod.Height = product.Height;
         prod.Depth = product.Depth;
     }
     _context.SaveChanges();
 }
Пример #24
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;
                }
            }

            context.SaveChanges();
        }
Пример #25
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} został zapisany.",
         product.Name);
         return RedirectToAction("Index");
     }
     else
     {
         // there is something wrong with the data values
         return View(product);
     }
 }
Пример #26
0
        public void Can_Retrieve_Image_Data()
        {
            var product = new Product
            {
                ProductID = 2,
                Name = "Test",
                ImageData = new byte[] {},
                ImageMimeType = "image/png"
            };

            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
            {
                new Product {ProductID = 1, Name = "p1"},
                product,
                new Product {ProductID = 3, Name = "p3"}
            }.AsQueryable());
            var target = new ProductController(mock.Object);
            var result = target.GetImage(2);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(FileResult));
            Assert.AreEqual(product.ImageMimeType, result.ContentType);
        }
Пример #27
0
        public void AddToCart(Product product, int quantity)
        {
            Cart cartItem = _storetUnitOfWork.Carts.Find(
                c => c.CartId == ShoppingCartId && c.ProductId == product.Id).FirstOrDefault();

            if (cartItem == null)
            {
                cartItem = new Cart
                {
                    ProductId = product.Id,
                    CartId = ShoppingCartId,
                    Count = quantity,
                    DateCreated = DateTime.Now
                };

                _storetUnitOfWork.Carts.Add(cartItem);
            }
            else
            {
                cartItem.Count += quantity;
            }

            _storetUnitOfWork.Commit();
        }
Пример #28
0
 public ProductRemovedEvent(Product product, Guid? id = null)
 {
     Product = product;
     Id = id ?? Guid.NewGuid();
 }
Пример #29
0
 public void RemoveLine(Product product)
 {
     _lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
 }
Пример #30
0
        public void Can_Save_Valid_Changes()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            var target = new AdminController(mock.Object);
            var product = new Product {Name = "Test"};

            var result = target.Edit(product, null);

            mock.Verify(m => m.SaveProduct(product));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }