IQueryable <Order> GetActiveOrderByUser(PPUser user) { return(_dbContext.Orders.Where(order => order.PpUser == user && (order.Status == OrderStatus.NewOrder || order.Status == OrderStatus.Pending || order.Status == OrderStatus.Shipped))); }
public async Task <bool> CreateOrder(NewPizzaOrder newPizzaOrder, PPUser user) { List <OrderPizza> pizzas = new List <OrderPizza>(newPizzaOrder.Pizzas.Count); var orderPizzaCount = 9; var orderTotalCost = 0; foreach (var pizzaInOrder in newPizzaOrder.Pizzas) { var dbPizza = await _dbContext.Pizzas.FindAsync(pizzaInOrder.Key); if (dbPizza == null) { return(false); } orderPizzaCount += pizzaInOrder.Value; orderTotalCost += pizzaInOrder.Value * dbPizza.Price; pizzas.Add(new OrderPizza { Pizza = dbPizza, Amount = pizzaInOrder.Value, UnitCost = dbPizza.Price }); } var activeOrders = GetActiveOrderByUser(user); var totalPizzaCount = await activeOrders.SumAsync(order => order.Pizzas.Sum(orderPizza => orderPizza.Amount)) + orderPizzaCount; var totalCost = await activeOrders.SumAsync(order => order.Pizzas.Sum(orderPizza => orderPizza.UnitCost *orderPizza.Amount)) + orderTotalCost; if (totalPizzaCount > MAX_PIZZA_COUNT || totalCost > MAX_ORDER_COST) { return(false); } var newOrder = new Order(user, pizzas); newOrder.Status = OrderStatus.Pending; _dbContext.Orders.Add(newOrder); await _dbContext.SaveChangesAsync(); return(true); }