public ActionResult GetReport(DateTimeRangeReport range) { if (!ModelState.IsValid) { return(BadRequest()); } return(Ok(_service.FindBestManager(range))); }
public DateTimeRangeReport FindBestManager(DateTimeRangeReport range) { var groupedDeals = _context.Deals .Where(x => x.Date >= range.Start && x.Date < range.End) .GroupBy(x => x.ManagerId) .Select(g => new { ManagerId = g.Key, Count = g.Count() }) .OrderByDescending(c => c.Count); var bestDeal = groupedDeals.First(); var bestManagerId = bestDeal.ManagerId; var bestManager = GetManager(bestManagerId); if (bestManager == null) { return(null); } range.DealsCount = bestDeal.Count; range.Manager = bestManager; return(range); }