Ejemplo n.º 1
0
        public async Task <JsonResult> Post([FromForm] string data, [FromForm] string signature)
        {
            try
            {
                //string answerJson;
                var decodedData   = CryptoUtils.Base64Decode(data);
                var questionToApi = JsonConvert.DeserializeObject <QuestionToApi>(decodedData);

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

                if (!Utils.ValidUrl(questionToApi.StatusUrl))
                {
                    throw new ErrorWithDataException("Provided URL is not valid.",
                                                     Models.Api.StatusCode.WrongStatusUrl);
                }

                var user = _applicationContext.User
                           .Include(u => u.UserProjects)
                           .ThenInclude(up => up.Project)
                           .FirstOrDefault(u => u.UserName == questionToApi.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 == questionToApi.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 = new Question
                {
                    Date      = DateTime.Now,
                    Project   = userProject.Project,
                    Text      = questionToApi.Text,
                    StatusUrl = questionToApi.StatusUrl
                };

                _applicationContext.Questions.Add(question);
                await _applicationContext.SaveChangesAsync();

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

                var answer = new AnswerOnRequest
                {
                    QuestionId    = question.Id,
                    StatusCode    = Models.Api.StatusCode.Success,
                    StatusMessage = "Your question was successfully added. Wait for an answer to Status URL."
                };
                //answerJson = JsonConvert.SerializeObject(answer);
                return(Json(answer));
            }
            catch (ErrorWithDataException ex)
            {
                var answer = new AnswerOnRequest()
                {
                    StatusCode    = ex.StatusCode(),
                    StatusMessage = ex.Message
                };
                //var answerJson = JsonConvert.SerializeObject(answer);
                return(Json(answer));
            }
            catch (Exception ex)
            {
                var answer = new AnswerOnRequest()
                {
                    StatusCode    = Models.Api.StatusCode.Error,
                    StatusMessage = ex.Message
                };
                //var answerJson = JsonConvert.SerializeObject(answer);
                return(Json(answer));
            }
        }