Academatica.Api/Academatica.Api.Leaderboards/Controllers/LeaderboardsController.cs
/// <summary>
/// Endpoint used to receive a specified page of the silver league leaderboard.
/// </summary>
/// <param name="page">Page number.</param>
/// <returns>Leaderboard segment.</returns>
Endpoint used to receive a specified page of the silver league leaderboard.
Endpoint used to receive a specified page of the silver league leaderboard .
[HttpGet]
        [HttpGet]
        [Route("silver")]
        public async Task<IActionResult> GetSilverLeagueList([FromQuery] int page)
        {
            if (page <= 0 || page > await _leaderboardService.GetSilverLeagueCount())
            {
                return BadRequest("Invalid page number.");
            }
            var leaderboardEntries = await _leaderboardService.GetSilverLeague(page);
            List<LeaderboardEntryDto> leaderboard = new List<LeaderboardEntryDto>();
            foreach (var entry in leaderboardEntries)
            {
                var user = _academaticaDbContext.Users.Where(x => x.Id.ToString() == entry.Element.ToString()).FirstOrDefault();
                if (user == null)
                {
                    continue;
                }
                var userStats = _academaticaDbContext.UserStats.Where(x => x.UserId.ToString() == entry.Element.ToString()).FirstOrDefault();
                if (userStats == null)
                {
                    continue;
                }
                LeaderboardEntryDto leaderboardEntryDto = new LeaderboardEntryDto()
                {
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    ExpThisWeek = Convert.ToUInt64(entry.Score),
                    Rank = await _leaderboardService.GetUserPosition(user.Id.ToString()) + 1,
                    Username = user.UserName,
                    ProfilePic = user.ProfilePicUrl,
                    UserId = user.Id
                };
                leaderboard.Add(leaderboardEntryDto);
            }
            return Ok(new GetLeaguePageResponseDto()
            {
                Leaderboard = leaderboard
            });
        }
[ HttpGet ] [ HttpGet ] [ Route ( " " ) ] public async Task < IActionResult > GetSilverLeagueList ( [ FromQuery ] int page ) { if ( page <= 0 || page > await _leaderboardService . GetSilverLeagueCount ( ) ) { return BadRequest ( " " ) ; } var leaderboardEntries = await _leaderboardService . GetSilverLeague ( page ) ; List < LeaderboardEntryDto > leaderboard = new List < LeaderboardEntryDto > ( ) ; foreach ( var entry in leaderboardEntries ) { var user = _academaticaDbContext . Users . Where ( x => x . Id . ToString ( ) == entry . Element . ToString ( ) ) . FirstOrDefault ( ) ; if ( user == null ) { continue ; } var userStats = _academaticaDbContext . UserStats . Where ( x => x . UserId . ToString ( ) == entry . Element . ToString ( ) ) . FirstOrDefault ( ) ; if ( userStats == null ) { continue ; } LeaderboardEntryDto leaderboardEntryDto = new LeaderboardEntryDto ( ) { FirstName = user . FirstName , LastName = user . LastName , ExpThisWeek = Convert . ToUInt64 ( entry . Score ) , Rank = await _leaderboardService . GetUserPosition ( user . Id . ToString ( ) ) + 1 , Username = user . UserName , ProfilePic = user . ProfilePicUrl , UserId = user . Id } ; leaderboard . Add ( leaderboardEntryDto ) ; } return Ok ( new GetLeaguePageResponseDto ( ) { Leaderboard = leaderboard } ) ; }
Endpoint used to receive a specified page of the silver league leaderboard.
Endpoint used to receive a specified page of the silver league leaderboard .
None
int
[FromQuery]
page
None