Ejemplo n.º 1
0
        public void AddPost()
        {
            //-- arrange
            var countBeforeAdd = _repository.GetPosts(1, _resourceParameters).Count();

            var post = new Post
            {
                Title       = "title",
                Body        = "body",
                TimeCreated = DateTime.Now
            };

            //-- act
            _repository.AddPost(1, post);
            _repository.Save();

            var actual = _repository.GetPosts(1, _resourceParameters).Count();

            //-- assert
            Assert.AreEqual(countBeforeAdd + 1, actual);

            //-- clean up
            _repository.DeletePost(post);
            _repository.Save();
        }
Ejemplo n.º 2
0
        public IActionResult GetPosts(int userId, int blogId,
                                      [FromQuery] PostsResourceParameters postsResourceParameters,
                                      [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId) ||
                !_weblogDataRepository.BlogExists(blogId))
            {
                return(NotFound());
            }

            var postEntities = _weblogDataRepository.GetPosts(blogId, 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 = CreateLinksForPost(Url, userId, blogId, post.PostId);

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

            var collectionToReturn = new
            {
                posts = postsWithLinks,
                links = LinksForCollection.Create(
                    CreatePostsResourceUri,
                    new int[] { userId, blogId },
                    postsResourceParameters,
                    postEntities.HasPrevious,
                    postEntities.HasNext)
            };

            return(Ok(collectionToReturn));
        }