public string MakeOrder(string id) { var cartItemQuery = _context.CartItem.Include(p => p.Product) .Where(x => x.UserId == id); var order = new Order { OrderDate = DateTimeOffset.Now, UserId = id, }; foreach (var ct in cartItemQuery) { var orderItem = new OrderItem { Product = ct.Product, Quantity = ct.Quantity }; order.AddOrderItem(orderItem); _context.Remove(ct); } order.TotalPrice = order.OrderItems.Sum(x => x.Product.Price * x.Quantity); _context.Add(order); _repository.SaveChangesAsync(); return(MakeOrderMailBody(order)); }
public CartItem AddToCart(string userId, int productId, string variationName, int quantity) { var cartItemQuery = _context.CartItem.Include(p => p.Product) .Where(x => x.ProductID == productId && x.UserId == userId); var cartItem = cartItemQuery.FirstOrDefault(); if (cartItem == null) { cartItem = new CartItem { UserId = userId, ProductID = productId, Quantity = quantity, }; _context.Add(cartItem); } else { cartItem.Quantity = quantity; } _repository.SaveChangesAsync(); return(cartItem); }