public async Task <bool> TryToSaveOrderAsync(OrderInfo order) { if (order.SubOrders == null || order.SubOrders.Count == 0) { return(false); } // Try to change count of products if (!await TryToChangeCountOfProductsAsync(order.SubOrders.Select(s => Tuple.Create(s.ProductId, s.Count)))) { return(false); } // Add order and get order id await Orders.AddAsync(order); await SaveChangesAsync(); var id = Orders.Last().Id; // Order sub orders (to add relations) order.SubOrders = order.SubOrders .OrderBy(s => s.ProductId) .ToList(); // Add relations and save sub orders for (int i = 0; i < order.SubOrders.Count; i++) { var subOrder = order.SubOrders[i]; subOrder.OrderId = id; SubOrders.Add(subOrder); for (int j = i + 1; j < order.SubOrders.Count; j++) { var relation = await GetRelationAsync(new object[] { subOrder.ProductId, order.SubOrders[j].ProductId }); if (relation == null) { await AddRelationAsync(new Relation() { LesserId = subOrder.ProductId, BiggerId = order.SubOrders[j].ProductId, Count = 1 }); } else { relation.Count += 1; } } } await SaveChangesAsync(); return(true); }
public async Task BuyProductAsync(OrderInputViewModel order) { // Mapping order information(which contains user information) to user domain model. var newUser = Mapper.Map <User>(order); var newOrder = new Order() { User = newUser, OrderDate = DateTime.UtcNow, OrderItems = new List <OrderItem>() }; newOrder.OrderItems.Add(new OrderItem() { Order = newOrder, ProductId = order.ProductId }); // Saving information about order to all repositories. await Orders.AddAsync(newOrder); }