public async Task <long> AddNewOrderAsync(AddOrderRequest addOrderRequest) { await using var transaction = await context.Database.BeginTransactionAsync(); var unitPrices = (await context.Products .Where(x => addOrderRequest.OrderItems.Select(oi => oi.ProductId).Contains(x.Id)) .Select(x => new { x.UnitPrice, x.Id }) .ToListAsync()); var dbOrder = new Order(); dbOrder.CustomerId = addOrderRequest.CustomerId; dbOrder.EmployeeId = addOrderRequest.EmployeeId; dbOrder.OrderDate = DateTime.Now.Date; dbOrder.RequiredDate = addOrderRequest.RequiredDate.Date; dbOrder.ShipAddress = addOrderRequest.ShipAddress; dbOrder.ShipCity = addOrderRequest.ShipCity; dbOrder.ShipCountry = addOrderRequest.ShipCountry; dbOrder.ShipName = addOrderRequest.ShipName; dbOrder.ShipPostalCode = addOrderRequest.ShipPostalCode; context.Orders.Add(dbOrder); await context.SaveChangesAsync(); foreach (var item in addOrderRequest.OrderItems) { decimal unitPrice = unitPrices .Where(x => x.Id == item.ProductId) .Select(x => x.UnitPrice) .SingleOrDefault(); OrderDetail dbOrderDetail = new OrderDetail(); dbOrderDetail.OrderId = dbOrder.Id; dbOrderDetail.ProductId = item.ProductId; dbOrderDetail.Id = $"{dbOrder.Id}/{item.ProductId}"; dbOrderDetail.UnitPrice = unitPrice; dbOrderDetail.Quantity = item.Quantity; dbOrderDetail.Discount = 0; context.OrderDetails.Add(dbOrderDetail); } await context.SaveChangesAsync(); await transaction.CommitAsync(); return(dbOrder.Id); }
protected async virtual Task GenerateCategoriesAsync() { context.Categories.Add(new Category() { CategoryName = "Beverages", Description = "Soft drinks, coffees, teas, beers, and ales" }); context.Categories.Add(new Category() { CategoryName = "Condiments", Description = "Sweet and savory sauces, relishes, spreads, and seasonings" }); context.Categories.Add(new Category() { CategoryName = "Confections", Description = "Desserts, candies, and sweet breads" }); context.Categories.Add(new Category() { CategoryName = "Dairy Products", Description = "Cheeses" }); context.Categories.Add(new Category() { CategoryName = "Grains/Cereals", Description = "Breads, crackers, pasta, and cereal" }); context.Categories.Add(new Category() { CategoryName = "Meat/Poultry", Description = "Prepared meats" }); context.Categories.Add(new Category() { CategoryName = "Produce", Description = "Dried fruit and bean curd" }); context.Categories.Add(new Category() { CategoryName = "Seafood", Description = "Seaweed and fish" }); await context.SaveChangesAsync(); }
public async Task AddNewProductAsync(AddProductRequest request) { var dbProduct = new Product(); dbProduct.ProductName = request.ProductName; dbProduct.UnitsInStock = request.UnitsInStock; dbProduct.UnitPrice = request.UnitPrice; dbProduct.QuantityPerUnit = request.QuantityPerUnit; dbProduct.Discontinued = request.Discontinued; dbProduct.CategoryId = request.CategoryId; dbProduct.SupplierId = request.SupplierId; context.Products.Add(dbProduct); await context.SaveChangesAsync(); }