public ActionResult SearchSub(string subName, string query, PostsSearchSortBy? sort, PostsTimeFilter? time, int? pageNumber, int? pageSize) { if (string.IsNullOrEmpty(subName)) return Redirect(Url.Subs()); if (sort == null) sort = PostsSearchSortBy.Relevance; if (time == null) time = PostsTimeFilter.All; var sub = _subDao.GetSubByName(subName); if (sub == null) return Redirect(Url.Subs(subName)); if (pageNumber == null || pageNumber < 1) pageNumber = 1; if (pageSize == null) pageSize = 25; if (pageSize > 100) pageSize = 100; if (pageSize < 1) pageSize = 1; var model = new SearchResultsModel(); model.Query = query; model.SortBy = sort.Value; model.TimeFilter = time.Value; model.ResultType = SearchResultType.Post; model.LimitingToSub = _subWrapper.Wrap(sub.Id, _userContext.CurrentUser); var postIds = _postDao.QueryPosts(query, model.LimitingToSub.Sub.Id, sortBy: sort.Value, timeFilter: time.Value, nsfw: _userContext.CurrentNsfw, skip: ((pageNumber - 1) * pageSize), take: pageSize); if (!string.IsNullOrEmpty(model.Query)) model.Posts = new PagedList<PostWrapped>( _postWrapper.Wrap(postIds, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, postIds.HasMore); return View("Search", model); }
public ActionResult SearchSite(string query, PostsSearchSortBy? sort, PostsTimeFilter? time, SearchResultType? resultType, int? pageNumber, int? pageSize) { if (sort == null) sort = PostsSearchSortBy.Relevance; if (time == null) time = PostsTimeFilter.All; if (pageNumber == null || pageNumber < 1) pageNumber = 1; if (pageSize == null) pageSize = 25; if (pageSize > 100) pageSize = 100; if (pageSize < 1) pageSize = 1; var model = new SearchResultsModel(); model.Query = query; model.SortBy = sort.Value; model.TimeFilter = time.Value; model.ResultType = resultType; if (!string.IsNullOrEmpty(model.Query)) { SeekedList<Guid> postIds = null; SeekedList<Guid> subIds = null; switch (resultType) { case null: postIds = _postDao.QueryPosts(query, model.LimitingToSub != null ? model.LimitingToSub.Sub.Id : (Guid?)null, sortBy: sort.Value, timeFilter: time.Value, skip: ((pageNumber - 1) * pageSize), take: pageSize); subIds = _subDao.GetAllSubs(model.Query, sortBy: SubsSortBy.Relevance, nsfw: _userContext.CurrentNsfw, skip: ((pageNumber - 1) * pageSize), take: pageSize); break; case SearchResultType.Post: postIds = _postDao.QueryPosts(query, model.LimitingToSub != null ? model.LimitingToSub.Sub.Id : (Guid?)null, sortBy: sort.Value, timeFilter: time.Value, skip: ((pageNumber - 1) * pageSize), take: pageSize); break; case SearchResultType.Sub: subIds = _subDao.GetAllSubs(model.Query, sortBy: SubsSortBy.Relevance, nsfw: _userContext.CurrentNsfw, skip: ((pageNumber - 1) * pageSize), take: pageSize); break; default: throw new Exception("unknown result type"); } if (postIds != null) model.Posts = new PagedList<PostWrapped>(_postWrapper.Wrap(postIds, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, postIds.HasMore); if (subIds != null) model.Subs = new PagedList<SubWrapped>(_subWrapper.Wrap(subIds, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, subIds.HasMore); } return View("Search", model); }