Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to get topic info for authenticated user in the given tier.
/// </summary>
/// <param name="id">Tier ID.</param>
/// <returns>Topic list.</returns>
Endpoint used to get topic info for authenticated user in the given tier.
Endpoint used to get topic info for authenticated user in the given tier .
[HttpGet]
        [Route("tiers/{id}")]
        public IActionResult GetTopicsForUser(string id)
        {
            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.");
            }
            var tier = _academaticaDbContext.Tiers.Where(x => x.Id == id).FirstOrDefault();
            if (tier == null)
            {
                return NotFound("Invalid tier ID");
            }
            List<GetTopicForUserResponseDto> topics = new List<GetTopicForUserResponseDto>();
            var topicsList = _academaticaDbContext.Topics.Where(x => x.TierId == id).AsEnumerable().OrderBy(x => x.Id, new TopicIdComparer());
            var algebraTopicsIds = _academaticaDbContext.Topics.Where(x => x.IsAlgebraTopic).AsEnumerable().OrderBy(x => x.Id, new TopicIdComparer()).Select(x => x.Id).ToList();
            var geometryTopicsIds = _academaticaDbContext.Topics.Where(x => !x.IsAlgebraTopic).AsEnumerable().OrderBy(x => x.Id, new TopicIdComparer()).Select(x => x.Id).ToList();
            foreach (var entry in algebraTopicsIds)
            {
                Console.WriteLine(entry);
            }
            Console.WriteLine();
            foreach (var entry in geometryTopicsIds)
            {
                Console.WriteLine(entry);
            }
            for (int i = 0; i < topicsList.Count(); ++i)
            {
                var topicClassesIds = _academaticaDbContext.Classes.Where(x => x.TopicId == topicsList.ElementAt(i).Id).Select(x => x.Id).ToList();
                var completedTopicClassesIds = _academaticaDbContext.UserClasses.Where(x => topicClassesIds.Contains(x.ClassId) && x.UserId == user.Id)
                    .Select(x => x.ClassId).ToList();
                var topicIsComplete = _academaticaDbContext.UserTopic.Where(x => x.UserId.ToString() == userId)
                    .Select(x => x.TopicId).Contains(topicsList.ElementAt(i).Id);
                var topicIsUnlocked = false;
                var isAlgebraTopic = topicsList.ElementAt(i).IsAlgebraTopic;
                var elementId = 0;
                if (isAlgebraTopic)
                {
                    elementId = algebraTopicsIds.IndexOf(topicsList.ElementAt(i).Id);
                    if (elementId == 0)
                    {
                        topicIsUnlocked = true;
                    }
                    else
                    {
                        topicIsUnlocked = _academaticaDbContext.UserTopic.Where(x => x.UserId.ToString() == userId)
                            .Select(x => x.TopicId).Contains(algebraTopicsIds[elementId - 1]);
                    }
                }
                else
                {
                    elementId = geometryTopicsIds.IndexOf(topicsList.ElementAt(i).Id);
                    if (elementId == 0)
                    {
                        topicIsUnlocked = true;
                    }
                    else
                    {
                        topicIsUnlocked = _academaticaDbContext.UserTopic.Where(x => x.UserId.ToString() == userId)
                            .Select(x => x.TopicId).Contains(geometryTopicsIds[elementId - 1]);
                    }
                }
                topics.Add(new GetTopicForUserResponseDto()
                {
                    Id = topicsList.ElementAt(i).Id,
                    Name = topicsList.ElementAt(i).Name,
                    Description = topicsList.ElementAt(i).Description,
                    ImageUrl = topicsList.ElementAt(i).ImageUrl,
                    IsAlgebraTopic = topicsList.ElementAt(i).IsAlgebraTopic,
                    CompletionRate = topicClassesIds.Count == 0 ? 0 : (int)(((double)completedTopicClassesIds.Count) / topicClassesIds.Count * 100),
                    IsComplete = topicIsComplete,
                    IsUnlocked = topicIsUnlocked,
                    ClassCount = topicClassesIds.Count
                });
            }
            return Ok(new GetTopicsForUserResponseDto()
            {
                Topics = topics
            });
        }
