示例#1
0
        public IActionResult GetComment(int userId, int blogId, int postId, int commentId,
                                        [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId) ||
                !_weblogDataRepository.BlogExists(blogId) ||
                !_weblogDataRepository.PostExists(postId))
            {
                return(NotFound());
            }

            var commentFromRepo = _weblogDataRepository.GetComment(commentId);

            if (commentFromRepo is null)
            {
                return(NotFound());
            }

            var commentToReturn = _mapper.Map <CommentDto>(commentFromRepo);

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

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

            var links = CreateLinksForComment(userId, blogId, postId,
                                              commentToReturn.CommentId, commentToReturn.UserId);

            var commentWithLinks = new CommentDtoWithLinks(commentToReturn, links);

            return(Ok(commentWithLinks));
        }
示例#2
0
        public void GetComment()
        {
            //-- arrange
            var comment = new Comment
            {
                UserId      = 1,
                Body        = "body",
                TimeCreated = DateTime.Now
            };

            _repository.AddComment(1, comment);
            _repository.Save();

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

            //-- assert
            Assert.AreEqual("body", actual.Body);

            //-- cleanup
            _repository.DeleteComment(comment);
            _repository.Save();
        }