PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/RavenOverflow.Web/Controllers/HomeController.cs

https://github.com/PureKrome/RavenOverflow
C# | 292 lines | 236 code | 41 blank | 15 comment | 11 complexity | e24ca3af9136e7ecacd22e507d4d194f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web.Mvc;
  5. using CuttingEdge.Conditions;
  6. using Raven.Abstractions.Data;
  7. using Raven.Client;
  8. using Raven.Client.Linq;
  9. using RavenOverflow.Core.Entities;
  10. using RavenOverflow.Core.Extensions;
  11. using RavenOverflow.Core.Filters;
  12. using RavenOverflow.Web.Models.ViewModels;
  13. using RavenOverflow.Web.RavenDb.Indexes;
  14. using WorldDomination.Security;
  15. namespace RavenOverflow.Web.Controllers
  16. {
  17. public class HomeController : BaseController
  18. {
  19. private readonly ICustomFormsAuthentication _customFormsAuthentication;
  20. public HomeController(IDocumentSession documentSession,
  21. ICustomFormsAuthentication customCustomFormsAuthentication) : base(documentSession)
  22. {
  23. Condition.Requires(customCustomFormsAuthentication).IsNotNull();
  24. _customFormsAuthentication = customCustomFormsAuthentication;
  25. }
  26. [HttpGet]
  27. public ActionResult Authenticate()
  28. {
  29. var userData = new UserData
  30. {
  31. UserId = "users/1",
  32. DisplayName = "Leah Culver",
  33. PictureUri = "/Content/LeahCulverAvatar.png"
  34. };
  35. _customFormsAuthentication.SignIn(userData);
  36. return RedirectToAction("Index", "Home", new {area = ""});
  37. }
  38. [HttpGet]
  39. public ActionResult SignOut()
  40. {
  41. _customFormsAuthentication.SignOut();
  42. return RedirectToAction("Index", "Home", new {area = ""});
  43. }
  44. [HttpGet]
  45. public ActionResult Index(string displayName, string tag)
  46. {
  47. string header;
  48. // 1. All the questions, ordered by most recent.
  49. IQueryable<QuestionWithDisplayName> questionsQuery = QuestionQuery(tag, out header);
  50. // 2. Popular Tags for a time period.
  51. // StackOverflow calls it 'recent tags'.
  52. IQueryable<RecentPopularTags.ReduceResult> recentPopularTags = RecentPopularTagsQuery();
  53. // 3. Logged in user information.
  54. IQueryable<User> userQuery = UserQuery(displayName);
  55. var viewModel = new IndexViewModel(ClaimsUser)
  56. {
  57. Header = header,
  58. QuestionListViewModel = new QuestionListViewModel
  59. {
  60. Questions = questionsQuery.ToList()
  61. },
  62. RecentPopularTags = recentPopularTags.ToDictionary(x => x.Tag, x => x.Count),
  63. UserFavoriteTagListViewModel = new UserTagListViewModel
  64. {
  65. Header = "Favorite Tags",
  66. DivId1 = "interesting-tags",
  67. DivId2 = "interestingtags",
  68. Tags = userQuery == null
  69. ? null
  70. : (userQuery.SingleOrDefault() ??
  71. new User()).FavoriteTags
  72. },
  73. UserIgnoredTagList = new UserTagListViewModel
  74. {
  75. Header = "Ignored Tags",
  76. DivId1 = "ignored-tags",
  77. DivId2 = "ignoredtags",
  78. Tags = null
  79. }
  80. };
  81. return View(viewModel);
  82. }
  83. [HttpGet]
  84. public ActionResult BatchedIndex(string displayName, string tag)
  85. {
  86. string header;
  87. // 1. All the questions, ordered by most recent.
  88. Lazy<IEnumerable<QuestionWithDisplayName>> questionsQuery = QuestionQuery(tag, out header).Lazily();
  89. // 2. Popular Tags for a time period.
  90. // StackOverflow calls it 'recent tags'.
  91. Lazy<IEnumerable<RecentPopularTags.ReduceResult>> recentPopularTags = RecentPopularTagsQuery().Lazily();
  92. // 3. Log in user information.
  93. IQueryable<User> userQuery = UserQuery(displayName);
  94. Lazy<IEnumerable<User>> lazyUserQuery = (userQuery != null ? userQuery.Lazily() : null);
  95. var viewModel = new IndexViewModel(ClaimsUser)
  96. {
  97. Header = header,
  98. QuestionListViewModel = new QuestionListViewModel
  99. {
  100. Questions = questionsQuery.Value.ToList()
  101. },
  102. RecentPopularTags = recentPopularTags.Value.ToDictionary(x => x.Tag, x => x.Count),
  103. UserFavoriteTagListViewModel = new UserTagListViewModel
  104. {
  105. Header = "Favorite Tags",
  106. DivId1 = "interesting-tags",
  107. DivId2 = "interestingtags",
  108. Tags = lazyUserQuery == null
  109. ? null
  110. : (lazyUserQuery.Value.
  111. SingleOrDefault() ??
  112. new User()).FavoriteTags
  113. },
  114. UserIgnoredTagList = new UserTagListViewModel
  115. {
  116. Header = "Ignored Tags",
  117. DivId1 = "ignored-tags",
  118. DivId2 = "ignoredtags",
  119. Tags = null
  120. }
  121. };
  122. return View("Index", viewModel);
  123. }
  124. [HttpGet]
  125. public ActionResult AggressiveIndex(string displayName, string tag)
  126. {
  127. using (DocumentSession.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(1)))
  128. {
  129. string header;
  130. // 1. All the questions, ordered by most recent.
  131. Lazy<IEnumerable<QuestionWithDisplayName>> questionsQuery = QuestionQuery(tag, out header).Lazily();
  132. // 2. Popular Tags for a time period.
  133. // StackOverflow calls it 'recent tags'.
  134. Lazy<IEnumerable<RecentPopularTags.ReduceResult>> recentPopularTags = RecentPopularTagsQuery().Lazily();
  135. // 3. Log in user information.
  136. IQueryable<User> userQuery = UserQuery(displayName);
  137. Lazy<IEnumerable<User>> lazyUserQuery = (userQuery != null ? userQuery.Lazily() : null);
  138. var viewModel = new IndexViewModel(ClaimsUser)
  139. {
  140. Header = header,
  141. QuestionListViewModel = new QuestionListViewModel
  142. {
  143. Questions = questionsQuery.Value.ToList()
  144. },
  145. RecentPopularTags =
  146. recentPopularTags.Value.ToDictionary(x => x.Tag, x => x.Count),
  147. UserFavoriteTagListViewModel = new UserTagListViewModel
  148. {
  149. Header = "Favorite Tags",
  150. DivId1 = "interesting-tags",
  151. DivId2 = "interestingtags",
  152. Tags = lazyUserQuery == null
  153. ? null
  154. : (lazyUserQuery.Value.
  155. SingleOrDefault() ??
  156. new User()).FavoriteTags
  157. },
  158. UserIgnoredTagList = new UserTagListViewModel
  159. {
  160. Header = "Ignored Tags",
  161. DivId1 = "ignored-tags",
  162. DivId2 = "ignoredtags",
  163. Tags = null
  164. }
  165. };
  166. return View("Index", viewModel);
  167. }
  168. }
  169. public ActionResult Tag(string id)
  170. {
  171. RavenQueryStatistics stats;
  172. List<Question> questions = DocumentSession.Query<Question>()
  173. .Statistics(out stats)
  174. .OrderByCreatedByDescending()
  175. .Take(20)
  176. .Where(x => x.Tags.Any(tag => tag == id))
  177. .ToList();
  178. return Json(new
  179. {
  180. Questions = questions,
  181. stats.TotalResults
  182. }, JsonRequestBehavior.AllowGet);
  183. }
  184. public ActionResult Facets(string id)
  185. {
  186. var facets = DocumentSession.Query
  187. <RecentPopularTagsMapOnly.ReduceResult, RecentPopularTagsMapOnly>()
  188. .Where(x => x.LastSeen > DateTime.UtcNow.AddMonths(-1).ToUtcToday())
  189. .ToFacets("Raven/Facets/Tags");
  190. return Json(facets, JsonRequestBehavior.AllowGet);
  191. }
  192. public ActionResult Search(string term)
  193. {
  194. IRavenQueryable<RecentPopularTags.ReduceResult> query = DocumentSession
  195. .Query<RecentPopularTags.ReduceResult, RecentPopularTags>()
  196. .Where(x => x.Tag == term);
  197. // Does this tag exist?
  198. RecentPopularTags.ReduceResult tag = query.FirstOrDefault();
  199. var results = new List<string>();
  200. if (tag != null)
  201. {
  202. results.Add(tag.Tag);
  203. }
  204. else
  205. {
  206. // No exact match .. so lets use Suggest.
  207. SuggestionQueryResult suggestedTags = query.Suggest();
  208. if (suggestedTags.Suggestions.Length > 0)
  209. {
  210. results.AddRange(suggestedTags.Suggestions);
  211. }
  212. }
  213. return Json(results, JsonRequestBehavior.AllowGet);
  214. }
  215. public JsonResult IndexJson(string displayName, string tag)
  216. {
  217. string header;
  218. var questions = QuestionQuery(tag, out header).ToList();
  219. return Json(questions.Count <= 0 ? null : questions);
  220. }
  221. private IQueryable<QuestionWithDisplayName> QuestionQuery(string tag, out string header)
  222. {
  223. header = "Top Questions";
  224. IQueryable<Question> questionsQuery = DocumentSession.Query<Question, Questions_Search>()
  225. .OrderByCreatedByDescending()
  226. .Take(20);
  227. // Filter Questions by Tags?
  228. if (!string.IsNullOrEmpty(tag))
  229. {
  230. header = "Tagged Questions";
  231. questionsQuery = questionsQuery
  232. .WithAnyTag(tag);
  233. }
  234. return questionsQuery.As<QuestionWithDisplayName>();
  235. }
  236. private IQueryable<RecentPopularTags.ReduceResult> RecentPopularTagsQuery()
  237. {
  238. IQueryable<RecentPopularTags.ReduceResult> recentPopularTags =
  239. DocumentSession.Query<RecentPopularTags.ReduceResult, RecentPopularTags>()
  240. .WithinTheLastMonth(1)
  241. .OrderByCountDescending()
  242. .Take(20);
  243. return recentPopularTags;
  244. }
  245. private IQueryable<User> UserQuery(string displayName)
  246. {
  247. string name = displayName ?? User.Identity.Name;
  248. return !string.IsNullOrEmpty(name) ? DocumentSession.Query<User>().WithDisplayName(name) : null;
  249. }
  250. }
  251. }