Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to get problems for custom practice with given parameters.
/// </summary>
/// <param name="problemsRequestDto">Custom practice problem request parameters.</param>
/// <returns>Custom practice problem list.</returns>
Endpoint used to get problems for custom practice with given parameters.
Endpoint used to get problems for custom practice with given parameters .
[HttpPost]
        [Route("practice/custom/problems")]
        public IActionResult GetCustomPracticeProblems(GetCustomPracticeProblemsRequestDto problemsRequestDto)
        {
            if (ModelState.IsValid)
            {
                Regex topicIdFormat = new Regex(@"^[0-9]+-[0-1]:[0-9]+$");
                if (problemsRequestDto.TopicData.Count() == 0 || problemsRequestDto.TopicData == null)
                {
                    return BadRequest("Topic data container no entries");
                }
                var problems = new List<ProblemDto>();
                foreach (var entry in problemsRequestDto.TopicData)
                {
                    var idIsValid = topicIdFormat.IsMatch(entry.Key);
                    if (!idIsValid)
                    {
                        return BadRequest($"Invalid topic ID {entry.Key}.");
                    }
                    if (entry.Value == null)
                    {
                        return BadRequest($"Invalid topic ID {entry.Key} data.");
                    }
                    var foundTopic = _academaticaDbContext.Topics.Where(x => x.Id == entry.Key).FirstOrDefault();
                    if (foundTopic == null)
                    {
                        return BadRequest($"Invalid topic ID {entry.Key}.");
                    }
                    var topicProblems = _academaticaDbContext.Problems
                        .Where(x => x.TopicId == foundTopic.Id && x.Difficulty == entry.Value.Difficulty)
                        .AsEnumerable().OrderBy(x => Guid.NewGuid()).Take(entry.Value.Count);
                    problems.AddRange(topicProblems.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
                    }));
                }
                return Ok(new GetCustomPracticeProblemsResponseDto()
                {
                    Problems = problems
                });
            } else
            {
                var message = string.Join(" | ", ModelState.Values
                                    .SelectMany(v => v.Errors)
                                    .Select(e => e.ErrorMessage));
                return BadRequest(message);
            }
        }
[ HttpPost ] [ Route ( " " ) ] public IActionResult GetCustomPracticeProblems ( GetCustomPracticeProblemsRequestDto problemsRequestDto ) { if ( ModelState . IsValid ) { Regex topicIdFormat = new Regex ( @"^[0-9]+-[0-1]:[0-9]+$" ) ; if ( problemsRequestDto . TopicData . Count ( ) == 0 || problemsRequestDto . TopicData == null ) { return BadRequest ( " " ) ; } var problems = new List < ProblemDto > ( ) ; foreach ( var entry in problemsRequestDto . TopicData ) { var idIsValid = topicIdFormat . IsMatch ( entry . Key ) ; if ( ! idIsValid ) { return BadRequest ( $" Invalid topic ID  { entry . Key } . " ) ; } if ( entry . Value == null ) { return BadRequest ( $" Invalid topic ID  { entry . Key }  data. " ) ; } var foundTopic = _academaticaDbContext . Topics . Where ( x => x . Id == entry . Key ) . FirstOrDefault ( ) ; if ( foundTopic == null ) { return BadRequest ( $" Invalid topic ID  { entry . Key } . " ) ; } var topicProblems = _academaticaDbContext . Problems . Where ( x => x . TopicId == foundTopic . Id && x . Difficulty == entry . Value . Difficulty ) . AsEnumerable ( ) . OrderBy ( x => Guid . NewGuid ( ) ) . Take ( entry . Value . Count ) ; problems . AddRange ( topicProblems . 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 } ) ) ; } return Ok ( new GetCustomPracticeProblemsResponseDto ( ) { Problems = problems } ) ; } else { var message = string . Join ( " " , ModelState . Values . SelectMany ( v => v . Errors ) . Select ( e => e . ErrorMessage ) ) ; return BadRequest ( message ) ; } }
Endpoint used to get problems for custom practice with given parameters.
Endpoint used to get problems for custom practice with given parameters .
Custom practice problem list.
Custom practice problem list .
problemsRequestDto
GetCustomPracticeProblemsRequestDto
Custom practice problem request parameters.
Custom practice problem request parameters .