Example #1
0
        public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
        {
            Product product = this.repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (product != null)
            {
                cart.RemoveLine(product);
            }

            return this.RedirectToAction("Index", new { returnUrl });
        }
Example #2
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
            if (cart == null)
            {
                cart = new Cart();
                controllerContext.HttpContext.Session[sessionKey] = cart;
            }

            return cart;
        }
        public void ProcessOrder(Cart cart, ShippingDetails shippingInfo)
        {
            using (var smtpClient = new SmtpClient())
            {
                smtpClient.EnableSsl = this.emailSettings.UseSsl;
                smtpClient.Host = this.emailSettings.ServerName;
                smtpClient.Port = this.emailSettings.ServerPort;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(this.emailSettings.Username, this.emailSettings.Password);
                if (this.emailSettings.WriteAsFile)
                {
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtpClient.PickupDirectoryLocation = this.emailSettings.FileLocation;
                    smtpClient.EnableSsl = false;
                }

                StringBuilder body =
                    new StringBuilder().AppendLine("A new order has been submitted").AppendLine("---").AppendLine(
                        "Items:");
                foreach (CartLine line in cart.Lines)
                {
                    decimal subtotal = line.Product.Price * line.Quantity;
                    body.AppendFormat("{0} x {1} (subtotal: {2:c}", line.Quantity, line.Product.Name, subtotal);
                }

                body.AppendFormat("Total order value: {0:c}", cart.ComputeTotalValue()).AppendLine("---").AppendLine(
                    "Ship to:").AppendLine(shippingInfo.Name).AppendLine(shippingInfo.Line1).AppendLine(
                        shippingInfo.Line2 ?? string.Empty).AppendLine(shippingInfo.Line3 ?? string.Empty).AppendLine(
                            shippingInfo.City).AppendLine(shippingInfo.State ?? string.Empty).AppendLine(
                                shippingInfo.Country).AppendLine(shippingInfo.Zip).AppendLine("---").AppendFormat(
                                    "Gift wrap: {0}", shippingInfo.GiftWrap ? "Yes" : "No");
                var mailMessage = new MailMessage(
                    this.emailSettings.MailFromAddress,
                    // From
                    this.emailSettings.MailToAddress,
                    // To
                    "New order submitted!",
                    // Subject
                    body.ToString()); // Body
                if (this.emailSettings.WriteAsFile)
                {
                    mailMessage.BodyEncoding = Encoding.ASCII;
                }

                smtpClient.Send(mailMessage);
            }
        }
Example #4
0
        public void CalculateCartTotal()
        {
            // Arrange - create some test products
            var p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            var p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };

            // Arrange - create a new cart
            var target = new Cart();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);
            decimal result = target.ComputeTotalValue();

            // Assert
            Assert.AreEqual(result, 450M);
        }
Example #5
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (cart.Lines.Count() == 0)
            {
                this.ModelState.AddModelError(string.Empty, "Sorry, your cart is empty!");
            }

            if (this.ModelState.IsValid)
            {
                this.orderProcessor.ProcessOrder(cart, shippingDetails);
                cart.Clear();
                return this.View("Completed");
            }
            else
            {
                return this.View(shippingDetails);
            }
        }
Example #6
0
        public void CanAddNewLines()
        {
            // Arrange - create some test products
            var p1 = new Product { ProductID = 1, Name = "P1" };
            var p2 = new Product { ProductID = 2, Name = "P2" };

            // Arrange - create a new cart
            var 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);
        }
Example #7
0
        public void CanAddToCart()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[] { new Product { ProductID = 1, Name = "P1", Category = "Apples" }, }.AsQueryable());

            // Arrange - create a Cart
            var cart = new Cart();

            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Act - add a product to the cart
            target.AddToCart(cart, 1, null);

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);
        }
Example #8
0
        public void AddingProductToCartGoesToCartScreen()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[] { new Product { ProductID = 1, Name = "P1", Category = "Apples" }, }.AsQueryable());

            // Arrange - create a Cart
            var cart = new Cart();

            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Act - add a product to the cart
            RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");

            // Assert
            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
        }
