public ActionResult SearchSub(string name, string query, PostsSearchSortBy? sort, TimeFilter? time, int? pageNumber, int? pageSize) { if (string.IsNullOrEmpty(name)) return Redirect(Url.Subs()); if (sort == null) sort = PostsSearchSortBy.Relevance; if (time == null) time = TimeFilter.All; var sub = _subDao.GetSubByName(name); if (sub == null) return Redirect(Url.Subs(name)); 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, sort.Value, time.Value, ((pageNumber - 1) * pageSize), 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, TimeFilter? time, SearchResultType? resultType, int? pageNumber, int? pageSize) { if (sort == null) sort = PostsSearchSortBy.Relevance; if (time == null) time = TimeFilter.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, sort.Value, time.Value, ((pageNumber - 1) * pageSize), pageSize); subIds = _subDao.GetAllSubs(model.Query, SubsSortBy.Relevance, ((pageNumber - 1) * pageSize), pageSize); break; case SearchResultType.Post: postIds = _postDao.QueryPosts(query, model.LimitingToSub != null ? model.LimitingToSub.Sub.Id : (Guid?)null, sort.Value, time.Value, ((pageNumber - 1) * pageSize), pageSize); break; case SearchResultType.Sub: subIds = _subDao.GetAllSubs(model.Query, SubsSortBy.Relevance, ((pageNumber - 1) * pageSize), 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); }