예제 #1
0
        public IActionResult GetComments(int userId, int blogId, int postId,
                                         [FromQuery] CommentsResourceParameters commentsResourceParameters,
                                         [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId) ||
                !_weblogDataRepository.BlogExists(blogId) ||
                !_weblogDataRepository.PostExists(postId))
            {
                return(NotFound());
            }

            var commentEntities = _weblogDataRepository.GetComments(postId, commentsResourceParameters);

            var commentsToReturn = _mapper.Map <IEnumerable <CommentDto> >(commentEntities);

            Response.Headers.Add(PaginationHeader <Comment> .Get(commentEntities));

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

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

            var commentsWithLinks = commentsToReturn.Select(comment =>
            {
                var links = CreateLinksForComment(userId, blogId, postId, comment.CommentId, comment.UserId);

                return(new CommentDtoWithLinks(comment, links));
            });

            var collectionToReturn = new
            {
                comments = commentsWithLinks,
                links    = LinksForCollection.Create(
                    CreateCommentsResourceUri,
                    new int[] { userId, blogId, postId },
                    commentsResourceParameters,
                    commentEntities.HasPrevious,
                    commentEntities.HasNext)
            };

            return(Ok(collectionToReturn));
        }
예제 #2
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));
        }
예제 #3
0
        public IActionResult GetBlogs(int userId,
                                      [FromQuery] BlogsResourceParameters blogsResourceParameters,
                                      [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId))
            {
                return(NotFound());
            }

            var blogEntities = _weblogDataRepository.GetBlogs(userId, blogsResourceParameters);

            var blogsToReturn = _mapper.Map <IEnumerable <BlogDto> >(blogEntities);

            Response.Headers.Add(PaginationHeader <Blog> .Get(blogEntities));

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

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

            var blogsWithLinks = blogsToReturn.Select(blog =>
            {
                var links = CreateLinksForBlog(Url, userId, blog.BlogId);

                return(new BlogDtoWithLinks(blog, links));
            });

            var collectionToReturn = new
            {
                blogs = blogsWithLinks,
                links = LinksForCollection.Create(
                    CreateBlogsResourceUri,
                    new int[] { userId },
                    blogsResourceParameters,
                    blogEntities.HasPrevious,
                    blogEntities.HasNext)
            };

            return(Ok(collectionToReturn));
        }
예제 #4
0
        public IActionResult GetUsers(
            [FromQuery] UsersResourceParameters usersResourceParameters,
            [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            var userEntities = _weblogDataRepository.GetUsers(usersResourceParameters);

            var usersToReturn = _mapper.Map <IEnumerable <UserDto> >(userEntities);

            Response.Headers.Add(PaginationHeader <User> .Get(userEntities));

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

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

            var usersWithLinks = usersToReturn.Select(user =>
            {
                var links = CreateLinksForUser(user.UserId);

                return(new UserDtoWithLinks(user, links));
            });

            var resourceToReturn = new
            {
                users = usersWithLinks,
                links = LinksForCollection.Create(CreateUsersResourceUri,
                                                  Array.Empty <int>(),
                                                  usersResourceParameters,
                                                  userEntities.HasPrevious,
                                                  userEntities.HasNext)
            };

            return(Ok(resourceToReturn));
        }