Esempio n. 1
0
        public async Task <IActionResult> SaveAnswers(Guid Id, int sequenceNo, int sectionNo, string pageId, string __redirectAction)
        {
            try
            {
                var updatePageResult = default(SetPageAnswersResponse);

                string __summaryLink = HttpContext.Request.Form["__summaryLink"];
                var    page          = await _qnaApiClient.GetPageBySectionNo(Id, sequenceNo, sectionNo, pageId);

                var isFileUploadPage = page.Questions?.Any(q => q.Input.Type == "FileUpload");

                var answers = new List <Answer>();

                // NOTE: QnA API stipulates that a Page cannot contain a mixture of FileUploads and other Question Types
                if (isFileUploadPage == false)
                {
                    answers = GetAnswersFromForm(page);

                    if (__redirectAction == "Feedback" && !HasAtLeastOneAnswerChanged(page, answers) && !page.AllFeedbackIsCompleted)
                    {
                        SetAnswerNotUpdated(page);
                    }
                    else
                    {
                        updatePageResult = await _qnaApiClient.SetPageAnswers(Id, page.SectionId.Value, page.PageId, answers);
                    }
                }
                else if (isFileUploadPage == true)
                {
                    var errorMessages = new List <ValidationErrorDetail>();

                    answers = GetAnswersFromFiles();

                    if (answers.Count < 1)
                    {
                        // Nothing to upload
                        if (__redirectAction == "Feedback")
                        {
                            if (page.HasFeedback && page.HasNewFeedback && !page.AllFeedbackIsCompleted)
                            {
                                SetAnswerNotUpdated(page);
                                return(RedirectToAction("Page", new { Id, sequenceNo, sectionNo, pageId, __redirectAction, __summaryLink }));
                            }
                            else if (FileValidator.AllRequiredFilesArePresent(page, errorMessages, ModelState))
                            {
                                return(RedirectToAction("Feedback", new { Id }));
                            }
                        }
                        else if (FileValidator.AllRequiredFilesArePresent(page, errorMessages, ModelState))
                        {
                            return(ForwardToNextSectionOrPage(page, Id, sequenceNo, sectionNo, __redirectAction));
                        }
                    }
                    else
                    {
                        if (FileValidator.FileValidationPassed(answers, page, errorMessages, ModelState, HttpContext.Request.Form.Files))
                        {
                            updatePageResult = await UploadFilesToStorage(Id, page.SectionId.Value, page.PageId, page);
                        }
                    }
                }

                if (updatePageResult?.ValidationPassed == true)
                {
                    if (__redirectAction == "Feedback")
                    {
                        foreach (var answer in answers)
                        {
                            if (page.Next.Exists(y => y.Conditions.Exists(x => x.QuestionId == answer.QuestionId || x.QuestionTag == answer.QuestionId)))
                            {
                                return(RedirectToNextAction(Id, sequenceNo, sectionNo, __redirectAction, updatePageResult.NextAction, updatePageResult.NextActionId, "Hide"));
                            }
                            break;
                        }

                        return(RedirectToAction("Feedback", new { Id }));
                    }

                    if (!string.IsNullOrEmpty(updatePageResult.NextAction))
                    {
                        return(RedirectToNextAction(Id, sequenceNo, sectionNo, __redirectAction, updatePageResult.NextAction, updatePageResult.NextActionId));
                    }
                }

                if (isFileUploadPage != true)
                {
                    page = StoreEnteredAnswers(answers, page);
                }

                SetResponseValidationErrors(updatePageResult?.ValidationErrors, page);

                return(RedirectToAction("Page", new { Id, sequenceNo, sectionNo, pageId, __redirectAction, __summaryLink }));
            }
            catch (Exception ex)
            {
                if (ex.Message.Equals("Could not find the page", StringComparison.OrdinalIgnoreCase))
                {
                    return(RedirectToAction("Applications"));
                }

                _logger.LogError(ex, ex.Message);

                throw ex;
            }
        }