[ HttpGet ] [ Route ( " " ) ] public IActionResult GetTopicsForUser ( string id ) { 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 ( " " ) ; } var tier = _academaticaDbContext . Tiers . Where ( x => x . Id == id ) . FirstOrDefault ( ) ; if ( tier == null ) { return NotFound ( " " ) ; } List < GetTopicForUserResponseDto > topics = new List < GetTopicForUserResponseDto > ( ) ; var topicsList = _academaticaDbContext . Topics . Where ( x => x . TierId == id ) . AsEnumerable ( ) . OrderBy ( x => x . Id , new TopicIdComparer ( ) ) ; var algebraTopicsIds = _academaticaDbContext . Topics . Where ( x => x . IsAlgebraTopic ) . AsEnumerable ( ) . OrderBy ( x => x . Id , new TopicIdComparer ( ) ) . Select ( x => x . Id ) . ToList ( ) ; var geometryTopicsIds = _academaticaDbContext . Topics . Where ( x => ! x . IsAlgebraTopic ) . AsEnumerable ( ) . OrderBy ( x => x . Id , new TopicIdComparer ( ) ) . Select ( x => x . Id ) . ToList ( ) ; foreach ( var entry in algebraTopicsIds ) { Console . WriteLine ( entry ) ; } Console . WriteLine ( ) ; foreach ( var entry in geometryTopicsIds ) { Console . WriteLine ( entry ) ; } for ( int i = 0 ; i < topicsList . Count ( ) ; ++ i ) { var topicClassesIds = _academaticaDbContext . Classes . Where ( x => x . TopicId == topicsList . ElementAt ( i ) . Id ) . Select ( x => x . Id ) . ToList ( ) ; var completedTopicClassesIds = _academaticaDbContext . UserClasses . Where ( x => topicClassesIds . Contains ( x . ClassId ) && x . UserId == user . Id ) . Select ( x => x . ClassId ) . ToList ( ) ; var topicIsComplete = _academaticaDbContext . UserTopic . Where ( x => x . UserId . ToString ( ) == userId ) . Select ( x => x . TopicId ) . Contains ( topicsList . ElementAt ( i ) . Id ) ; var topicIsUnlocked = false ; var isAlgebraTopic = topicsList . ElementAt ( i ) . IsAlgebraTopic ; var elementId = 0 ; if ( isAlgebraTopic ) { elementId = algebraTopicsIds . IndexOf ( topicsList . ElementAt ( i ) . Id ) ; if ( elementId == 0 ) { topicIsUnlocked = true ; } else { topicIsUnlocked = _academaticaDbContext . UserTopic . Where ( x => x . UserId . ToString ( ) == userId ) . Select ( x => x . TopicId ) . Contains ( algebraTopicsIds [ elementId - 1 ] ) ; } } else { elementId = geometryTopicsIds . IndexOf ( topicsList . ElementAt ( i ) . Id ) ; if ( elementId == 0 ) { topicIsUnlocked = true ; } else { topicIsUnlocked = _academaticaDbContext . UserTopic . Where ( x => x . UserId . ToString ( ) == userId ) . Select ( x => x . TopicId ) . Contains ( geometryTopicsIds [ elementId - 1 ] ) ; } } topics . Add ( new GetTopicForUserResponseDto ( ) { Id = topicsList . ElementAt ( i ) . Id , Name = topicsList . ElementAt ( i ) . Name , Description = topicsList . ElementAt ( i ) . Description , ImageUrl = topicsList . ElementAt ( i ) . ImageUrl , IsAlgebraTopic = topicsList . ElementAt ( i ) . IsAlgebraTopic , CompletionRate = topicClassesIds . Count == 0 ? 0 : ( int ) ( ( ( double ) completedTopicClassesIds . Count ) / topicClassesIds . Count * 100 ) , IsComplete = topicIsComplete , IsUnlocked = topicIsUnlocked , ClassCount = topicClassesIds . Count } ) ; } return Ok ( new GetTopicsForUserResponseDto ( ) { Topics = topics } ) ; }
Endpoint used to get topic info for authenticated user in the given tier.
Endpoint used to get topic info for authenticated user in the given tier .
None
id
string
None