Пример #1
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (!cart.Lines.Any())
            {
                ModelState.AddModelError(string.Empty, Resources.EmptyCartError);
            }

            if (ModelState.IsValid)
            {
                _cartApplicationService.Process(cart, shippingDetails);
                return View("Completed");
            }

            return View(shippingDetails);
        }
Пример #2
0
        private Order ProcessTheOrder(Cart cart, ShippingDetails shippingDetails)
        {
            var order = new Order();
            var orderDetailsList = new List<OrderDetail>();

            try
            {
                foreach (var item in cart.Lines)
                {
                    var orderDetail = new OrderDetail
                    {
                        OrderId = order.OrderId,
                        ProductName = item.Product.Name,
                        Quantity = item.Quantity,
                        UnitPrice = item.Product.Price
                    };
                    order.TotalCost += orderDetail.UnitPrice*orderDetail.Quantity;
                    //TODO: Make Unit Test
                    orderDetailsList.Add(orderDetail);
                }
            }
            catch (Exception)
            {
                // TODO: Add HttpException handling in place of Exception below
                throw new Exception("Erorr building list of oder details -> cart.Lines was probably null.");
            }

            var firstCartLineProduct = cart.Lines.FirstOrDefault();
            int establishmentId = firstCartLineProduct.Product.EstablishmentId;

            order.OrderDetails = orderDetailsList;
            order.EstablishmentId = establishmentId;
            order.CustomerName = shippingDetails.Name;
            order.TimeProcessed = DateTime.Now;
            return order;
        }
Пример #3
0
 public void Process(Cart cart, ShippingDetails shippingDetails)
 {
     var order = ProcessTheOrder(cart, shippingDetails);
     orderRepository.CreateOrder(order);
     cart.Clear();
 }
Пример #4
0
        public void CanCheckoutAndSubmitOrder()
        {
            // Arrange - create a mock order processor
            var mockRepository = new Mock<IProductRepository>();
            var what = new Mock<ICartApplicationService>();
            // Arrange - create a cart with an item
            var cart = new Cart();
            var shippingDetails = new ShippingDetails
                {
                    Name = "Jason",
                    Line1 = "123 Fake",
                    City = "Corpus Christi",
                    State = "Texas",
                    Zip = "78414",
                    Country = "United States",
                    GiftWrap = false
                };
            cart.AddItem(new Product {ProductId = 1, Name = "P1", Price = 100M, EstablishmentId = 1, CategoryId = 1, Description = "none"}, 1);
            // Arrange - create an instance of the controller

            var controller = new CartController(mockRepository.Object, what.Object);

            // Action - try to checkout
            var result = controller.Checkout(cart, shippingDetails);

            // Assert - check that the order has been passed on to the processor
            what.Verify(m => m.Process(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);
        }
Пример #5
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 controller = new CartController(null, null);

            // Action
            ViewResult result = controller.Checkout(cart, shippingDetails);

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.CreateOrder(It.IsAny<Order>()),
                        Times.Never());
            // Assert - check that the method is returning the default view
            Assert.AreEqual("", result.ViewName);
            // Assert - check that we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }