Example #1
0
        /// <summary>
        /// Get the question's answers.
        /// </summary>
        /// <param name="id">The question id.</param>
        /// <returns>The list of question's answers.</returns>
        private async Task <List <DisplayAnswerModel> > GetQuestionAnswers(string id)
        {
            // Database related helpers
            LinkQuestionAnswerData linkQuestionAnswerData = new LinkQuestionAnswerData(_db);
            AnswerData             answerData             = new AnswerData(_db);

            // Get question's answers
            List <DataLinkQuestionAnswerModel> dataLinkQuestionAnswerModels = await linkQuestionAnswerData.GetLinkedAnswers(id);

            // Build the display object answers
            List <DisplayAnswerModel> displayAnswerModels = new List <DisplayAnswerModel>();

            foreach (var dataLinkQuestionAnswerModel in dataLinkQuestionAnswerModels)
            {
                DataAnswerModel dataAnswerModel = await answerData.GetAnswerById(dataLinkQuestionAnswerModel.AnswerId).ContinueWith((x) => { return(x.Result[0]); });

                displayAnswerModels.Add(new DisplayAnswerModel
                {
                    Answer       = dataAnswerModel.Answer,
                    Id           = dataAnswerModel.Id,
                    PlayerAnswer = dataAnswerModel.PlayerAnswer,
                    Type         = dataAnswerModel.Type,
                });
            }

            return(displayAnswerModels);
        }
Example #2
0
        /// <summary>
        /// Add a new answer to the question.
        /// </summary>
        /// <returns>Refresh the page.</returns>
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            LinkQuestionAnswerData      linkQuestionAnswerData     = new LinkQuestionAnswerData(_db);
            DataLinkQuestionAnswerModel newLinkQuestionAnswerModel = new DataLinkQuestionAnswerModel
            {
                AnswerId   = DisplayLink.AnswerId,
                Id         = DisplayLink.Id,
                QuestionId = DisplayLink.QuestionId,
            };

            // Getting the question type
            QuestionData questionData      = new QuestionData(_db);
            var          dataQuestionModel = await questionData.GetQuestionById(newLinkQuestionAnswerModel.QuestionId);

            QuestionTypeData questionTypeData = new QuestionTypeData(_db);
            var dataQuestionTypeModel         = await questionTypeData.GetQuestionTypeById(dataQuestionModel[0].QuestionTypeId);

            // Getting the question answers
            List <DisplayAnswerModel> questionAnswers = await GetQuestionAnswers(newLinkQuestionAnswerModel.QuestionId);

            // Getting the user answer
            AnswerData answerData      = new AnswerData(_db);
            var        dataAnswerModel = await answerData.GetAnswerById(newLinkQuestionAnswerModel.AnswerId);

            if (dataAnswerModel[0].Type == true)
            {
                // If the answer to add is correct and it already exists a correct response in simple and boolean types, we shouldn't add the answer
                if ((dataQuestionTypeModel[0].QuestionType == "Simple" || dataQuestionTypeModel[0].QuestionType == "Boolean") &&
                    questionAnswers.Any(x => x.Type == true))
                {
                    return(RedirectToPage("./linkquestiontoanswer", new { id = DisplayLink.QuestionId }));
                }
            }

            await linkQuestionAnswerData.InsertLink(newLinkQuestionAnswerModel);

            return(RedirectToPage("./linkquestiontoanswer", new { id = DisplayLink.QuestionId }));
        }