Exemple #1
0
        public void AddComment()
        {
            //-- arrange
            var countBeforeAdd = _repository.GetComments(1, _resourceParameters).Count();

            var comment = new Comment
            {
                UserId      = 1,
                Body        = "body",
                TimeCreated = DateTime.Now
            };

            //-- act
            _repository.AddComment(1, comment);
            _repository.Save();

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

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

            //-- clean up
            _repository.DeleteComment(comment);
            _repository.Save();
        }
Exemple #2
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));
        }