Exemplo n.º 1
0
        public async Task <QuizAnswerResponseDto> AnswerQuestion(long quizId, long personId, QuizQuestionAnswerDto questionAnswerDto)
        {
            var quiz = await _context.Quizzes.FirstOrDefaultAsync(q => q.Id == quizId);

            if (quiz == null)
            {
                throw new QuizNotFoundException(quizId);
            }

            var person = await _context.People.FirstOrDefaultAsync(p => p.Id == personId);

            if (person == null)
            {
                throw new PersonNotFoundException(quizId);
            }

            var allAnswers = await _context.QuizAnswers.ToListAsync();

            var answers = _quizManager.ValidateAnswers(quiz, allAnswers, questionAnswerDto.AnswerIds);

            _context.PersonAnswers.AddRange(answers.Select(a => new PersonAnswer()
            {
                AnswerId = a.Id, PersonId = person.Id
            }));

            await _context.SaveChangesAsync();

            var answerResponse = _quizManager.QuestionResponse(person, answers);

            return(new QuizAnswerResponseDto
            {
                PersonId = person.Id,
                AnswerResponseText = answerResponse,
            });
        }