Example #1
0
 public ActionResult Index(Cart cart,string returnUrl)
 {
     CartIndexViewModel result = new CartIndexViewModel();
     result.cart = cart;
     result.returnUrl = returnUrl;
     return View(result);
 }
Example #2
0
 public RedirectToRouteResult AddToCart(Cart cart, int ProductId, string returnUrl)
 {
     Product product = repository.Products.Where(m => m.ProductID == ProductId).FirstOrDefault();
     if (product != null) {
         cart.AddItem(product, 1);
     }
     return RedirectToAction("Index", new { returnUrl });
 }
        public void ProcessOrder(Cart cart, ShippingDetails shippingDetails)
        {
            using (SmtpClient smptClient = new SmtpClient())
            {
                smptClient.EnableSsl = emailSettings.UseSSL;
                smptClient.Host = emailSettings.ServerName;
                smptClient.Port = emailSettings.ServerPort;
                smptClient.UseDefaultCredentials = false;

                smptClient.Credentials = new NetworkCredential(emailSettings.UserName, emailSettings.Password);

                if (emailSettings.WriteAsFile)
                {
                    smptClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smptClient.PickupDirectoryLocation = emailSettings.FileLocation;
                    smptClient.EnableSsl = false;
                }
                StringBuilder body = new StringBuilder();

                body
                    .AppendLine("Размещен новый заказ")
                    .AppendLine("---")
                    .AppendLine("Товары:");
                foreach (CartLine line in cart.Lines)
                {
                    body.AppendFormat("{0} x{1} (Subtotal {2})",line.Product.Name,line.Quantity,line.Product.Price*line.Quantity);
                }

                body.AppendFormat("Итого: {0}", cart.ComputeTotalValue())
                    .AppendLine("---")
                    .AppendLine("Доставка")
                    .AppendLine(shippingDetails.Name)
                    .AppendLine(shippingDetails.Line1)
                    .AppendLine(shippingDetails.Line2 ?? "")
                    .AppendLine(shippingDetails.Line3 ?? "")
                    .AppendLine(shippingDetails.State ?? "")
                    .AppendLine(shippingDetails.City)
                    .AppendLine(shippingDetails.Zip)
                    .AppendLine(shippingDetails.Country)
                    .AppendLine(shippingDetails.Line1)
                    .AppendLine("---")
                    .AppendFormat("GiftWrap: {0}", shippingDetails.GiftWrap ? "Yes" : "No");

                MailMessage mailMessage = new MailMessage(
                    emailSettings.MailFromAdress,
                    emailSettings.MailToAdress,
                    "New Order",
                    body.ToString()
                    );
                if (emailSettings.WriteAsFile)
                {
                    mailMessage.BodyEncoding = Encoding.GetEncoding("iso-8859-1");
                }

                smptClient.Send(mailMessage);

            }
        }
Example #4
0
        public void Adding_Products_To_Cart_Goes_To_Cart_Screen()
        {
            var productsArray = products.Products.ToArray();
            Cart cart = new Cart();
            CartController target = new CartController(products, orderProcessor);

            RedirectToRouteResult result = target.AddToCart(cart, 1, "testUrl");
            Assert.AreEqual(result.RouteValues["returnUrl"], "testUrl");
            Assert.AreEqual(result.RouteValues["action"], "Index");
        }
Example #5
0
        public void Can_Add_To_Cart()
        {
            var productsArray = products.Products.ToArray();
            Cart cart = new Cart();
            CartController target = new CartController(products, orderProcessor);

            target.AddToCart(cart, 1, "testUrl");

            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);
        }
