Ejemplo n.º 1
0
        public ActionResult GetSimilar(int questionId)
        {
            var question = _applicationContext.Questions
                           .Include(q => q.Answer)
                           .FirstOrDefault(q => q.Id == questionId);

            if (question == null)
            {
                return(Content("Ошибка: неверный ID вопроса."));
            }

            var ids = _processorService.GetListOfSimilarQuestionIds(question.Text, question.ProjectId);

            var similarQuestions = _applicationContext.Questions
                                   .Include(q => q.Answer)
                                   .Where(q => ids.Contains(q.Id) && q.Id != questionId)
                                   .OrderByDescending(q => q.Answer != null)
                                   .ToList();

            var viewModel = new SimilarQuestionsViewModel
            {
                QuestionId       = questionId,
                SimilarQuestions = similarQuestions
            };

            return(PartialView("_SimilarQuestions", viewModel));
        }
Ejemplo n.º 2
0
        public JsonResult Get(string data, /*[FromBody]string signature, */ [FromQuery] string signature /*, [FromForm]string signature*/)
        {
            try
            {
                var decodedData       = CryptoUtils.Base64Decode(data);
                var getQuestionsModel = JsonConvert.DeserializeObject <GetQuestions>(decodedData);

                if (!getQuestionsModel.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 == getQuestionsModel.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 == getQuestionsModel.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 questionIds = _processorService.GetListOfSimilarQuestionIds(getQuestionsModel.Text, getQuestionsModel.ProjectId, getQuestionsModel.SimilarMaxCount);

                var questionList = _applicationContext.Questions
                                   .Where(q => questionIds.Contains(q.Id))
                                   .Select(q => q.Text)
                                   .ToList();

                var getQuestionResponse = new GetQuestionsResponse
                {
                    QuestionText  = getQuestionsModel.Text,
                    Questions     = questionList,
                    StatusCode    = Models.Api.StatusCode.Success,
                    StatusMessage = "List of similar questions."
                };

                return(Json(getQuestionResponse));
            }
            catch (ErrorWithDataException ex)
            {
                var answer = new PostQuestionResponse()
                {
                    StatusCode    = ex.StatusCode(),
                    StatusMessage = ex.Message
                };

                return(Json(answer));
            }
            catch (Exception ex)
            {
                var answer = new PostQuestionResponse()
                {
                    StatusCode    = Models.Api.StatusCode.Error,
                    StatusMessage = ex.Message
                };

                return(Json(answer));
            }
        }