Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,FullName,Email,Phone,Address,Comment,TotalPrice,OrderStatus")] Order order)
        {
            if (ModelState.IsValid)
            {
                var cartItems = cartService.GetProducts();
                order.TotalPrice = cartItems.Sum(x => x.Product.Price * x.Amount);
                _context.Add(order);
                await _context.SaveChangesAsync();


                var orderProducts = cartItems.Select(x => new OrderProduct
                {
                    OrderId   = order.Id,
                    ProductId = x.Product.Id,
                    Amount    = x.Amount
                });

                _context.OrderProducts.AddRange(orderProducts);
                await _context.SaveChangesAsync();

                cartService.Clear();

                StringBuilder content = new StringBuilder(1000);
                foreach (var item in cartItems)
                {
                    content.Append($"{item.Product.Title} {item.Amount}<br>");
                }
                await emailSender.SendEmailAsync(order.Email, "Order from EShop", content.ToString());

                return(RedirectToAction("Index", "Home"));
            }
            return(View(order));
        }
Beispiel #2
0
 public IActionResult ClearAllInCart()
 {
     cartService.Clear();
     return(RedirectToAction("Index", "Home"));
 }