Example #6
0
        public void Can_Remove_From_Cart()
        {
            var productsArray = products.Products.ToArray();
            Cart cart = new Cart();
            cart.AddItem(productsArray[0], 1);
            cart.AddItem(productsArray[1], 1);
            cart.AddItem(productsArray[2], 1);
            CartController target = new CartController(products, orderProcessor);

            RedirectToRouteResult result = (RedirectToRouteResult)target.RemoveFromCart(cart, productsArray[1].ProductID, "testUrl");
            Assert.AreEqual(result.RouteValues["returnUrl"], "testUrl");
            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(cart.Lines.ToArray()[0].Product, productsArray[0]);
            Assert.AreEqual(cart.Lines.ToArray()[1].Product, productsArray[2]);
        }
Example #7
0
 public ViewResult CheckOut(Cart cart, ShippingDetails shippingDetails)
 {
     if (cart.Lines.Count() == 0)
     {
         ModelState.AddModelError("", "Корзина пуста");
     }
     if (ModelState.IsValid)
     {
         orderProcessor.ProcessOrder(cart, shippingDetails);
         cart.Clear();
         return View("Completed");
     }
     else
     {
         return View(shippingDetails);
     }
 }
Example #8
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            Cart cart = null;
            if (controllerContext.HttpContext.Session != null)
            {
                cart =(Cart) controllerContext.HttpContext.Session[sessionKey];
            }

            if (cart == null)
            {
                cart = new Cart();
                if (controllerContext.HttpContext.Session != null)
                {
                    controllerContext.HttpContext.Session[sessionKey] = cart;
                }

            }
            return cart;
        }
Example #9
0
 public PartialViewResult Summary(Cart cart)
 {
     return PartialView(cart);
 }
Example #10
0
        public void Can_View_Cart_Contents()
        {
            var productsArray = products.Products.ToArray();
            Cart cart = new Cart();
            CartController target = new CartController(products, orderProcessor);

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

            Assert.AreEqual(result.returnUrl, "testUrl");
            Assert.AreEqual(result.cart, cart);
        }
Example #11
0
        public void Cart_Can_Remove_Lines_And_Clear()
        {
            var productsArray = products.Products.ToArray();
            Cart target = new Cart();
            target.AddItem(productsArray[0], 1);
            target.AddItem(productsArray[1], 2);
            target.AddItem(productsArray[2], 2);
            target.RemoveLine(productsArray[0]);

            var result = target.Lines.ToArray();
            Assert.AreEqual(result.Count(), 2);
            Assert.AreEqual(result[0].Product, productsArray[1]);
            Assert.AreEqual(result[1].Product, productsArray[2]);

            target.Clear();
            result = target.Lines.ToArray();
            Assert.AreEqual(result.Count(), 0);
        }
Example #12
0
        public void Cart_Can_Compute_Sum()
        {
            var pruductsArray = products.Products.ToArray();
            Cart target = new Cart();
            target.AddItem(pruductsArray[0], 1);
            target.AddItem(pruductsArray[1], 2);
            var resultValue = target.ComputeTotalValue();
            var resultCount = target.ComputeTotalCount();

            var checkresult = pruductsArray[0].Price + pruductsArray[1].Price * 2;
            Assert.AreEqual(resultValue, checkresult);
            Assert.AreEqual(resultCount, 3);
        }
Example #13
0
        public void Cart_Can_Add_New_Lines()
        {
            var productsArray = products.Products.ToArray();

            Cart target = new Cart();
            target.AddItem(productsArray[0], 1);
            target.AddItem(productsArray[1], 2);

            var result=target.Lines.ToArray();
            Assert.AreEqual(result.Count(), 2);
            Assert.AreEqual(result[0].Product, productsArray[0]);
            Assert.AreEqual(result[1].Product, productsArray[1]);
        }
Example #14
0
        public void Cart_Can_Add_Exisiting_Lines()
        {
            var productsArray = products.Products.ToArray();

            Cart target = new Cart();
            target.AddItem(productsArray[0], 1);
            target.AddItem(productsArray[1], 2);
            target.AddItem(productsArray[1], 3);

            var result = target.Lines.ToArray();
            Assert.AreEqual(result.Count(), 2);
            Assert.AreEqual(result[0].Quantity, 1);
            Assert.AreEqual(result[1].Quantity, 5);
        }