public async Task <OrderContainsProduct> AddAsyn(OrderContainsProduct orderContainsProduct) { this.context.Set <OrderContainsProduct>().Add(orderContainsProduct); await this.context.SaveChangesAsync(); return(orderContainsProduct); }
public async Task Add(int userId, OrderRequest orderRequest) { var user = await this.userRepository.GetAsync(userId); if (user == null) throw new InvalidOperationException($"User with Id={userId} not found"); foreach (var productRequest in orderRequest.ProductList) { var product = await this.productRepository.GetAsync(productRequest.ProductId); if (product == null) throw new InvalidOperationException($"Product with Id={productRequest.ProductId} not found"); } var cost = orderRequest.ProductList.Sum(item => item.Quantity * item.Price); var order = new Order {UserId = userId, TotalAmount = cost}; await this.orderRepository.AddAsyn(order); foreach (var productRequest in orderRequest.ProductList) { var orderContainsProduct = new OrderContainsProduct(); orderContainsProduct.OrderId = order.Id; orderContainsProduct.ProductId = productRequest.ProductId; orderContainsProduct.Quantity = productRequest.Quantity; await this.orderContainsProductRepository.AddAsyn(orderContainsProduct); } }