public ActionResult Clear() { //We just clear the session when you clear the cart //Obviously not the smartest thing to do, but since the only thing the session saves is the cart, it works Session["ShoppingCart"] = new ShoppingCart(); return RedirectToAction("Index"); }
// // GET: /ShoppingCart/ public ActionResult Index() { if(Session["ShoppingCart"] == null) { Session["ShoppingCart"] = new ShoppingCart(); } ShoppingCart cart = Session["ShoppingCart"] as ShoppingCart; return View(cart); }
public void AddOrder(ShoppingCart Cart, int CustomerID) { //Add the order Order newOrder = new Order() { Customer = new Facade().GetCustomerRepository().GetCustomer(CustomerID), date = DateTime.Now }; new Facade().GetOrderRepository().Add(newOrder); //Add the orderlines int OrderID = new Facade().GetOrderRepository().ReadAll().Where(x => x.CustomerId == CustomerID).OrderBy(x => x.date).LastOrDefault().Id; List<OrderLine> OrderLines = new List<OrderLine>(); foreach (OrderLineViewModel Line in Cart.OrderLines) { OrderLine newLine = new OrderLine() { MovieId = Line.MovieVM.Movie.Id, Amount = Line.Amount, OrderId = OrderID }; new Facade().GetOrderLineRepository().Add(newLine); } }
public ActionResult CompleteCheckout(string CustomerMail) { //This try catch will attempt to create your order and clear the shopping cart for a new order, if it fails, it will just redirect you to a failure page. try { ShoppingCart Cart = Session["ShoppingCart"] as ShoppingCart; OrderRepository.getInstance().AddOrder(Cart, CustomerRepository.getInstance().GetAll().Where(x => x.Email.ToLower() == CustomerMail.ToLower()).FirstOrDefault().ID); Session["ShoppingCart"] = new ShoppingCart(); return View(CustomerRepository.getInstance().GetAll().Where(x => x.Email.ToLower() == CustomerMail.ToLower()).FirstOrDefault()); } catch { return RedirectToAction("Failure"); } }
public ActionResult AddToCart(int movID) { if (Session["ShoppingCart"] == null) { Session["ShoppingCart"] = new ShoppingCart(); } ShoppingCart cart = Session["ShoppingCart"] as ShoppingCart; if(cart.OrderLines.FirstOrDefault(x => x.MovieVM.Movie.Id == movID) != null) { cart.OrderLines.FirstOrDefault(x => x.MovieVM.Movie.Id == movID).Amount += 1; } else { cart.OrderLines.Add(new OrderLineViewModel(MovieRepository.getInstance().GetAll().FirstOrDefault(x => x.Movie.Id == movID),1)); } Session["ShoppingCart"] = cart; return RedirectToAction("Index"); }