Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to get problems for a topic practice.
/// </summary>
/// <returns>Problems list.</returns>
Endpoint used to get problems for a topic practice.
Endpoint used to get problems for a topic practice .
[HttpGet]
        [Route("practice/topic/problems")]
        public IActionResult GetTopicPracticeProblems([FromQuery] 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.");
            }
            var randomProblems = _academaticaDbContext.Problems.Where(x => x.TopicId == topicId).AsEnumerable().OrderBy(x => Guid.NewGuid()).Take(10);
            return Ok(new GetTopicPracticeProblemsResponseDto()
            {
                Problems = randomProblems.Select(x => new ProblemDto()
                {
                    Id = x.Id,
                    ClassId = x.ClassId,
                    TopicId = x.TopicId,
                    CorrectAnswers = x.CorrectAnswers,
                    Description = x.Description,
                    Difficulty = x.Difficulty,
                    Expression = x.Expression,
                    ImageUrl = x.ImageUrl,
                    Options = x.Options,
                    ProblemType = x.ProblemType,
                    Task = x.Task
                })
            });
        }
[ HttpGet ] [ Route ( " " ) ] public IActionResult GetTopicPracticeProblems ( [ FromQuery ] 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 ( " " ) ; } var randomProblems = _academaticaDbContext . Problems . Where ( x => x . TopicId == topicId ) . AsEnumerable ( ) . OrderBy ( x => Guid . NewGuid ( ) ) . Take ( 10 ) ; return Ok ( new GetTopicPracticeProblemsResponseDto ( ) { Problems = randomProblems . Select ( x => new ProblemDto ( ) { Id = x . Id , ClassId = x . ClassId , TopicId = x . TopicId , CorrectAnswers = x . CorrectAnswers , Description = x . Description , Difficulty = x . Difficulty , Expression = x . Expression , ImageUrl = x . ImageUrl , Options = x . Options , ProblemType = x . ProblemType , Task = x . Task } ) } ) ; }
Endpoint used to get problems for a topic practice.
Endpoint used to get problems for a topic practice .
None
string
[FromQuery]