public async Task <ActionResult> GetQuestions(QuestionsFilter questionFilter)
        {
            if (!ModelState.IsValid)
            {
                var vm = new QuestionsFilterViewModel()
                {
                    Catagories = categoryManager
                };
                return(View("GetCatagories", vm));
            }
            else
            {
                TriviaQuestionManagerDTO tmpQuestionManagerDTO =
                    new TriviaQuestionManagerDTO();

                string apiURL =
                    $"https://opentdb.com/api.php?amount={questionFilter.NumOfQuestions}&category={questionFilter.CategoryId}&difficulty={questionFilter.Difficulty}&type={questionFilter.Type}";

                var jsonData = await apiJsonCaller.GetJsonString(apiURL);

                tmpQuestionManagerDTO = JsonConvert.DeserializeObject <TriviaQuestionManagerDTO>(jsonData);
                triviaQuestionsviewModel.questionsManager = DataMapper.MapTriviaQuestionManager(tmpQuestionManagerDTO);

                return(View("GetQuestions", triviaQuestionsviewModel));
            }
        }
Beispiel #2
0
        public static TriviaQuestionManager MapTriviaQuestionManager(TriviaQuestionManagerDTO objDTO)
        {
            TriviaQuestionManager result = new TriviaQuestionManager();

            foreach (var questionDTO in objDTO.results)
            {
                var question = MapQuestionsFromDTO(questionDTO);
                result.Questions.Add(question);
            }
            return(result);
        }
        public async Task <ActionResult> GetQuestions(int?id)
        {
            string apiURL = "";
            TriviaQuestionManagerDTO tmpQuestionManagerDTO =
                new TriviaQuestionManagerDTO();

            if (!id.HasValue)
            {
                apiURL = "https://opentdb.com/api.php?amount=10&type=multiple";
            }
            else
            {
                apiURL = $"https://opentdb.com/api.php?amount=10&category= {id}";
            }

            if (triviaQuestionsviewModel.isSessionFinished || triviaQuestionsviewModel.currentQuestion == 0)
            {
                var jsonData = await apiJsonCaller.GetJsonString(apiURL);

                tmpQuestionManagerDTO = JsonConvert.DeserializeObject <TriviaQuestionManagerDTO>(jsonData);
                triviaQuestionsviewModel.questionsManager = DataMapper.MapTriviaQuestionManager(tmpQuestionManagerDTO);
            }
            return(View(triviaQuestionsviewModel));
        }