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 void RemoveCartItem(string userId, int productId, string variationName) { var cartItemQuery = _context.CartItem.Include(p => p.Product) .Where(x => x.ProductID == productId && x.UserId == userId); var cartItem = cartItemQuery.FirstOrDefault(); if (cartItem != null) { _context.Remove(cartItem); } else { //error } _repository.SaveChangesAsync(); }