Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoind used to get recommended practice topic for the authenticated user.
/// </summary>
/// <returns>Recommended practice topic.</returns>
Endpoind used to get recommended practice topic for the authenticated user.
Endpoind used to get recommended practice topic for the authenticated user .
[HttpGet]
        [Route("practice/recommended")]
        public IActionResult GetRecommendedPracticeTopic()
        {
            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 userMistakes = _academaticaDbContext.UserTopicMistakes.Where(x => x.UserId.ToString() == userId).AsEnumerable().OrderByDescending(x => x.MistakeCount);
            if (userMistakes.Count() == 0)
            {
                var userCompletedTopics = _academaticaDbContext.UserTopic.Where(x => x.UserId.ToString() == userId).AsEnumerable();
                if (userCompletedTopics.Count() == 0)
                {
                    return Ok(new GetRecommendedPracticeTopicResponseDto()
                    {
                        RecommendedTopic = null
                    });
                }
                var randomTopicId = userCompletedTopics.OrderBy(x => Guid.NewGuid()).Select(x => x.TopicId).Take(1).SingleOrDefault();
                var topic = _academaticaDbContext.Topics.Where(x => x.Id == randomTopicId).FirstOrDefault();
                if (topic == null)
                {
                    return Ok(new GetRecommendedPracticeTopicResponseDto()
                    {
                        RecommendedTopic = null
                    });
                }
                return Ok(new GetRecommendedPracticeTopicResponseDto()
                {
                    RecommendedTopic = topic == null ? null : new TopicDto()
                    {
                        Description = topic.Description,
                        Id = topic.Id,
                        ImageUrl = topic.ImageUrl,
                        IsAlgebraTopic = topic.IsAlgebraTopic,
                        Name = topic.Name
                    }
                });
            }
            var topicId = userMistakes.Select(x => x.TopicId).First();
            var recommendedTopic = _academaticaDbContext.Topics.Where(x => x.Id == topicId).FirstOrDefault();
            if (recommendedTopic == null)
            {
                return Ok(new GetRecommendedPracticeTopicResponseDto()
                {
                    RecommendedTopic = null
                });
            }
            return Ok(new GetRecommendedPracticeTopicResponseDto()
            {
                RecommendedTopic = recommendedTopic == null ? null : new TopicDto()
                {
                    Description = recommendedTopic.Description,
                    Id = recommendedTopic.Id,
                    ImageUrl = recommendedTopic.ImageUrl,
                    IsAlgebraTopic = recommendedTopic.IsAlgebraTopic,
                    Name = recommendedTopic.Name
                }
            });
        }
[ HttpGet ] [ Route ( " " ) ] public IActionResult GetRecommendedPracticeTopic ( ) { 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 userMistakes = _academaticaDbContext . UserTopicMistakes . Where ( x => x . UserId . ToString ( ) == userId ) . AsEnumerable ( ) . OrderByDescending ( x => x . MistakeCount ) ; if ( userMistakes . Count ( ) == 0 ) { var userCompletedTopics = _academaticaDbContext . UserTopic . Where ( x => x . UserId . ToString ( ) == userId ) . AsEnumerable ( ) ; if ( userCompletedTopics . Count ( ) == 0 ) { return Ok ( new GetRecommendedPracticeTopicResponseDto ( ) { RecommendedTopic = null } ) ; } var randomTopicId = userCompletedTopics . OrderBy ( x => Guid . NewGuid ( ) ) . Select ( x => x . TopicId ) . Take ( 1 ) . SingleOrDefault ( ) ; var topic = _academaticaDbContext . Topics . Where ( x => x . Id == randomTopicId ) . FirstOrDefault ( ) ; if ( topic == null ) { return Ok ( new GetRecommendedPracticeTopicResponseDto ( ) { RecommendedTopic = null } ) ; } return Ok ( new GetRecommendedPracticeTopicResponseDto ( ) { RecommendedTopic = topic == null ? null : new TopicDto ( ) { Description = topic . Description , Id = topic . Id , ImageUrl = topic . ImageUrl , IsAlgebraTopic = topic . IsAlgebraTopic , Name = topic . Name } } ) ; } var topicId = userMistakes . Select ( x => x . TopicId ) . First ( ) ; var recommendedTopic = _academaticaDbContext . Topics . Where ( x => x . Id == topicId ) . FirstOrDefault ( ) ; if ( recommendedTopic == null ) { return Ok ( new GetRecommendedPracticeTopicResponseDto ( ) { RecommendedTopic = null } ) ; } return Ok ( new GetRecommendedPracticeTopicResponseDto ( ) { RecommendedTopic = recommendedTopic == null ? null : new TopicDto ( ) { Description = recommendedTopic . Description , Id = recommendedTopic . Id , ImageUrl = recommendedTopic . ImageUrl , IsAlgebraTopic = recommendedTopic . IsAlgebraTopic , Name = recommendedTopic . Name } } ) ; }
Endpoind used to get recommended practice topic for the authenticated user.
Endpoind used to get recommended practice topic for the authenticated user .
Recommended practice topic.
Recommended practice topic .