Beispiel #1
0
        public IActionResult GetPosts([FromQuery] PostsResourceParameter resourceParameter)
        {
            //List<Post> posts = await _context.Posts.ToListAsync();
            //var postEntities = postRepo.Get(10);
            var postEntities = postRepo.Get(resourceParameter);

            var paginationMetadata = new
            {
                totalCount  = postEntities.TotalCount,
                pageSize    = postEntities.PageSize,
                currentPage = postEntities.CurrentPage,
                totalPages  = postEntities.TotalPages
            };

            Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata));

            var postDtos = mapper.Map <IEnumerable <PostDto> >(postEntities).Expand();

            var postDtosWithLinks = postDtos.Select(el =>
            {
                var postDictionary = el as IDictionary <string, object>;
                postDictionary.Add("links", CreateLinksForPost((Guid)postDictionary["Id"]));
                return(postDictionary);
            });

            var links = CreateLinksForPosts(resourceParameter, postEntities.HasNext, postEntities.HasPrevious);

            return(Ok(new { posts = postDtosWithLinks, links }));
        }
Beispiel #2
0
        private IEnumerable <LinkDto> CreateLinksForPosts(PostsResourceParameter resourceParameter, bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>();

            links.Add(new LinkDto
            {
                Href   = Url.Link("GetPosts", new { resourceParameter }),
                Rel    = "currentPage",
                Method = "GET"
            });

            if (hasNext)
            {
                links.Add(new LinkDto
                {
                    Href   = Url.Link("GetPosts", new { PageNumber = resourceParameter.PageNumber + 1, resourceParameter.BlogCategory, resourceParameter.PageSize }),
                    Rel    = "nextPage",
                    Method = "GET"
                });
            }

            if (hasPrevious)
            {
                links.Add(new LinkDto
                {
                    Href   = Url.Link("GetPosts", new { PageNumber = resourceParameter.PageNumber - 1, resourceParameter.BlogCategory, resourceParameter.PageSize }),
                    Rel    = "prevPage",
                    Method = "GET"
                });
            }
            return(links);
        }
        public PagedList <Post> Get(PostsResourceParameter resourceParameter)
        {
            var posts = context.Posts
                        .OrderByDescending(p => p.CreatedDate)
                        .Include(p => p.Comments)
                        .ThenInclude(p => p.User)
                        .Include(p => p.Images) as IQueryable <Post>;

            if (!string.IsNullOrEmpty(resourceParameter.BlogCategory))
            {
                posts = posts.Where(p => p.BlogCategory == (int)Enum.Parse(typeof(BlogCategory), resourceParameter.BlogCategory, true));
            }

            return(PagedList <Post> .Create(posts, resourceParameter.PageNumber, resourceParameter.PageSize));
        }
Beispiel #4
0
 public PagedList <Post> Get(PostsResourceParameter resourceParameter)
 {
     throw new NotImplementedException();
 }