Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoind used to get class information for given class ID and authenticated user.
/// </summary>
/// <param name="id">Class ID.</param>
/// <returns>Class information.</returns>
Endpoind used to get class information for given class ID and authenticated user.
Endpoind used to get class information for given class ID and authenticated user .
[HttpGet]
        [Route("classes/{id}")]
        public IActionResult GetClass(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 classFound = _academaticaDbContext.Classes.Where(x => x.Id == id).FirstOrDefault();
            if (classFound == null)
            {
                return NotFound($"No class with ID {id} was found.");
            }
            var userClassStats = _academaticaDbContext.UserClasses.Where(x => x.UserId == user.Id);
            var classIsComplete = userClassStats.Where(x => x.ClassId == classFound.Id).Any();
            var classFoundIdTokens = classFound.Id.Split("-");
            var classFoundTopicId = classFoundIdTokens[0] + "-" + classFoundIdTokens[1];
            var classTopic = _academaticaDbContext.Topics.Where(x => x.Id == classFoundTopicId).FirstOrDefault();
            var topicName = classTopic == null ? null : classTopic.Name;
            var classNum = int.Parse(classFoundIdTokens[2]);
            var sameTypeClasses = _academaticaDbContext.Classes
                .Where(x => x.IsAlgebraClass == classFound.IsAlgebraClass).ToList();
            var completedSameTypeClasses = sameTypeClasses.Join(_academaticaDbContext.UserClasses,
                c => c.Id,
                uc => uc.ClassId,
                (c, uc) => c).AsEnumerable();
            var isUnlocked = false;
            var classIndex = sameTypeClasses.IndexOf(classFound);
            if (classIndex == 0)
            {
                isUnlocked = true;
            } else
            {
                isUnlocked = completedSameTypeClasses.Contains(sameTypeClasses.ElementAt(classIndex - 1));
            }
            return Ok(new GetClassForUserResponseDto()
            {
                Id = classFound.Id,
                Name = classFound.Name,
                Description = classFound.Description,
                IsComplete = classIsComplete,
                ImageUrl = classFound.ImageUrl,
                ProblemNum = classFound.ProblemNum,
                TheoryUrl = classFound.TheoryUrl,
                TopicName = topicName,
                IsUnlocked = isUnlocked
            });
        }
[ HttpGet ] [ Route ( " " ) ] public IActionResult GetClass ( 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 classFound = _academaticaDbContext . Classes . Where ( x => x . Id == id ) . FirstOrDefault ( ) ; if ( classFound == null ) { return NotFound ( $" No class with ID  { id }  was found. " ) ; } var userClassStats = _academaticaDbContext . UserClasses . Where ( x => x . UserId == user . Id ) ; var classIsComplete = userClassStats . Where ( x => x . ClassId == classFound . Id ) . Any ( ) ; var classFoundIdTokens = classFound . Id . Split ( " " ) ; var classFoundTopicId = classFoundIdTokens [ 0 ] + " " + classFoundIdTokens [ 1 ] ; var classTopic = _academaticaDbContext . Topics . Where ( x => x . Id == classFoundTopicId ) . FirstOrDefault ( ) ; var topicName = classTopic == null ? null : classTopic . Name ; var classNum = int . Parse ( classFoundIdTokens [ 2 ] ) ; var sameTypeClasses = _academaticaDbContext . Classes . Where ( x => x . IsAlgebraClass == classFound . IsAlgebraClass ) . ToList ( ) ; var completedSameTypeClasses = sameTypeClasses . Join ( _academaticaDbContext . UserClasses , c => c . Id , uc => uc . ClassId , ( c , uc ) => c ) . AsEnumerable ( ) ; var isUnlocked = false ; var classIndex = sameTypeClasses . IndexOf ( classFound ) ; if ( classIndex == 0 ) { isUnlocked = true ; } else { isUnlocked = completedSameTypeClasses . Contains ( sameTypeClasses . ElementAt ( classIndex - 1 ) ) ; } return Ok ( new GetClassForUserResponseDto ( ) { Id = classFound . Id , Name = classFound . Name , Description = classFound . Description , IsComplete = classIsComplete , ImageUrl = classFound . ImageUrl , ProblemNum = classFound . ProblemNum , TheoryUrl = classFound . TheoryUrl , TopicName = topicName , IsUnlocked = isUnlocked } ) ; }
Endpoind used to get class information for given class ID and authenticated user.
Endpoind used to get class information for given class ID and authenticated user .
None
id
string
None