Exemple #1
0
        public async Task SubmitTestAsync(TestModel test)
        {
            await _testValidator.ValidateAsync(test);

            test.DateTimePassed = DateTime.Now;
            var score = 0;
            List <QuestionResultDto> results = new();

            foreach (var question in test.Questions)
            {
                var all = true;

                foreach (var answer in question.Answers)
                {
                    if (answer.IsCorrect.Equals((await _answerService.GetByIdAsync(answer.Id)).IsCorrect))
                    {
                        continue;
                    }
                    all = false;
                    break;
                }

                QuestionResultDto result = new()
                {
                    Id         = Guid.NewGuid().ToString(),
                    QuestionId = question.Id,
                    Result     = false
                };

                if (all)
                {
                    result.Result = true;
                    score++;
                }

                results.Add(result);
            }

            test.Score = score;

            AttemptDto attemptDto = new()
            {
                Id        = Guid.NewGuid().ToString(),
                StudentId = test.Student.Id,
                TopicId   = test.Topic.Id,
                Score     = test.Score,
                DateTime  = test.DateTimePassed
            };

            results.ForEach(qr => qr.AttemptId = attemptDto.Id);

            await _attemptService.CreateEntityAsync(attemptDto);

            results.ForEach(async qr => await _questionResultService.CreateEntityAsync(qr));
        }
    }
}
Exemple #2
0
        public async Task <ActionResult> Create([FromBody] SubjectDto subjectDto)
        {
            try
            {
                await _subjectService.CreateEntityAsync(subjectDto);
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }

            return(CreatedAtAction(nameof(Create), new { subjectDto.Id }, subjectDto));
        }
Exemple #3
0
        public async Task <ActionResult> Create([FromBody] QuestionDto questionDto)
        {
            try
            {
                await _questionService.CreateEntityAsync(questionDto);
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }

            return(CreatedAtAction(nameof(Create), new { questionDto.Id }, questionDto));
        }
Exemple #4
0
        public async Task <ActionResult> Create([FromBody] AttemptDto attemptDto)
        {
            try
            {
                await _attemptService.CreateEntityAsync(attemptDto);
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }

            return(CreatedAtAction(nameof(Create), new { attemptDto.Id }, attemptDto));
        }