public ViewResult Index(Cart cart, string returnUrl)
 {
     return View(new CartIndexViewModel
     {
         Cart = cart,
         ReturnUrl = returnUrl
     });
 }
 public RedirectToRouteResult RemoveFromCart(Cart cart, int productID, string returnUrl)
 {
     log4net.Config.XmlConfigurator.Configure();
     Product product = repository.Products
         .FirstOrDefault(p => p.ProductID == productID);
     if (product != null)
     {
         cart.RemoveLine(product);
     }
     log.Info("Product delete from cart");
     return RedirectToAction("Index", new { returnUrl });
 }
 public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     // Получаем объект Cart из сеанса
     Cart cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
     
     // Создаем объект Cart, если его не обнаружено в данных сеанса
     if (cart == null) {
         cart = new Cart();
         controllerContext.HttpContext.Session[sessionKey] = cart;
     }
     return cart;
 }
 public RedirectToRouteResult AddToCart(Cart cart, int productID, string returnUrl)
 {
     log4net.Config.XmlConfigurator.Configure();
     Product product = repository.Products
         .FirstOrDefault(p => p.ProductID == productID);
     if (product != null)
     {
         cart.AddItem(product, 1);
     }
     log.Info("Product add to cart");
     return RedirectToAction("Index", new { returnUrl });
 }
        public void Calculate_Cart_Total()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };

            Cart target = new Cart();

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

            decimal result = target.ComputeTotalValue();

            Assert.AreEqual(result, 450M);
        }
        public void Can_Add_New_Lines()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            Cart target = new Cart();

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

            CartLine[] results = target.Lines.ToArray();

            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            Cart target = new Cart();

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

            CartLine[] 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);
        }
        public void Can_Remove_Line()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };
            Product p3 = new Product { ProductID = 3, Name = "P3" };

            Cart target = new Cart();

            target.AddItem(p1, 1);
            target.AddItem(p2, 3);
            target.AddItem(p3, 5);
            target.AddItem(p2, 1);

            target.RemoveLine(p2);

            Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0);
            Assert.AreEqual(target.Lines.Count(), 2);

        }
        public ActionResult MakeOrder(Order order, Cart cart, OrderPayment orderPayment, OrderDelivery orderDelivery)
        {   
            if (cart.Lines.Count() != 0)
            {
                order.Payment = orderPayment.Payment;
                order.Delivery = orderDelivery.Delivery;
                order.Cart = JsonConvert.SerializeObject(cart);
                order.TotalPrice = cart.ComputeTotalValue();
                order.UserID = WebSecurity.CurrentUserId;
                {
                    if (ModelState.IsValid)
                    {
                        repository.AddOrder(order);
                    }
                }
                cart.Clear();
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }

        }
 public PartialViewResult Summary(Cart cart)
 {
     return PartialView(cart);
 }
示例#11
0
        public void Can_Clear_Contents()
        {
            Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };

            Cart target = new Cart();

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

            target.Clear();

            Assert.AreEqual(target.Lines.Count(), 0);
        }
示例#12
0
        public void Can_View_Cart_Contents()
        {
            Cart cart = new Cart();

            CartController target = new CartController(null);

            CartIndexViewModel result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;

            Assert.AreEqual(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
示例#13
0
        public void Adding_Product_To_Cart_Goes_To_Cart_Screen()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {ProductID = 1, Name = "P1", Category = "Apples"},
            }.AsQueryable());

            Cart cart = new Cart();
            CartController target = new CartController(mock.Object);

            RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");

            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
        }
示例#14
0
        public void Can_Add_To_Cart()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {ProductID = 1, Name = "P1", Category = "Apples"},
            }.AsQueryable());

            Cart cart = new Cart();
            CartController target = new CartController(mock.Object);
            target.AddToCart(cart, 1, null);

            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);
        }