Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to get random problems for given class ID.
/// </summary>
/// <param name="id">Class ID.</param>
/// <returns>Problem list.</returns>
Endpoint used to get random problems for given class ID.
Endpoint used to get random problems for given class ID .
[HttpGet]
        [Route("classes/{id}/problems")]
        public IActionResult GetProblemsForClass(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return BadRequest("Invalid class ID");
            }
            Regex classIdFormat = new Regex(@"^[0-9]+-[0-1]:[0-9]+-[0-9]+$");
            var idIsValid = classIdFormat.IsMatch(id);
            if (!idIsValid)
            {
                return BadRequest("Invalid class ID");
            }
            var classFound = _academaticaDbContext.Classes.Where(x => x.Id == id).FirstOrDefault();
            if (classFound == null)
            {
                return NotFound($"Class with ID {id} was not found.");
            }
            var problemList = _academaticaDbContext.Problems.Where(x => x.ClassId == id).AsEnumerable().OrderBy(x => Guid.NewGuid()).Take((int)classFound.ProblemNum);
            return Ok(new GetClassProblemsResponseDto()
            {
                Problems = problemList.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 GetProblemsForClass ( string id ) { if ( String . IsNullOrEmpty ( id ) ) { return BadRequest ( " " ) ; } Regex classIdFormat = new Regex ( @"^[0-9]+-[0-1]:[0-9]+-[0-9]+$" ) ; var idIsValid = classIdFormat . IsMatch ( id ) ; if ( ! idIsValid ) { return BadRequest ( " " ) ; } var classFound = _academaticaDbContext . Classes . Where ( x => x . Id == id ) . FirstOrDefault ( ) ; if ( classFound == null ) { return NotFound ( $" Class with ID  { id }  was not found. " ) ; } var problemList = _academaticaDbContext . Problems . Where ( x => x . ClassId == id ) . AsEnumerable ( ) . OrderBy ( x => Guid . NewGuid ( ) ) . Take ( ( int ) classFound . ProblemNum ) ; return Ok ( new GetClassProblemsResponseDto ( ) { Problems = problemList . 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 random problems for given class ID.
Endpoint used to get random problems for given class ID .
None
id
string
None