public ViewResult Index(Cart cart, string returnUrl) { return View(new CartIndexViewModel { Cart = cart, ReturnUrl = returnUrl, }); }
public RedirectToRouteResult RemoveFromCart(Cart cart, int PizzaId, string returnUrl) { Pizza pizza = repository.Pizzas.FirstOrDefault(p => p.PizzaId == PizzaId); if (pizza != null) { cart.RemoveLine(pizza); } return RedirectToAction("Index", new { returnUrl }); }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { Cart cart = (Cart)controllerContext.HttpContext.Session[sessionKey]; if (cart == null) { cart = new Cart(); controllerContext.HttpContext.Session[sessionKey] = cart; } return cart; }
public void IndexActionLoads() { //Arrange Mock<IPizzaRepository> mock = new Mock<IPizzaRepository>(); CartController controller = new CartController(mock.Object, null, null, null, null, null); Cart cart = new Cart(); //Action ViewResult result = controller.Index(cart, null) as ViewResult; //Assert Assert.AreEqual(result.ViewName, ""); }
public void CanViewCart() { //Arrange Cart cart = new Cart(); Delivery delivery = new Delivery(); CartController controller = new CartController(null, null, null, null, null, null); //Action CartIndexViewModel result = (CartIndexViewModel)controller.Index(cart, "testUrl").ViewData.Model; //Assert Assert.AreSame(result.Cart, cart); Assert.AreEqual(result.ReturnUrl, "testUrl"); }
public void CanAddANewLine() { //Arrange Pizza p1 = new Pizza { PizzaId = 1, Name = "Pizza1" }; Pizza p2 = new Pizza { PizzaId = 2, Name = "Pizza2" }; Cart cart = new Cart(); //Action cart.AddItem(p1, 1); cart.AddItem(p2, 1); CartLine[] results = cart.Lines.ToArray(); //Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[1].Pizza, p2); }
public ViewResult Checkout(Cart cart) { var context = new UsersContext(); var username = User.Identity.Name; var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username); return View(new CartCheckoutViewModel { User = user, Cart = cart, DeliveryTypes = deliveryRepository.Deliveries.ToList(), Voucher = null }); //return View(user); }
public void CanAddToCart() { //Arrange Mock<IPizzaRepository> mock = new Mock<IPizzaRepository>(); mock.Setup(p => p.Pizzas).Returns(new Pizza[]{ new Pizza {PizzaId = 1, Name = "Pizza1"}, }.AsQueryable()); Cart cart = new Cart(); CartController controller = new CartController(mock.Object, null, null, null, null, null); //Action controller.AddToCart(cart, 1, null); //Assert Assert.AreEqual(cart.Lines.Count(), 1); }
public void CalcCartTotal() { //Arrange Pizza p1 = new Pizza { PizzaId = 1, Name = "Pizza1", Price = 5M }; Pizza p2 = new Pizza { PizzaId = 2, Name = "Pizza2", Price = 3.50M }; Pizza p3 = new Pizza { PizzaId = 3, Name = "Pizza3", Price = 51M }; Cart cart = new Cart(); //Action cart.AddItem(p1, 1); cart.AddItem(p2, 2); cart.AddItem(p3, 1); decimal result = cart.GetTotalValue(); //Assert Assert.AreEqual(result, 63M); }
public void AddToCartGoesToShoppingList() { //Arrange Mock<IPizzaRepository> mock = new Mock<IPizzaRepository>(); mock.Setup(p => p.Pizzas).Returns(new Pizza[]{ new Pizza {PizzaId = 1, Name = "Pizza1"}, }.AsQueryable()); Cart cart = new Cart(); CartController controller = new CartController(mock.Object, null, null, null, null, null); //Action RedirectToRouteResult result = controller.AddToCart(cart, 1, "testUrl"); //Assert Assert.AreEqual(result.RouteValues["action"], "Index"); Assert.AreEqual(result.RouteValues["returnUrl"], "testUrl"); }
public void CanAddQuantityToExistingLine() { //Arrange Pizza p1 = new Pizza { PizzaId = 1, Name = "Pizza1" }; Pizza p2 = new Pizza { PizzaId = 2, Name = "Pizza2" }; Cart cart = new Cart(); //Action cart.AddItem(p1, 1); cart.AddItem(p2, 1); cart.AddItem(p1, 5); CartLine[] results = cart.Lines.OrderBy(p => p.Pizza.PizzaId).ToArray(); //Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Quantity, 6); Assert.AreEqual(results[1].Quantity, 1); }
public void CanRemoveLine() { //Arrange Pizza p1 = new Pizza { PizzaId = 1, Name = "Pizza1" }; Pizza p2 = new Pizza { PizzaId = 2, Name = "Pizza2" }; Pizza p3 = new Pizza { PizzaId = 3, Name = "Pizza3" }; Cart cart = new Cart(); //Action cart.AddItem(p1, 1); cart.AddItem(p2, 2); cart.AddItem(p1, 5); cart.AddItem(p3, 3); cart.RemoveLine(p1); //Assert Assert.AreEqual(cart.Lines.Count(), 2); Assert.AreEqual(cart.Lines.Where(p => p.Pizza == p1).Count(), 0); }
public RedirectToRouteResult AddToCart(Cart cart, int PizzaId, string returnUrl) { Pizza pizza = repository.Pizzas.FirstOrDefault(p => p.PizzaId == PizzaId); if (pizza != null) { if (pizza.Name == "Create Your Own") { foreach (var topping in Session["ToppingList"] as List<Topping>) { pizza.Price += topping.Price; } cart.AddItem(pizza, 1); } else { cart.AddItem(pizza, 1); } } return RedirectToAction("Index", new { returnUrl }); }
public PartialViewResult Summary(Cart cart) { return PartialView(cart); }
public void CanEnterValidVoucher() { //Arrange Cart cart = new Cart(); CartController controller = new CartController(null, null, null, null, null, null); //Action String voucherResponse = controller.VoucherCheck("PD201401062"); //Assert Assert.AreEqual("Voucher PD201401062 is a valid code. GREAT DEAL!", voucherResponse); }
public ActionResult RetrieveSavedOrder(Cart cart) { var orderlineQuery = orderlineRepository.Orderlines.Where(u => u.UserId == WebSecurity.CurrentUserId).ToList(); cart.Clear(); foreach (var orderline in orderlineQuery) { if (orderline != null) { if (orderline.OrderId == 0) { Pizza pizza = new Pizza(); pizza.PizzaId = orderline.PizzaId; pizza.Name = repository.Pizzas.FirstOrDefault(p => p.PizzaId == pizza.PizzaId).Name; pizza.Size = repository.Pizzas.FirstOrDefault(p => p.PizzaId == pizza.PizzaId).Size; pizza.Price = orderline.OrderlinePrice; cart.AddItem(pizza, 1); TempData["message"] = string.Format("Order Retrieved"); } } } if(orderlineQuery.Count() == 0){ TempData["message"] = string.Format("No Items Saved"); } return RedirectToAction("Index"); }
public ActionResult SaveCurrentOrder(Cart cart) { var orderlineQuery = orderlineRepository.Orderlines.Where(u => u.UserId == WebSecurity.CurrentUserId).ToList(); foreach (var orderline in orderlineQuery) { if (orderline != null) { orderlineRepository.ClearOrderline(orderline); } } for(int i = 0; i<cart.Lines.Count(); i++){ var line = cart.Lines.ElementAt(i); for (int j = 0; j < line.Quantity; j++) { Orderline orderline = new Orderline(); orderline.PizzaId = line.Pizza.PizzaId; orderline.OrderlinePrice = line.Pizza.Price; orderline.UserId = WebSecurity.CurrentUserId; orderlineRepository.SaveOrderline(orderline); } } TempData["message"] = string.Format("Order saved"); return RedirectToAction("Index"); }
public ViewResult ConfirmCheckout(Cart cart, int deliveryId) { Delivery delivery = deliveryRepository.Deliveries.FirstOrDefault(d => d.DeliveryId == deliveryId); Order order = new Order(); order.PriceBeforeVouchers = cart.GetTotalValue(); order.UserId = WebSecurity.CurrentUserId; order.TimeSubmitted = DateTime.Now; order.DeliveryId = deliveryId; order.Subtotal = order.PriceBeforeVouchers + delivery.Cost; order.Total = order.Subtotal; order.Status = "Processed"; orderRepository.SaveOrder(order); int orderId = orderRepository.Orders.FirstOrDefault(o => o.TimeSubmitted == order.TimeSubmitted).OrderId; List<Pizza> pizzas = new List<Pizza>(); foreach(var cartLine in cart.Lines){ for(int i=0;i<cartLine.Quantity;i++) { Orderline orderline = new Orderline(); orderline.PizzaId = cartLine.Pizza.PizzaId; orderline.OrderlinePrice = cartLine.Pizza.Price * cartLine.Quantity; orderline.UserId = WebSecurity.CurrentUserId; orderline.OrderId = order.OrderId; orderlineRepository.SaveOrderline(orderline); pizzas.Add(repository.Pizzas.FirstOrDefault(p => p.PizzaId == orderline.PizzaId)); Pizza pizza = repository.Pizzas.FirstOrDefault(p => p.PizzaId == orderline.PizzaId); if (pizza.Name == "Create Your Own") { foreach (var topping in Session["ToppingList"] as List<Topping>) { PizzaToppingOrder pizzaToppingOrder = new PizzaToppingOrder(); pizzaToppingOrder.OrderlineId = orderline.OrderlineId; pizzaToppingOrder.ToppingId = topping.ToppingId; pizzaToppingOrderRepository.SavePizzaToppingOrder(pizzaToppingOrder); } } } } cart.Clear(); ViewBag.PriceBeforeVouchers = order.PriceBeforeVouchers; ViewBag.DeliveryCost = delivery.Cost; ViewBag.PriceIncDelivery = order.PriceBeforeVouchers + delivery.Cost; ViewBag.DeliveryType = delivery.DeliveryType; var context = new UsersContext(); var username = User.Identity.Name; var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username); List<Topping> toppings = Session["ToppingList"] as List<Topping>; return View(new ConfirmCheckoutViewModel { User = user, Order = order, Pizzas = pizzas, //orderlineRepository.Orderlines.Where(ol => ol.OrderId == order.OrderId).ToList() Toppings = toppings }); //return View(user); }
public void CanClearCart() { //Arrange Pizza p1 = new Pizza { PizzaId = 1, Name = "Pizza1", Price = 5M }; Pizza p2 = new Pizza { PizzaId = 2, Name = "Pizza2", Price = 3.50M }; Pizza p3 = new Pizza { PizzaId = 3, Name = "Pizza3", Price = 51M }; Cart cart = new Cart(); //Action cart.AddItem(p1, 1); cart.AddItem(p2, 2); cart.AddItem(p3, 1); cart.Clear(); //Assert Assert.AreEqual(cart.Lines.Count(), 0); }
public void CanEnterInvalidVoucher() { //Arrange Cart cart = new Cart(); CartController controller = new CartController(null, null, null, null, null, null); //Action String voucherResponse = controller.VoucherCheck("PD1401062"); //Assert Assert.AreEqual("Voucher 'PD1401062' is not a valid code.", voucherResponse); }