Example #1
0
        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));
        }