Ejemplo n.º 1
0
        public async Task <IActionResult> Answer(int projectId, int questionId, string answerText)
        {
            if (string.IsNullOrEmpty(answerText))
            {
                return(BadRequest());
            }

            var project = _applicationContext.Projects
                          .Include(p => p.Questions)
                          .FirstOrDefault(p => p.Id == projectId);

            if (project == null)
            {
                return(BadRequest());
            }

            var answer = new Answer
            {
                Date    = DateTime.Now,
                Text    = answerText,
                Project = project
            };

            var question = project.Questions.FirstOrDefault(q => q.Id == questionId);

            if (question == null)
            {
                return(BadRequest());
            }

            question.Answer          = answer;
            question.AnsweredByHuman = true;

            _applicationContext.Answers.Add(answer);
            await _applicationContext.SaveChangesAsync();

            _processorService.UpdateStatusForQuestion(questionId);

            return(Ok());
        }