Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to get classes info for authenticated user in the given tier and given topic.
/// </summary>
/// <param name="topicId">Topic ID.</param>
/// <returns>Topic list.</returns>
Endpoint used to get classes info for authenticated user in the given tier and given topic.
Endpoint used to get classes info for authenticated user in the given tier and given topic .
[HttpGet]
        [Route("topics/{topicId}")]
        public IActionResult GetClassesForUser(string topicId)
        {
            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 topic = _academaticaDbContext.Topics.Where(x => x.Id == topicId).FirstOrDefault();
            if (topic == null)
            {
                return NotFound("Invalid topic ID");
            }
            List<GetClassForUserResponseDto> classes = new List<GetClassForUserResponseDto>();
            var classesList = _academaticaDbContext.Classes.Where(x => x.TopicId == topicId).AsEnumerable().OrderBy(x => x.Id, new ClassIdComparer());
            for (int i = 0; i < classesList.Count(); ++i)
            {
                var currentClass = classesList.ElementAt(i);
                var classIsComplete = _academaticaDbContext.UserClasses.Where(x => x.UserId.ToString() == userId)
                    .Select(x => x.ClassId).Contains(currentClass.Id);
                var classIsUnlocked = false;
                if (i == 0 || classesList.ElementAt(i - 1).TopicId != currentClass.TopicId)
                {
                    classIsUnlocked = true;
                }
                else
                {
                    classIsUnlocked = _academaticaDbContext.UserClasses.Where(x => x.UserId.ToString() == userId)
                        .Select(x => x.ClassId).Contains(classesList.ElementAt(i - 1).Id);
                }
                classes.Add(new GetClassForUserResponseDto()
                {
                    Id = currentClass.Id,
                    Name = currentClass.Name,
                    Description = currentClass.Description,
                    IsComplete = classIsComplete,
                    ImageUrl = currentClass.ImageUrl,
                    ProblemNum = currentClass.ProblemNum,
                    TheoryUrl = currentClass.TheoryUrl,
                    TopicName = topic.Name,
                    IsUnlocked = classIsUnlocked
                });
            }
            return Ok(new GetClassesForUserResponseDto()
            {
                Classes = classes
            });
        }
[ HttpGet ] [ Route ( " " ) ] public IActionResult GetClassesForUser ( string topicId ) { 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 topic = _academaticaDbContext . Topics . Where ( x => x . Id == topicId ) . FirstOrDefault ( ) ; if ( topic == null ) { return NotFound ( " " ) ; } List < GetClassForUserResponseDto > classes = new List < GetClassForUserResponseDto > ( ) ; var classesList = _academaticaDbContext . Classes . Where ( x => x . TopicId == topicId ) . AsEnumerable ( ) . OrderBy ( x => x . Id , new ClassIdComparer ( ) ) ; for ( int i = 0 ; i < classesList . Count ( ) ; ++ i ) { var currentClass = classesList . ElementAt ( i ) ; var classIsComplete = _academaticaDbContext . UserClasses . Where ( x => x . UserId . ToString ( ) == userId ) . Select ( x => x . ClassId ) . Contains ( currentClass . Id ) ; var classIsUnlocked = false ; if ( i == 0 || classesList . ElementAt ( i - 1 ) . TopicId != currentClass . TopicId ) { classIsUnlocked = true ; } else { classIsUnlocked = _academaticaDbContext . UserClasses . Where ( x => x . UserId . ToString ( ) == userId ) . Select ( x => x . ClassId ) . Contains ( classesList . ElementAt ( i - 1 ) . Id ) ; } classes . Add ( new GetClassForUserResponseDto ( ) { Id = currentClass . Id , Name = currentClass . Name , Description = currentClass . Description , IsComplete = classIsComplete , ImageUrl = currentClass . ImageUrl , ProblemNum = currentClass . ProblemNum , TheoryUrl = currentClass . TheoryUrl , TopicName = topic . Name , IsUnlocked = classIsUnlocked } ) ; } return Ok ( new GetClassesForUserResponseDto ( ) { Classes = classes } ) ; }
Endpoint used to get classes info for authenticated user in the given tier and given topic.
Endpoint used to get classes info for authenticated user in the given tier and given topic .
None
topicId
string
None