public async Task<IHttpActionResult> GetMatchesForPoll(Guid pollId) { var query = UnitOfWork.OrderRepository.GetForPoll(pollId, "Dish.Kind,CreatedBy"); List<Order> orders = query.ToList(); if (orders.Count > 0) { OrderMatchHandler matchHandler = new OrderMatchHandler(orders); matchHandler.ProcessOrders(); // add not matched orders var matches = matchHandler.AddNotMatchedOrders(); var viewModelQuery = matches.Select(b => new MatchedOrderViewModel() { Captain = b.Captain != null ? b.Captain.Email : null, Orders = b.MatchedOrders.Select(o => new OrderViewModel() { Id = o.Id, RestaurantId = b.RestaurantId, Dish = o.Dish.Name, DishId = o.Dish.Id, Kind = o.Dish.Kind.Name, KindId = o.Dish.Kind.Id, IsHelpNeeded = o.IsHelpNeeded, Restaurant = !b.IsNotMatched ? o.Dish.Restaurant.Name : "Not matched", UserEmail = o.CreatedBy.Email }).ToList(), RestaurantId = !b.IsNotMatched ? b.RestaurantId : new Guid() }); var viewModelList = viewModelQuery.ToList(); return Ok(viewModelList); } else { return Ok(new List<MatchedOrderViewModel>()); } }
public async Task<ActionResult> Finish() { Poll poll = await UnitOfWork.PollRepository.GetCurrentForGroup(CurrentGroup, "CreatedBy").SingleAsync(); if (poll.CreatedBy.Id != CurrentUser.Id) { return new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Unauthorized"); } List<Order> orders = UnitOfWork.OrderRepository.GetForPoll(poll).ToList(); List<OrderBasket> matches = new List<OrderBasket>(); if (orders.Count > 0) { OrderMatchHandler matchHandler = new OrderMatchHandler(orders); matchHandler.ProcessOrders(); // add not matched orders matches = matchHandler.AddNotMatchedOrders(); } if (poll.IsFinished) // is it "second round"? { if (poll.IsResolved) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Poll already closed"); } // to prevent "holding up" the poll, second attempt is always resolving it poll.IsResolved = true; await ProcessResolvedPoll(orders, matches); } else { poll.IsFinished = true; // mark poll as resolved only when there are no non-matched orders poll.IsResolved = matches.Any(m => m.IsNotMatched == false && m.MatchedOrders.Count() == 1) == false; if (poll.IsResolved) { await ProcessResolvedPoll(orders, matches); } else { var singleOrders = matches.Where(o => o.MatchedOrders.Count() == 1 && o.IsNotMatched == false) .SelectMany(o => o.MatchedOrders).ToList(); // if poll is not resolved, notify owners of not-matched orders await SendFinalizeWarnings(singleOrders); } } await UnitOfWork.PollRepository.Update(poll); // TODO: make PollViewModel constructor that handles rewriting return Json(new PollViewModel() { Id = poll.Id, Group = CurrentGroup.Name, Orders = null, // i'm too lazy ETA = poll.ETA, FinishedAt = poll.FinishedAt, IsFinished = poll.IsFinished, IsResolved = poll.IsResolved, Remarks = poll.Remarks }); }