public async Task <IActionResult> ByBoard([FromQuery] ByBoardScoresQuery filter = null) { if (filter?.IsApproved != true) { var isAdmin = await _auth.AuthorizeAsync(User, "AppAdmin").ConfigureAwait(false); // if the user is requesting unapproved scores, they must be an admin if (!isAdmin.Succeeded) { return(Unauthorized()); } } return(Ok(GetScoresByBoard(filter))); }
public async Task <IDictionary <string, IEnumerable <ScoreViewModel> > > GetScoresByBoard(ByBoardScoresQuery filter = null) { var top = filter?.Top; var topBoards = filter?.TopBoards; // if we're selecting only x top boards, then 'top' referring to the top count from each board. // so we have to apply the top filter after we've performed the grouping if (topBoards != null) { filter.Top = null; } // EF Core doesn't support groupby, so we have to evaluate the query and do it in memory var scores = await GetScores(filter).ToArrayAsync().ConfigureAwait(false); var scoresQuery = scores.GroupBy(s => s.Board.Id); if (topBoards != null) { scoresQuery = scoresQuery.Take((int)filter.TopBoards); } if (top != null) { scoresQuery = scoresQuery.SelectMany(g => g.Take((int)top).GroupBy(s => s.Board.Id)); } var scoresByBoard = scoresQuery.ToDictionary(g => g.Key, g => g.AsEnumerable()); return(scoresByBoard); }