/// <inheritdoc /> public async Task <Result> AddOrder(OrderDto orderDto) { await using var transaction = await _context.Database.BeginTransactionAsync(); try { var customer = await _context.Customers.FindAsync(orderDto.CustomerId); var productsQuery = await _context.Products.Where(x => orderDto.ProductsId.Contains(x.Id)).ToListAsync(); var order = new Order { Customer = customer ?? null, Sum = orderDto.Sum, TimeAdd = orderDto.TimeAdd, TimeUpdate = orderDto.TimeUpdate }; await _context.AddAsync(order); await _context.SaveChangesAsync(); if (productsQuery.Count > 0) { var products = productsQuery.Select(x => new OrderProduct { OrderId = order.Id, Order = order, Product = x, ProductId = x.Id }); await _context.AddRangeAsync(products); await _context.SaveChangesAsync(); } transaction.Commit(); return(Result.Ok()); } catch (Exception e) { await transaction.RollbackAsync(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }