public void SaveOrder(Order order) { if (order.OrderId == 0) { context.Orders.Add(order); } else { context.Entry(order).State = EntityState.Modified; } context.SaveChanges(); }
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); }