Ejemplo 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
            }));
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> Post([FromForm] string data, [FromForm] string signature)
        {
            try
            {
                //string answerJson;
                var decodedData = CryptoUtils.Base64Decode(data);
                var answerToApi = JsonConvert.DeserializeObject <PostAnswer>(decodedData);

                if (!answerToApi.IsModelValid())
                {
                    throw new ErrorWithDataException("Some of the data parameters are invalid. Check the documentation.",
                                                     Models.Api.StatusCode.WrongData);
                }

                var user = _applicationContext.User
                           .Include(u => u.UserProjects)
                           .ThenInclude(up => up.Project)
                           .FirstOrDefault(u => u.UserName == answerToApi.Login);

                if (user == null)// || user.PasswordHash != questionToApi.PasswordHash)
                {
                    throw new ErrorWithDataException("User with such email doesn't exist.",
                                                     Models.Api.StatusCode.WrongLoginPasswordCredentials);
                }

                var userProject = user.UserProjects.FirstOrDefault(up => up.Project.Id == answerToApi.ProjectId);

                if (userProject == null)
                {
                    throw new ErrorWithDataException("Provided user doesn't consist in the project with such ID.",
                                                     Models.Api.StatusCode.WrongProjectId);
                }

                if (!Utils.ValidateSignature(data, signature, userProject.Project.PrivateKey))
                {
                    throw new ErrorWithDataException("Signature is not valid. Check your PrivateKey and MD5 alghorithm.",
                                                     Models.Api.StatusCode.WrongSignature);
                }

                var question = _applicationContext.Questions
                               .Include(q => q.Answer)
                               .FirstOrDefault(q => q.Id == answerToApi.QuestionId);

                if (question == null)
                {
                    throw new ErrorWithDataException("Provided question ID doesn't consist in the project with such ID.",
                                                     Models.Api.StatusCode.WrongQuestionId);
                }

                var answer = new Answer
                {
                    Date      = DateTime.Now,
                    Text      = answerToApi.AnswerText,
                    ProjectId = answerToApi.ProjectId,
                    User      = user
                };

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

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

                // Запускаем обработку вопроса без ожидания результата
                _processorService.AnswerOnSimilarQuestionsAsync(question.Id);

                var postAnswerResponse = new PostAnswerResponse
                {
                    QuestionId    = question.Id,
                    StatusCode    = Models.Api.StatusCode.Success,
                    StatusMessage = $"Answer on question with ID {question.Id} was successfully placed."
                };

                return(Json(postAnswerResponse));
            }
            catch (ErrorWithDataException ex)
            {
                var answer = new PostQuestionResponse()
                {
                    StatusCode    = ex.StatusCode(),
                    StatusMessage = ex.Message
                };
                //var answerJson = JsonConvert.SerializeObject(answer);
                return(Json(answer));
            }
            catch (Exception ex)
            {
                var answer = new PostQuestionResponse()
                {
                    StatusCode    = Models.Api.StatusCode.Error,
                    StatusMessage = ex.Message
                };
                //var answerJson = JsonConvert.SerializeObject(answer);
                return(Json(answer));
            }
        }