public IActionResult CourierDelivery(OrderCourier order) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var text = "Your type of order - courier delivery. For more information follow the link: https://mayakelectronics20191126070610.azurewebsites.net/Account/YourAccount"; var items = _cart.GetCartItems(); ViewBag.Total = _cart.GetCartTotal(); var user = _db.Users.FirstOrDefault(u => u.Id == User.FindFirstValue(ClaimTypes.NameIdentifier)); ViewBag.Email = user.Email; ViewBag.Items = items; if (items.Count == 0) { ModelState.AddModelError("", "Cart is empty"); } new EfProductRepository().UpdateAreBoughts(items.Select(i => i.Product).ToList()); if (ModelState.IsValid) { EmailSender.SendEmail(order.Email, text); _cart.ClearCart(); _orderRepository.CreateOrder(order, userId); return(RedirectToAction("Complete")); } return(View(order)); }
public void CreateOrder(OrderCourier order, string userId) { order.UserId = userId; order.OrderPlaced = DateTime.Now; _db.OrdersCourier.Add(order); _db.SaveChanges(); order.OrderTotal = _cart.GetCartTotal(); var cartItems = _cart.CartItems; foreach (var item in cartItems) { var orderDetail = new OrderDetailCourier() { Amount = item.Amount, ProductId = item.Product.Id, OrderCourierId = order.OrderCourierId, Price = item.Product.Price }; _db.OrderDetailsCourier.Add(orderDetail); } _db.SaveChanges(); }