コード例 #1
0
        public JsonResult Get(Guid code)
        {
            try
            {
                Question ques = Question.Fetch(code, null, CurrentUserId);

                ApiQuestion q = new ApiQuestion
                {
                    Code = ques.Code,
                    Question = ques.QuestionText,
                    CorrectAnswer = ques.CorrectAnswer,
                    WrongAnswer1 = ques.WrongAnswer1,
                    WrongAnswer2 = ques.WrongAnswer2,
                    WrongAnswer3 = ques.WrongAnswer3
                };

                return Json(new { Success = true, Data = q }, JsonRequestBehavior.AllowGet);

            }
            catch (Exception ex)
            {
                return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
コード例 #2
0
        public async Task <ActionResult <Logic.Objects.Question> > PostSurveyQuestion([FromBody, Bind("questionText, isSurveyOptionList")] ApiQuestion question)
        {
            try
            {
                Logic.Objects.Question newQuestion = new Logic.Objects.Question()
                {
                    questionID         = Guid.NewGuid(),
                    questionText       = question.questionText,
                    isSurveyOptionList = question.isSurveyOptionList
                };

                try
                {
                    await repository.CreateSurveyQuestionAsync(newQuestion);

                    await repository.SaveAsync();

                    return(Ok(await repository.GetSurveyQuestionByIDAsync(newQuestion.questionID)));
                }
                catch (Exception e)
                {
                    return(BadRequest(e.Message));
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
コード例 #3
0
        public async Task <IActionResult> GetQuestion()
        {
            //loads data from SelectQuestion, and retreives specified question from Trivia API
            var loadDifficulty  = TempData["difficulty"];
            var loadCategory    = TempData["category"];
            var loadAPIorCustom = TempData["apiOrCustom"];

            //defaults to calling api questions if tempdata is lost
            if (loadAPIorCustom == null)
            {
                loadAPIorCustom = "api";
            }
            if (loadAPIorCustom.ToString() == "custom")
            {
                string aspId     = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                var    questions = _context.Question.Where(x => x.QuestionStr1 == aspId).ToList();

                //if (questions == null)
                //{ return RedirectToAction("Chore", "ErrorPage") }

                //1) find a way to randomize the question pulled from the Db
                //int indexOffset = 1;
                Random random = new Random();
                if (questions.Count <= 0)
                {
                    return(View("ErrorPage"));
                }
                else
                {
                    int myRandom = random.Next(0, questions.Count);
                    ViewModelQuestions getQuestion = new ViewModelQuestions(questions[myRandom]);
                    return(View(getQuestion));
                }
            }

            //need a way to ask "is this an API question, or a custom question"
            else
            {
                ApiQuestion question = await GetAPIQuestion(loadDifficulty, loadCategory);

                //mixes up answers and assigns them to the all_answers property (see ApiQuestion class)
                question.results[0].ScrambleAnswers(question.results[0].correct_answer, question.results[0].incorrect_answers);

                //determine point value based on question difficulty
                if (question.results[0].difficulty == "easy")
                {
                    question.results[0].point_value = 3;
                }
                else if (question.results[0].difficulty == "medium")
                {
                    question.results[0].point_value = 5;
                }
                else
                {
                    question.results[0].point_value = 8;
                }
                ViewModelQuestions getQuestion = new ViewModelQuestions();
                getQuestion.ApiQuestion = question;

                return(View(getQuestion));
            }
        }