public async Task <ActionResult> LoadQuestionDetails(int questionID) { StackOverflowDetailsVM result = await CallStackOverflow <StackOverflowDetailsVM>("questions", questionID.ToString(), "order=desc&sort=activity&site=stackoverflow&filter=!-*jbN-o8P3E5"); Random rng = new Random(); //Small data sets, not concerned with time complexity of this sort result.items.FirstOrDefault().answers = result.items.FirstOrDefault().answers.OrderBy(a => rng.Next()).ToList(); return(PartialView("_QuestionDetails", result.items.FirstOrDefault())); }
//Submitting the question detail model to this action to maintain the randomized answer sort order public async Task <ActionResult> SubmitGuess(StackOverflowQuestionVM vm) { GuessHandler guessHandler = new GuessHandler(); var answerID = vm.answers.FirstOrDefault(a => a.selected_answer == true).answer_id; GuessResult guessResult = guessHandler.SubmitGuess(answerID, vm.question_id, vm.title, vm.creation_date); StackOverflowDetailsVM result = await CallStackOverflow <StackOverflowDetailsVM>("questions", vm.question_id.ToString(), "order=desc&sort=activity&site=stackoverflow&filter=!-*jbN-o8P3E5"); var question = result.items.FirstOrDefault(); vm.body = question.body; foreach (var answer in vm.answers) { answer.body = question.answers.FirstOrDefault(a => a.answer_id == answer.answer_id).body; answer.creation_date = question.answers.FirstOrDefault(a => a.answer_id == answer.answer_id).creation_date; answer.selected_answer = answer.answer_id == answerID; answer.correct_answer = answer.answer_id == question.accepted_answer_id; answer.guess_count = guessResult.Answers.FirstOrDefault(a => a.AnswerID == answer.answer_id)?.GuessCount ?? 0; answer.guess_percentage = guessResult.Answers.FirstOrDefault(a => a.AnswerID == answer.answer_id)?.GuessPercentage ?? 0; } //Update score int total = 0, correct = 0; #pragma warning disable CS0168 // The variable 'grade' is declared but never used string grade; #pragma warning restore CS0168 // The variable 'grade' is declared but never used if (Int32.TryParse(HttpContext.Request.Cookies.Get("total")?.Value, out total)) { total++; Response.Cookies["total"].Value = total.ToString(); if (Int32.TryParse(HttpContext.Request.Cookies.Get("correct")?.Value, out correct)) { if (vm.answers.Where(a => a.selected_answer.Value && a.correct_answer.Value).Count() > 0) { correct++; Response.Cookies["correct"].Value = correct.ToString(); } } Response.Cookies["grade"].Value = GetLetterGrade(correct, total); } return(PartialView("_QuestionDetails", vm)); }