Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to get upcoming lessons for the authenticated user.
/// </summary>
/// <returns>Upcoming classes.</returns>
Endpoint used to get upcoming lessons for the authenticated user.
Endpoint used to get upcoming lessons for the authenticated user .
[HttpGet]
        [Route("classes/upcoming")]
        public IActionResult GetUpcomingLessons()
        {
            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 algebraClasses = _academaticaDbContext.Classes
                .Where(x => x.IsAlgebraClass)
                .ToList()
                .OrderBy(x => x.Id, new ClassIdComparer());
            var geometryClasses = _academaticaDbContext.Classes
                .Where(x => !x.IsAlgebraClass)
                .ToList()
                .OrderBy(x => x.Id, new ClassIdComparer());
            List<UpcomingClassDto> upcomingClasses = new List<UpcomingClassDto>();
            foreach (var alClass in algebraClasses)
            {
                var isFinished = _academaticaDbContext.UserClasses
                    .Where(x => x.UserId.ToString() == userId && x.ClassId == alClass.Id)
                    .FirstOrDefault() != null;
                if (!isFinished)
                {
                    Topic classTopic = _academaticaDbContext.Topics.Where(x => x.Id == alClass.TopicId).FirstOrDefault();
                    if (classTopic == null)
                    {
                        continue;
                    }
                    int topicClassCount = _academaticaDbContext.Classes.Where(x => x.TopicId == classTopic.Id).Count();
                    string topicName = classTopic.Name;
                    int classNumber = int.Parse(alClass.Id.Split('-').LastOrDefault());
                    upcomingClasses.Add(new UpcomingClassDto() { 
                        Id = alClass.Id,
                        TopicId = alClass.TopicId,
                        TierId = alClass.TierId,
                        Name = alClass.Name,
                        Description = alClass.Description,
                        ExpReward = alClass.ExpReward,
                        ImageUrl = alClass.ImageUrl,
                        ClassNumber = classNumber,
                        IsAlgebraClass = alClass.IsAlgebraClass,
                        ProblemNum = alClass.ProblemNum,
                        TheoryUrl = alClass.TheoryUrl,
                        TopicClassCount = topicClassCount,
                        TopicName = topicName
                    });
                    break;
                }
            }
            foreach (var geomClass in geometryClasses)
            {
                var isFinished = _academaticaDbContext.UserClasses
                    .Where(x => x.UserId.ToString() == userId && x.ClassId == geomClass.Id)
                    .FirstOrDefault() != null;
                if (!isFinished)
                {
                    Topic classTopic = _academaticaDbContext.Topics.Where(x => x.Id == geomClass.TopicId).FirstOrDefault();
                    if (classTopic == null)
                    {
                        continue;
                    }
                    int topicClassCount = _academaticaDbContext.Classes.Where(x => x.TopicId == classTopic.Id).Count();
                    string topicName = classTopic.Name;
                    int classNumber = int.Parse(geomClass.Id.Split('-').LastOrDefault());
                    upcomingClasses.Add(new UpcomingClassDto()
                    {
                        Id = geomClass.Id,
                        TopicId = geomClass.TopicId,
                        TierId = geomClass.TierId,
                        Name = geomClass.Name,
                        Description = geomClass.Description,
                        ExpReward = geomClass.ExpReward,
                        ImageUrl = geomClass.ImageUrl,
                        ClassNumber = classNumber,
                        IsAlgebraClass = geomClass.IsAlgebraClass,
                        ProblemNum = geomClass.ProblemNum,
                        TheoryUrl = geomClass.TheoryUrl,
                        TopicClassCount = topicClassCount,
                        TopicName = topicName
                    });
                    break;
                }
            }
            return Ok(new GetUpcomingClassesResponseDto
            {
                UpcomingClasses = upcomingClasses
            });
        }
[ HttpGet ] [ Route ( " " ) ] public IActionResult GetUpcomingLessons ( ) { 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 algebraClasses = _academaticaDbContext . Classes . Where ( x => x . IsAlgebraClass ) . ToList ( ) . OrderBy ( x => x . Id , new ClassIdComparer ( ) ) ; var geometryClasses = _academaticaDbContext . Classes . Where ( x => ! x . IsAlgebraClass ) . ToList ( ) . OrderBy ( x => x . Id , new ClassIdComparer ( ) ) ; List < UpcomingClassDto > upcomingClasses = new List < UpcomingClassDto > ( ) ; foreach ( var alClass in algebraClasses ) { var isFinished = _academaticaDbContext . UserClasses . Where ( x => x . UserId . ToString ( ) == userId && x . ClassId == alClass . Id ) . FirstOrDefault ( ) != null ; if ( ! isFinished ) { Topic classTopic = _academaticaDbContext . Topics . Where ( x => x . Id == alClass . TopicId ) . FirstOrDefault ( ) ; if ( classTopic == null ) { continue ; } int topicClassCount = _academaticaDbContext . Classes . Where ( x => x . TopicId == classTopic . Id ) . Count ( ) ; string topicName = classTopic . Name ; int classNumber = int . Parse ( alClass . Id . Split ( ' ' ) . LastOrDefault ( ) ) ; upcomingClasses . Add ( new UpcomingClassDto ( ) { Id = alClass . Id , TopicId = alClass . TopicId , TierId = alClass . TierId , Name = alClass . Name , Description = alClass . Description , ExpReward = alClass . ExpReward , ImageUrl = alClass . ImageUrl , ClassNumber = classNumber , IsAlgebraClass = alClass . IsAlgebraClass , ProblemNum = alClass . ProblemNum , TheoryUrl = alClass . TheoryUrl , TopicClassCount = topicClassCount , TopicName = topicName } ) ; break ; } } foreach ( var geomClass in geometryClasses ) { var isFinished = _academaticaDbContext . UserClasses . Where ( x => x . UserId . ToString ( ) == userId && x . ClassId == geomClass . Id ) . FirstOrDefault ( ) != null ; if ( ! isFinished ) { Topic classTopic = _academaticaDbContext . Topics . Where ( x => x . Id == geomClass . TopicId ) . FirstOrDefault ( ) ; if ( classTopic == null ) { continue ; } int topicClassCount = _academaticaDbContext . Classes . Where ( x => x . TopicId == classTopic . Id ) . Count ( ) ; string topicName = classTopic . Name ; int classNumber = int . Parse ( geomClass . Id . Split ( ' ' ) . LastOrDefault ( ) ) ; upcomingClasses . Add ( new UpcomingClassDto ( ) { Id = geomClass . Id , TopicId = geomClass . TopicId , TierId = geomClass . TierId , Name = geomClass . Name , Description = geomClass . Description , ExpReward = geomClass . ExpReward , ImageUrl = geomClass . ImageUrl , ClassNumber = classNumber , IsAlgebraClass = geomClass . IsAlgebraClass , ProblemNum = geomClass . ProblemNum , TheoryUrl = geomClass . TheoryUrl , TopicClassCount = topicClassCount , TopicName = topicName } ) ; break ; } } return Ok ( new GetUpcomingClassesResponseDto { UpcomingClasses = upcomingClasses } ) ; }
Endpoint used to get upcoming lessons for the authenticated user.
Endpoint used to get upcoming lessons for the authenticated user .
None