Esempio n. 1
0
        public IActionResult GetAllPostsForUser(int userId,
                                                [FromQuery] PostsResourceParameters postsResourceParameters,
                                                [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId))
            {
                return(NotFound());
            }

            var postEntities = _weblogDataRepository.GetAllPostsForUser(userId, postsResourceParameters);

            var postsToReturn = _mapper.Map <IEnumerable <PostDto> >(postEntities);

            Response.Headers.Add(PaginationHeader <Post> .Get(postEntities));

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

            if (!includeLinks)
            {
                return(Ok(postsToReturn));
            }

            var postsWithLinks = postsToReturn.Select(post =>
            {
                var links = PostsController.CreateLinksForPost(
                    Url, userId, post.BlogId, post.PostId);

                return(new PostDtoWithLinks(post, links));
            });

            var collectionToReturn = new
            {
                posts = postsWithLinks,
                links = LinksForCollection.Create(
                    CreatePostsResourceUri,
                    Array.Empty <int>(),
                    postsResourceParameters,
                    postEntities.HasPrevious,
                    postEntities.HasNext)
            };

            return(Ok(collectionToReturn));
        }