Example #1
0
        public async Task <IActionResult> Question(int id)
        {
            // var question = await context.Questions.Include(q => q.PossibleAnswers)
            //     .FirstOrDefaultAsync(q => q.Id == id);
            var question = await questionRepository.ById(id);

            var userId = userManager.GetUserId(User);

            if (question == null)
            {
                return(RedirectToAction("Index"));
            }

            // var answer = await context.Answers
            //     .Include(a => a.Comment)
            //     .FirstOrDefaultAsync(a => a.QuestionId == id && a.UserId == userId);
            var answer = await answerRepository.ByQuestionAndUserId(id, userId);

            // var nextQuestionId = await NextQuestionId(question.QuestionnaireId, question.Id);
            var nextQuestionId = await questionnaireRepository.NextQuestionId(question.QuestionnaireId, id);

            var vm = new QuestionViewModel()
            {
                Question        = question.QuestionValue,
                Id              = question.Id,
                QuestionnaireId = question.QuestionnaireId,
                NextQuestionId  = nextQuestionId,
                PossibleAnswers = question.PossibleAnswers.Select(pa => new PossibleAnswerViewModel()
                {
                    Id               = pa.Id,
                    Answer           = pa.Answer,
                    IsSelectedAnswer = answer?.PossibleAnswerId == pa.Id
                }).ToList(),
                IsAnswered = answer != null,
                Comment    = answer?.Comment?.CommentValue
            };

            return(View(vm));
        }