コード例 #1
0
        void AppendSort(StringBuilder sb, PostSort sort)
        {
            switch (sort)
            {
            case PostSort.Updated:
                sb.Append("updated");
                break;

            case PostSort.Created:
                sb.Append("created");
                break;

            case PostSort.Stars:
                sb.Append("stars");
                break;

            case PostSort.Watches:
                sb.Append("watches");
                break;

            case PostSort.Comments:
                sb.Append("comments");
                break;

            case PostSort.BestMatch:
                sb.Append("best_match");
                break;

            default:
                break;
            }
        }
コード例 #2
0
        public async Task <IHttpActionResult> GetPosts(
            string id,
            PostSort sort = PostSort.New,
            int offset    = 0,
            int limit     = 20)
        {
            offset = Math.Max(0, offset);
            limit  = Math.Min(100, limit);

            var decodedId = id.FromEncodedId();
            var category  = await _dbContext.Categories.FindAsync(decodedId);

            if (category == null)
            {
                return(NotFound());
            }

            var max = await _dbContext.Entry(category)
                      .Collection(p => p.Posts)
                      .Query()
                      .ExcludeDeleted()
                      .CountAsync();

            List <Post> posts;

            if (sort == PostSort.Hot)
            {
                // calculating hotness needs a bit of a more complex query
                posts = await _dbContext.Entry(category)
                        .Collection(p => p.Posts)
                        .Query()
                        .ExcludeDeleted()
                        .OrderByHotness()
                        .Skip(offset)
                        .Take(limit)
                        .ToListAsync();
            }
            else
            {
                Expression <Func <Post, int> > sortClause = p => p.Id;

                if (sort == PostSort.Top)
                {
                    sortClause = p => p.Votes.Count(m => m.IsUp) - p.Votes.Count(m => !m.IsUp);
                }

                posts = await _dbContext.Entry(category)
                        .Collection(p => p.Posts)
                        .Query()
                        .ExcludeDeleted()
                        .OrderByDescending(sortClause)
                        .Skip(offset)
                        .Take(limit)
                        .ToListAsync();
            }

            return(OkPageData(await _dtoService.CreateListAsync <Post, DtoPost>(posts), offset + limit < max));
        }
コード例 #3
0
        public async Task <IHttpActionResult> GetPosts(Country country, PostSort sort = PostSort.New, int offset = 0, int limit = 20)
        {
            offset = Math.Max(0, offset);
            limit  = Math.Min(100, limit);

            var max = await _dbContext.Posts
                      .ExcludeDeleted()
                      .CountAsync();

            List <Post> posts;

            if (sort == PostSort.Hot)
            {
                // calculating hotness needs a bit of a more complex query
                posts = await _dbContext.Posts
                        .ExcludeDeleted()
                        .Where(p => p.Category.Country == country)
                        .OrderByHotness()
                        .Skip(offset)
                        .Take(limit)
                        .ToListAsync();
            }
            else
            {
                Expression <Func <Post, int> > sortClause = p => p.Id;

                if (sort == PostSort.Top)
                {
                    sortClause = p => p.Votes.Count(m => m.IsUp) - p.Votes.Count(m => !m.IsUp);
                }

                posts = await _dbContext.Posts
                        .ExcludeDeleted()
                        .Where(p => p.Category.Country == country)
                        .OrderByDescending(sortClause)
                        .Skip(offset)
                        .Take(limit)
                        .ToListAsync();
            }

            return(OkPageData(await _dtoService.CreateListAsync <Post, DtoPost>(posts), offset + limit < max));
        }
コード例 #4
0
 public GetPostsRequest Sort(PostSort sort)
 {
     return(this.Query("sort", sort));
 }
