예제 #1
0
        public async Task <IActionResult> CreateQuestion(int projectId, string questionText)// TODO: , ICollection<string> tags)
        {
            if (string.IsNullOrEmpty(questionText))
            {
                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 questionTags = new List <QuestionTag>();

            var question = new Question
            {
                Text         = questionText,
                Date         = DateTime.Now,
                Project      = project,
                QuestionTags = questionTags,
                StatusUrl    = Url.Action("TestStatusUrl", "Home", null, Request.Scheme) // TODO: test purpose
            };

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

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

            return(Json(new Result {
                Success = true
            }));
        }
예제 #2
0
        public async Task <JsonResult> Post([FromForm] string data, [FromForm] string signature)
        {
            try
            {
                //string answerJson;
                var decodedData   = CryptoUtils.Base64Decode(data);
                var questionToApi = JsonConvert.DeserializeObject <PostQuestion>(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,
                    AnswerToEmail = questionToApi.AnswerToEmail,
                    StatusUrl     = questionToApi.StatusUrl
                };

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

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

                var postQuestionResponse = new PostQuestionResponse
                {
                    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(postQuestionResponse));
            }
            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));
            }
        }