Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to get tiers info for authenticated user.
/// </summary>
/// <returns>Tier list.</returns>
Endpoint used to get tiers info for authenticated user.
Endpoint used to get tiers info for authenticated user .
[HttpGet]
        [Route("tiers")]
        public IActionResult GetTiersForUser()
        {
            var userId = User.FindFirst("sub")?.Value;
            if (string.IsNullOrEmpty(userId))
            {
                return BadRequest("User ID was null.");
            }
            var user = _academaticaDbContext.Users.Where(x => x.Id.ToString() == userId).FirstOrDefault();
            if (user == null)
            {
                return NotFound("Invalid user ID.");
            }
            List<GetTierForUserResponseDto> tiers = new List<GetTierForUserResponseDto>();
            var tiersList = _academaticaDbContext.Tiers.AsEnumerable().OrderBy(x => x.Id, new TierIdComparer());
            for (int i = 0; i < tiersList.Count(); ++i)
            {
                var tier = tiersList.ElementAt(i);
                var tierTopicsIds = _academaticaDbContext.Topics.Where(x => x.TierId == tier.Id).Select(x => x.Id).ToList();
                var completedTierTopicsIds = _academaticaDbContext.UserTopic.Where(x => tierTopicsIds.Contains(x.TopicId) && x.UserId == user.Id).Select(x => x.TopicId).ToList();
                var tierIsComplete = _academaticaDbContext.UserTier.Where(x => x.UserId.ToString() == userId)
                    .Select(x => x.TierId).Contains(tier.Id);
                var tierIsUnlocked = false;
                if (i == 0)
                {
                    tierIsUnlocked = true;
                }
                else
                {
                    tierIsUnlocked = _academaticaDbContext.UserTier.Where(x => x.UserId.ToString() == userId)
                        .Select(x => x.TierId).Contains(tiersList.ElementAt(i - 1).Id);
                }
                tiers.Add(new GetTierForUserResponseDto()
                {
                    Id = tier.Id,
                    Name = tier.Name,
                    Description = tier.Description,
                    CompletionRate = (int)((double)completedTierTopicsIds.Count / tierTopicsIds.Count * 100),
                    IsComplete = tierIsComplete,
                    IsUnlocked = tierIsUnlocked
                });
            }
            return Ok(new GetTiersForUserResponseDto()
            {
                Tiers = tiers
            });
        }
[ HttpGet ] [ Route ( " " ) ] public IActionResult GetTiersForUser ( ) { var userId = User . FindFirst ( " " ) ? . Value ; if ( string . IsNullOrEmpty ( userId ) ) { return BadRequest ( " " ) ; } var user = _academaticaDbContext . Users . Where ( x => x . Id . ToString ( ) == userId ) . FirstOrDefault ( ) ; if ( user == null ) { return NotFound ( " " ) ; } List < GetTierForUserResponseDto > tiers = new List < GetTierForUserResponseDto > ( ) ; var tiersList = _academaticaDbContext . Tiers . AsEnumerable ( ) . OrderBy ( x => x . Id , new TierIdComparer ( ) ) ; for ( int i = 0 ; i < tiersList . Count ( ) ; ++ i ) { var tier = tiersList . ElementAt ( i ) ; var tierTopicsIds = _academaticaDbContext . Topics . Where ( x => x . TierId == tier . Id ) . Select ( x => x . Id ) . ToList ( ) ; var completedTierTopicsIds = _academaticaDbContext . UserTopic . Where ( x => tierTopicsIds . Contains ( x . TopicId ) && x . UserId == user . Id ) . Select ( x => x . TopicId ) . ToList ( ) ; var tierIsComplete = _academaticaDbContext . UserTier . Where ( x => x . UserId . ToString ( ) == userId ) . Select ( x => x . TierId ) . Contains ( tier . Id ) ; var tierIsUnlocked = false ; if ( i == 0 ) { tierIsUnlocked = true ; } else { tierIsUnlocked = _academaticaDbContext . UserTier . Where ( x => x . UserId . ToString ( ) == userId ) . Select ( x => x . TierId ) . Contains ( tiersList . ElementAt ( i - 1 ) . Id ) ; } tiers . Add ( new GetTierForUserResponseDto ( ) { Id = tier . Id , Name = tier . Name , Description = tier . Description , CompletionRate = ( int ) ( ( double ) completedTierTopicsIds . Count / tierTopicsIds . Count * 100 ) , IsComplete = tierIsComplete , IsUnlocked = tierIsUnlocked } ) ; } return Ok ( new GetTiersForUserResponseDto ( ) { Tiers = tiers } ) ; }
Endpoint used to get tiers info for authenticated user.
Endpoint used to get tiers info for authenticated user .
None