Ejemplo n.º 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));
        }
Ejemplo n.º 2
0
        public void BlogExists()
        {
            //-- arrange
            var blog = new Blog
            {
                Title   = "title",
                Excerpt = "excerpt"
            };

            _repository.AddBlog(1, blog);
            _repository.Save();

            //-- act
            var actual = _repository.BlogExists(1);

            //-- assert
            Assert.IsTrue(actual);

            //-- clean up
            _repository.DeleteBlog(blog);
            _repository.Save();
        }