Example #9
0
        public void CanRemoveLine()
        {
            // Arrange - create some test products
            var p1 = new Product { ProductID = 1, Name = "P1" };
            var p2 = new Product { ProductID = 2, Name = "P2" };
            var p3 = new Product { ProductID = 3, Name = "P3" };

            // Arrange - create a new cart
            var target = new Cart();

            // Arrange - add some products to the cart
            target.AddItem(p1, 1);
            target.AddItem(p2, 3);
            target.AddItem(p3, 5);
            target.AddItem(p2, 1);

            // Act
            target.RemoveLine(p2);

            // Assert
            Assert.AreEqual(target.Lines.Count(c => c.Product == p2), 0);
            Assert.AreEqual(target.Lines.Count(), 2);
        }
Example #10
0
        public void CanClearContents()
        {
            // Arrange - create some test products
            var p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            var p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };

            // Arrange - create a new cart
            var target = new Cart();

            // Arrange - add some items
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            // Act - reset the cart
            target.Clear();

            // Assert
            Assert.AreEqual(target.Lines.Count(), 0);
        }
Example #11
0
        public void CanCheckoutAndSubmitOrder()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();

            // Arrange - create a cart with an item
            var cart = new Cart();
            cart.AddItem(new Product(), 1);

            // Arrange - create an instance of the controller
            var target = new CartController(null, mock.Object);

            // Act - try to checkout
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            // Assert - check that the order has been passed on to the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Once());

            // Assert - check that the method is returning the Completed view
            Assert.AreEqual("Completed", result.ViewName);

            // Assert - check that we are passing a valid model to the view
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
Example #12
0
        public void CannotCheckoutInvalidShippingDetails()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();

            // Arrange - create a cart with an item
            var cart = new Cart();
            cart.AddItem(new Product(), 1);

            // Arrange - create an instance of the controller
            var target = new CartController(null, mock.Object);

            // Arrange - add an error to the model
            target.ModelState.AddModelError("error", "error");

            // Act - try to checkout
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());

            // Assert - check that the method is returning the default view
            Assert.AreEqual(string.Empty, result.ViewName);

            // Assert - check that we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Example #13
0
        public void CannotCheckoutEmptyCart()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();

            // Arrange - create an empty cart
            var cart = new Cart();

            // Arrange - create shipping details
            var shippingDetails = new ShippingDetails();

            // Arrange - create an instance of the controller
            var target = new CartController(null, mock.Object);

            // Act
            ViewResult result = target.Checkout(cart, shippingDetails);

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());

            // Assert - check that the method is returning the default view
            Assert.AreEqual(string.Empty, result.ViewName);

            // Assert - check that we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Example #14
0
        private Cart GetCart()
        {
            var cart = (Cart)this.Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                this.Session["Cart"] = cart;
            }

            return cart;
        }
Example #15
0
 public ViewResult Summary(Cart cart)
 {
     return this.View(cart);
 }
Example #16
0
 public ViewResult Index(Cart cart, string returnUrl)
 {
     return this.View(new CartIndexViewModel { Cart = cart, ReturnUrl = returnUrl });
 }
Example #17
0
        public void CanAddQuantityForExistingLines()
        {
            // Arrange - create some test products
            var p1 = new Product { ProductID = 1, Name = "P1" };
            var p2 = new Product { ProductID = 2, Name = "P2" };

            // Arrange - create a new cart
            var target = new Cart();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 10);
            CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductID).ToArray();

            // Assert
            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, 11);
            Assert.AreEqual(results[1].Quantity, 1);
        }
Example #18
0
        public void Can_View_Cart_Contents()
        {
            // Arrange - create a Cart
            var cart = new Cart();

            // Arrange - create the controller
            var target = new CartController(null, null);

            // Act - call the Index action method
            var result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;

            // Assert
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }