Esempio n. 1
0
        public IActionResult GetQuestionsAswer([FromBody] QuestionAswerRequest questionAswerRequest)
        {
            Logger.Debug("Request: {0}", Framework.Common.SerializeJson.ToObject(questionAswerRequest));
            DateTime dateRequest   = DateTime.Now;
            var      response      = new IResponse <QuestionAswerResponse>();
            string   correlationId = string.Empty;

            try
            {
                #region Authorization Usuario y Contraseña
                if (string.IsNullOrEmpty(Request.Headers["Authorization"]))
                {
                    var validate = Models.Response.Error(null, "NotAuthenticated");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }

                AuthenticationHeaderValue authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                //var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter)).Split(':');
                correlationId = Request.Headers["Correlation-Id"].ToString();

                Core.Entity.User user = new Core.Entity.User()
                {
                    Public   = questionAswerRequest.PublicToken,
                    UserName = questionAswerRequest.UserAplication,
                    Password = questionAswerRequest.PasswordAplication
                };
                var userAuthenticate = _user.Authenticate(user);
                if (userAuthenticate.Data == null)
                {
                    var validate = Models.Response.Error("NotAuthenticated");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }
                Core.Entity.UserPolicy userPolicy = new Core.Entity.UserPolicy()
                {
                    AppUserId = questionAswerRequest.AppUserId,
                    IdUser    = ((Core.Entity.User)userAuthenticate.Data).Id
                };
                Core.Entity.Policy policy = new Core.Entity.Policy()
                {
                    Name = Request.Path.Value
                };
                var userPolicyAuthorize = _userPolicy.Authorize(userPolicy, policy);
                if (userPolicyAuthorize.Data == null)
                {
                    var validate = Models.Response.Error("NotUnauthorized");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }
                #endregion

                var answerAndQuestion = _module.GetAnswerAndQuestions(questionAswerRequest.ModuleNumber);
                if (answerAndQuestion.Data == null)
                {
                    response.Data    = null;
                    response.Message = answerAndQuestion.Message;
                    response.State   = answerAndQuestion.State;
                    return(Unauthorized(response));
                }
                var answerAndQuestionResponse = (QuestionAswerResponse)answerAndQuestion.Data;
                foreach (QuestionAswerResponse.Question question in answerAndQuestionResponse.Questions)
                {
                    foreach (string answer in question.AnswerWithoutProcess.Split("@"))
                    {
                        var answerResponse = answer.Split(":");
                        question.Answers.Add(new QuestionAswerResponse.Question.Answer
                        {
                            IdAnswer     = Convert.ToInt64(answerResponse[0]),
                            DetailAnswer = answerResponse[1].ToString(),
                            State        = answerResponse[2].Equals("1")
                        });
                    }
                }
                response.Data    = answerAndQuestionResponse;
                response.Message = Models.Response.CommentMenssage("QuestionsAnswersSuccessful");
                response.State   = "000";
                return(Ok(response));
            }
            catch (Exception ex)
            {
                Logger.Error("Message: {0}; Exception: {1}", ex.Message, Framework.Common.SerializeJson.ToObject(ex));
                response.Data    = null;
                response.Message = "Error General";
                response.State   = "099";
                return(BadRequest(response));
            }
            finally
            {
                DateTime dateResponse = DateTime.Now;
                Core.Entity.ConsumptionHistory consumptionHistory = new Core.Entity.ConsumptionHistory
                {
                    ApiName       = Request.Path.Value,
                    Host          = Dns.GetHostName() + ":" + Request.Host.Port,
                    CorrelationId = correlationId,
                    AppUserId     = "token",
                    Request       = Framework.Common.SerializeJson.ToObject(questionAswerRequest),
                    DateRequest   = dateRequest,
                    Response      = Framework.Common.SerializeJson.ToObject(response),
                    DateResponse  = dateResponse,
                    CodeResponse  = response.State
                };
                _consumptionHistory.Insert(consumptionHistory);
                Logger.Debug("Request: {0} Response: {1}", questionAswerRequest, response);
            }
        }