コード例 #5
0
		public async Task<Tuple<List<RedditPost>, string, string>> LoadPostsAsync(string suffix, PostSort sort, PostType type, int countViewed, string after)
		{
			var posts = new Tuple<List<RedditPost>, string, string>(new List<RedditPost>(), "", "");

			switch (type)
			{
				case PostType.All:
					switch (sort)
					{
						case PostSort.Controversial:
							posts = await Helpers.Posts.FindControversialPostsAsync(suffix, countViewed, after);
							break;

						case PostSort.Hot:
							posts = await Helpers.Posts.FindHotPostsAsync(suffix, countViewed, after);
							break;

						case PostSort.New:
							posts = await Helpers.Posts.FindNewPostsAsync(suffix, countViewed, after);
							break;

						case PostSort.Rising:
							posts = await Helpers.Posts.FindRisingPostsAsync(suffix, countViewed, after);
							break;

						case PostSort.Top:
						default:
							posts = await Helpers.Posts.FindTopPostsAsync(suffix, countViewed, after);
							break;
					}
					break;

				case PostType.Pics:
					switch (sort)
					{
						case PostSort.Controversial:
							posts = await Helpers.Pics.FindControversialPicsAsync(suffix, countViewed, after);
							break;

						case PostSort.Hot:
							posts = await Helpers.Pics.FindHotPicsAsync(suffix, countViewed, after);
							break;

						case PostSort.New:
							posts = await Helpers.Pics.FindNewPicsAsync(suffix, countViewed, after);
							break;

						case PostSort.Rising:
							posts = await Helpers.Pics.FindRisingPicsAsync(suffix, countViewed, after);
							break;

						case PostSort.Top:
						default:
							posts = await Helpers.Pics.FindTopPicsAsync(suffix, countViewed, after);
							break;
					}
					break;

				case PostType.SelfText:
					switch (sort)
					{
						case PostSort.Controversial:
							posts = await Helpers.SelfText.FindControversialSelfTextAsync(suffix, countViewed, after);
							break;

						case PostSort.Hot:
							posts = await Helpers.SelfText.FindHotSelfTextAsync(suffix, countViewed, after);
							break;

						case PostSort.New:
							posts = await Helpers.SelfText.FindNewSelfTextAsync(suffix, countViewed, after);
							break;

						case PostSort.Rising:
							posts = await Helpers.SelfText.FindRisingSelfTextAsync(suffix, countViewed, after);
							break;

						case PostSort.Top:
						default:
							posts = await Helpers.SelfText.FindTopSelfTextAsync(suffix, countViewed, after);
							break;
					}
					break;
			}

			return posts;
		}
コード例 #6
0
		public async Task<bool> LoadMorePostsAsync(string suffix, PostSort sort, PostType type)
		{
			Tuple<List<RedditPost>, string, string> posts = await LoadPostsAsync(suffix, sort, type, CountViewed, AfterPost);

			foreach (RedditPost p in posts.Item1)
			{
				Posts.Add(p);
			}
			BeforePost = posts.Item2;
			AfterPost = posts.Item3;
			await CheckPostStuff();

			CountViewed += 25;

			_loading = false;

			return true;
		}
コード例 #7
0
		public async Task<bool> LoadInitialPostsAsync(string suffix, PostSort sort, PostType type)
		{
			Suffix = suffix;
			Sort = sort;
			Type = type;

			Tuple<List<RedditPost>, string, string> posts = await LoadPostsAsync(Suffix, Sort, Type);

			Posts = posts.Item1;
			FeaturedPost = posts.Item1[0];
			BeforePost = posts.Item2;
			AfterPost = posts.Item3;
			await CheckPostStuff();

			CountViewed = 25;

			return true;
		}
コード例 #8
0
        public Task <Post[]> GetPostsAsync(string teamName, string query, PostIncludes include = PostIncludes.None, PostSort sort = PostSort.Updated, PostOrder order = PostOrder.Desc, int perPage = 20)
        {
            var url = BuildStringBuilderWithTeamName(teamName);

            url.Append("/posts");
            url.Append("?q="); url.Append(query);
            url.Append("&include="); AppendInclude(url, include);
            url.Append("&sort="); AppendSort(url, sort);
            url.Append("&order="); AppendOrder(url, order);
            url.Append("&per_page="); url.Append(perPage);

            return(ReadPaginiationMesasge <Post>(url.ToString()));
        }