private async Task <IActionResult> NewMethod1(int page, int pageSize, string mode, string region, LeaderboardViewModel vm) { try { // Form the query predicate. // This expression selects all scores that match the provided game // mode and region (map). // Select the score if the game mode or region is empty. Expression <Func <Score, bool> > queryPredicate = score => (string.IsNullOrEmpty(mode) || score.GameMode == mode) && (string.IsNullOrEmpty(region) || score.GameRegion == region); // Fetch the total number of results in the background. var countItemsTask = _scoreRepository.CountItemsAsync(queryPredicate); // Fetch the scores that match the current filter. IEnumerable <Score> scores = await _scoreRepository.GetItemsAsync( queryPredicate, // the predicate defined above score => score.HighScore, // sort descending by high score page - 1, // subtract 1 to make the query 0-based pageSize ); // Wait for the total count. vm.TotalResults = await countItemsTask; // Set previous and next hyperlinks. if (page > 1) { vm.PrevLink = $"/?page={page - 1}&pageSize={pageSize}&mode={mode}®ion={region}#leaderboard"; } if (vm.TotalResults > page * pageSize) { vm.NextLink = $"/?page={page + 1}&pageSize={pageSize}&mode={mode}®ion={region}#leaderboard"; } // Fetch the user profile for each score. // This creates a list that's parallel with the scores collection. var profiles = new List <Task <Profile> >(); foreach (var score in scores) { profiles.Add(_profileRespository.GetItemAsync(score.ProfileId)); } Task <Profile> .WaitAll(profiles.ToArray()); // Combine each score with its profile. vm.Scores = scores.Zip(profiles, (score, profile) => new ScoreProfile { Score = score, Profile = profile.Result }); return(View(vm)); } catch (Exception) { return(View(vm)); } }
public void FetchCountRequestedGameRegion(string gameRegion) { // Form the query predicate. // This expression selects all scores for the provided game region. Expression <Func <Score, bool> > queryPredicate = score => (score.GameRegion == gameRegion); // Fetch the scores. Task <int> scoresTask = _scoreRepository.CountItemsAsync( queryPredicate // the predicate defined above ); int scores = scoresTask.Result; // Verify that each score's game region matches the provided game region. Assert.That(scores, Is.GreaterThan(0)); }
public async Task <IActionResult> Index( int page = 1, int pageSize = 10, string mode = "", string region = "" ) { // Create the view model with initial values we already know. var vm = new LeaderboardViewModel { Page = page, PageSize = pageSize, SelectedMode = mode, SelectedRegion = region, GameModes = new List <string>() { "Solo", "Duo", "Trio" }, GameRegions = new List <string>() { "Milky Way", "Andromeda", "Pinwheel", "NGC 1300", "Messier 82", } }; try { // Form the query predicate. // This expression selects all scores that match the provided game // mode and region (map). // Select the score if the game mode or region is empty. Expression <Func <Score, bool> > queryPredicate = score => (string.IsNullOrEmpty(mode) || score.GameMode == mode) && (string.IsNullOrEmpty(region) || score.GameRegion == region); // Fetch the total number of results in the background. var countItemsTask = _scoreRepository.CountItemsAsync(queryPredicate); // Fetch the scores that match the current filter. IEnumerable <Score> scores = await _scoreRepository.GetItemsAsync( queryPredicate, // the predicate defined above score => score.HighScore, // sort descending by high score page - 1, // subtract 1 to make the query 0-based pageSize ); // Wait for the total count. vm.TotalResults = await countItemsTask; // Set previous and next hyperlinks. if (page > 1) { vm.PrevLink = $"/?page={page - 1}&pageSize={pageSize}&mode={mode}®ion={region}#leaderboard"; } if (vm.TotalResults > page * pageSize) { vm.NextLink = $"/?page={page + 1}&pageSize={pageSize}&mode={mode}®ion={region}#leaderboard"; } // Fetch the user profile for each score. // This creates a list that's parallel with the scores collection. var profiles = new List <Task <Profile> >(); foreach (var score in scores) { profiles.Add(_profileRespository.GetItemAsync(score.ProfileId)); } Task <Profile> .WaitAll(profiles.ToArray()); // Combine each score with its profile. vm.Scores = scores.Zip(profiles, (score, profile) => new ScoreProfile { Score = score, Profile = profile.Result }); return(View(vm)); } catch (Exception ex) { Console.Write(ex.Message); return(View(vm)); } }