private void ReduceBasketSize(OrderBasket orderBasket)
        {
            var reducedBasket = new OrderBasket() {RestaurantId = orderBasket.RestaurantId};

            Order[] orderListClone = new Order[orderBasket.MatchedOrders.Count];
            orderBasket.MatchedOrders.CopyTo(orderListClone);

            foreach (Order order in orderListClone)
            {
                // does owner of this order have other order in another basket?
                var orderOwner = order.CreatedBy;
                if (HasOrderInAnotherBasket(_orderBaskets, orderOwner, orderBasket.RestaurantId))
                {
                    // rewrite order to basket
                    orderBasket.MatchedOrders.Remove(order);
                    orderBasket.PossibleOrders.Add(order);
                }
            }

            // REVIEW: it's always false
            if (reducedBasket.MatchedOrders.Any())
            {
                _orderBaskets.Add(reducedBasket);
            }
        }
Exemple #2
0
        public async Task<IHttpActionResult> Put(CreateOrderViewModel order)
        {
            Dish dish = await UnitOfWork.DishRepository.SingleOrDefault(order.DishId, false, "Restaurant,Kind");
            if (dish == null)
            {
                return BadRequest("Wrong dish id.");
            }

            Poll currentPoll = await UnitOfWork.PollRepository.GetCurrentForGroup(CurrentGroup).FirstOrDefaultAsync();
            if (currentPoll == null)
            {
                return BadRequest("No active poll at this time.");
            }

            Order newOrder = new Order()
            {
                Dish = dish,
                //IsOptional = order.IsOptional,
                IsHelpNeeded = order.IsHelpNeeded,
                Poll = currentPoll
            };

            // validate if user doesn't have other order from the same restaurant within the same poll
            Restaurant restaurant = dish.Restaurant;
            bool hasOtherOrderInRestaurant =
                await
                    UnitOfWork.OrderRepository.GetForPoll(currentPoll)
                        .AnyAsync(o => o.CreatedBy.Id == CurrentUser.Id && o.Dish.Restaurant.Id == restaurant.Id);

            if (!hasOtherOrderInRestaurant)
            {
                await UnitOfWork.OrderRepository.Insert(newOrder);

                return Ok(new OrderViewModel()
                {
                    DishId = newOrder.Dish.Id,
                    Dish = newOrder.Dish.Name,

                    KindId = newOrder.Dish.Kind.Id,
                    Kind = newOrder.Dish.Kind.Name,

                    RestaurantId = newOrder.Dish.Restaurant.Id,
                    Restaurant = newOrder.Dish.Restaurant.Name,

                    //IsOptional = newOrder.IsOptional,
                    IsHelpNeeded = newOrder.IsHelpNeeded,

                    UserEmail = newOrder.CreatedBy.Email
                });
            }
            else
            {
                return BadRequest("Current user already have ordered in this restaurant.");
            }
        }
        private void BalanceBasket(OrderBasket orderBasket)
        {
            Order[] orderListClone = new Order[orderBasket.MatchedOrders.Count];
            orderBasket.MatchedOrders.CopyTo(orderListClone);

            foreach (Order order in orderListClone)
            {
                // did this user have order in other, currently smaller basket?
                var orderOwner = order.CreatedBy;

                IEnumerable<Guid> currentlySmallerBaskets =
                    _orderBaskets.Where(b => (b.MatchedOrders.Count() < orderBasket.MatchedOrders.Count() || (b.MatchedOrders.Count() == orderBasket.MatchedOrders.Count() && orderBasket.RestaurantId != b.RestaurantId)))
                        .OrderByDescending(b => b.MatchedOrders.Count())
                        .Select(b => b.RestaurantId);

                OrderBasket smallerBasket =
                    _orderBaskets.FirstOrDefault(b => currentlySmallerBaskets.Contains(b.RestaurantId) &&
                                                      b.PossibleOrders.Any(o => o.CreatedBy.Id == orderOwner.Id));

                if (smallerBasket != null)
                {
                    // if so, remove order from this basket
                    orderBasket.MatchedOrders.Remove(order);
                    // while balancing DO NOT save order in PossibleOrders to avoid infinite loops

                    // and rewrite previously removed orders into smaller basket
                    RestoreOrder(smallerBasket, orderOwner);

                    CleanupEmptyBaskets();

                    break;
                }
            }
        }
Exemple #4
0
        private List<Order> GenerateRandomOrders(Random random, List<ApplicationUser> users, List<Dish> dishes, int maxOrdersPerUser)
        {
            var result = new List<Order>();
            foreach (var user in users)
            {
                var ordersCount = random.Next(1, maxOrdersPerUser + 1);
                for (var i = 0; i < ordersCount; i++)
                {
                    var hasUserOrderedAnotherDishInThisRestaurant = false;
                    int doIterations = 0; // cutout
                    Order newOrder;
                    do
                    {
                        newOrder = new Order()
                        {
                            Id = Guid.NewGuid(),
                            Dish = dishes[random.Next(0, dishes.Count)],
                            CreatedBy = users[random.Next(0, users.Count)],
                            IsHelpNeeded = random.Next(0, 2) == 1
                        };

                        hasUserOrderedAnotherDishInThisRestaurant = result.Any(o =>
                            o.Dish.Restaurant.Id == newOrder.Dish.Restaurant.Id
                            && o.CreatedBy.Id == newOrder.CreatedBy.Id
                            );
                        doIterations++;
                    } while (hasUserOrderedAnotherDishInThisRestaurant && doIterations < 1000);

                    if (doIterations < 1000)
                    {
                        result.Add(newOrder);
                    }
                }
            }
            return result;
        }