public async Task<IActionResult> AddressAndPayment(OrderHeader order) { var formCollection = await Context.Request.GetFormAsync(); try { if (string.Equals(formCollection.GetValues("PromoCode").FirstOrDefault(), PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return View(order); } else { // TODO [EF] Swap to store generated identity key when supported var nextId = db.Orders.Any() ? db.Orders.Max(o => o.OrderId) + 1 : 1; order.OrderId = nextId; order.Username = this.Context.User.Identity.GetUserName(); order.OrderDate = DateTime.Now; //Add the Order db.Orders.Add(order); //Process the order var cart = ShoppingCart.GetCart(db, this.Context); cart.CreateOrder(order); // Save all changes db.SaveChanges(); return RedirectToAction("Complete", new { id = order.OrderId }); } } catch { //Invalid - redisplay with errors return View(order); } }
public int CreateOrder(OrderHeader order) { decimal orderTotal = 0; var cartItems = GetCartItems(); // TODO [EF] Swap to store generated identity key when supported var nextId = _db.OrderDetails.Any() ? _db.OrderDetails.Max(o => o.OrderDetailId) + 1 : 1; // Iterate over the items in the cart, adding the order details for each foreach (var item in cartItems) { //var album = _db.Albums.Find(item.AlbumId); var album = _db.Albums.Single(a => a.AlbumId == item.AlbumId); var orderDetail = new OrderDetail { OrderDetailId = nextId, AlbumId = item.AlbumId, OrderId = order.OrderId, UnitPrice = album.Price, Quantity = item.Count, }; // Set the order total of the shopping cart orderTotal += (item.Count * album.Price); _db.OrderDetails.Add(orderDetail); nextId++; } // Set the order's total to the orderTotal count order.Total = orderTotal; // Empty the shopping cart EmptyCart(); // Return the OrderId as the confirmation number return order.OrderId; }