public ActionResult Index(string q, string client, string client_version, string debug) { ViewBag.Title = "Natural Date and Time"; if (!string.IsNullOrEmpty(q)) { q = HttpUtility.UrlDecode(q); q = q.Replace("_", " "); ViewBag.Title = q + " - Natural Date and Time"; var answerService = new AnswerService(); var answer = answerService.GetAnswer(q); var userAgent = String.Empty; if (Request.Headers["User-Agent"] != null) userAgent = Request.Headers["User-Agent"].ToString(); if (string.IsNullOrEmpty(client)) client = "web"; if (string.IsNullOrEmpty(client_version)) client_version = "2.0"; var dbContext = new NaturalDateTimeContext(); var questionLog = new QuestionLog(answer.Question, answer, DateTime.UtcNow, client, client_version, IsBot(userAgent)); dbContext.AddQuestionLog(questionLog); dbContext.SaveChanges(); return View("Index", new HomeViewModel(q, answer.AnswerText, answer.Note)); } return View(new HomeViewModel(!String.IsNullOrEmpty(debug))); }
public void AddToCache(QuestionLog questionLog) { Queue<QuestionLog> questionLogs; if (questionLog.IsBot) questionLogs = LatestBotQuestionLogs; else questionLogs = LatestUserQuestionLogs; questionLogs.Enqueue(questionLog); if (questionLogs.Count > NaturalDateTimeContext.MaxCacheEntries) questionLogs.Dequeue(); }
public ActionResult Question(string question, string client, string client_version, string debug) { if (string.IsNullOrEmpty(question)) throw new HttpException(400, "Invalid request. Question not specified."); if (string.IsNullOrEmpty(client)) throw new HttpException(400, "Invalid request. Client not specified."); if (string.IsNullOrEmpty(client_version)) throw new HttpException(400, "Invalid request. Client version not specified."); question = HttpUtility.UrlDecode(question); var userAgent = String.Empty; if (Request.Headers["User-Agent"] != null) userAgent = Request.Headers["User-Agent"].ToString(); var answerService = new AnswerService(); var answer = answerService.GetAnswer(question, !String.IsNullOrEmpty(debug)); var dbContext = new NaturalDateTimeContext(); var questionLog = new QuestionLog(answer.Question, answer, DateTime.UtcNow, client, client_version, IsBot(userAgent)); dbContext.AddQuestionLog(questionLog); dbContext.SaveChanges(); return Json(new AnswerModel(answer), JsonRequestBehavior.AllowGet); }
public void AddQuestionLog(QuestionLog questionLog) { this.QuestionLog.Add(questionLog); _questionLogCache.AddToCache(questionLog); }