示例#1
0
        public static void AddQuestionModel(QuestionCacheModel model)
        {
            if (_questionCache == null)
            {
                _questionCache = new Dictionary <long, QuestionCacheModel>();
            }

            if (!_questionCache.ContainsKey(model.QuestionId))
            {
                _questionCache.Add(model.QuestionId, model);
            }
        }
示例#2
0
        private async Task LoadAnswersFromStackoverflow(QuestionCacheModel cacheModel)
        {
            _client = new Client();
            var result = await _client.GetAnswersResultAsync(_questionId);

            foreach (var item in result.Items)
            {
                item.BodyMarkdown = HtmlParser(item.BodyMarkdown);
            }
            result.Items          = result.Items.OrderByDescending(x => x.IsAccepted).ToList();
            icAnswers.ItemsSource = result.Items;
            cacheModel.Answers    = result.Items;
            NaiveCache.AddQuestionModel(cacheModel);
        }
示例#3
0
        public ActionResult Solve(int selectedAnswerId, int testId, int question)
        {
            string currentUserId       = User.Identity.GetUserId();
            string currentTestCacheKey = currentUserId + testId;

            TestCacheModel currentTest = (TestCacheModel)this.HttpContext.Cache[currentTestCacheKey];

            if (currentTest == null)
            {
                return(RedirectToAction("Logoff", "Account"));
            }

            QuestionCacheModel currentQuestion = currentTest.Questions[question];

            if (currentQuestion.CorrectAnswerId == selectedAnswerId)
            {
                currentQuestion.Guessed = true;
            }
            else
            {
                currentQuestion.Guessed = false;
            }

            if (question == currentTest.Questions.Count - 1)
            {
                double result = CalculateTestResult(currentTest);
                SaveTestResult(currentTest, currentUserId, result);
                this.HttpContext.Cache.Remove(currentTestCacheKey);

                return(RedirectToAction("Index", "Tests"));
            }

            this.HttpContext.Cache.Insert(
                currentTestCacheKey,
                currentTest,
                null,
                DateTime.Now.AddDays(1),
                TimeSpan.Zero,
                CacheItemPriority.Default,
                null);

            currentTest.QuestionIndex = question + 1;
            return(RedirectToAction("Solve", new RouteValueDictionary(new { testId = testId, question = currentTest.QuestionIndex })));
        }
示例#4
0
        private async Task LoadQuestionFromStackoverflow()
        {
            _client = new Client();
            var result = await _client.GetQuestionResultAsync(_questionId);

            var model = result.Items.FirstOrDefault();

            txtTitle.Text    = HtmlParser(model.Title);
            txtBody.Text     = HtmlParser(model.BodyMarkdown);
            btnGotToSite.Tag = model.Link;

            var cacheModel = new QuestionCacheModel()
            {
                QuestionId = model.QuestionId,
                AddedOn    = DateTime.Now,
                Question   = model
            };

            LoadAnswersFromStackoverflow(cacheModel);
        }
示例#5
0
        public static QuestionCacheModel GetQuestionModel(long id)
        {
            if (_questionCache == null)
            {
                return(null);
            }

            var model = new QuestionCacheModel();

            if (_questionCache.TryGetValue(id, out model))
            {
                var timeDifference = (DateTime.Now - model.AddedOn).TotalMinutes;
                if (timeDifference >= 60)
                {
                    RemoveQuestionModel(id);
                    return(null);
                }
                return(model);
            }
            return(null);
        }