Exemplo n.º 1
0
        public async Task <IActionResult> Answer(int projectId, int questionId, string answerText)
        {
            if (string.IsNullOrEmpty(answerText))
            {
                return(Json(new Result {
                    Message = "Текст ответа не может быть пустым."
                }));
            }

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

            if (project == null)
            {
                return(Json(new Result {
                    Message = "Parameter projectId"
                }));
            }

            var user = _repositoryService.GetUserByClaims(User);

            if (user == null)
            {
                return(Json(new Result {
                    Message = "Ошибка с идентификацией пользователя."
                }));
            }

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

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

            if (question == null)
            {
                return(Json(new Result {
                    Message = "Parameter questionId"
                }));
            }

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

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

            await _processorService.TrySendQuestionAnswerAsync(questionId);

            _processorService.AnswerOnSimilarQuestionsAsync(questionId);

            return(Json(new Result {
                Success = true
            }));
        }