コード例 #1
0
        public IEnumerable <CommentDto> GetCommentsForPost(int postId, int?userId)
        {
            Post post = _db.Post.FirstOrDefault(p => p.PostId == postId) ?? throw new PostNotFoundException();

            IEnumerable <Comment> commentListQuery = _db.Comment
                                                     .Where(c => c.PostId == postId & c.ParentCommentId == null)
                                                     .Include(c => c.User)
                                                     .Include(c => c.InverseParentComment).ThenInclude(c => c.CommentLike)
                                                     .Include(c => c.InverseParentComment).ThenInclude(c => c.User)
                                                     .Include(c => c.CommentType)
                                                     .Include(c => c.CommentLike);

            return(CommentMapper.ToDto(commentListQuery, userId));
        }
コード例 #2
0
        public void EntityToDtoTest()
        {
            // Arrange
            var model = new CommentEntity
            {
                Id      = 1,
                Content = "Rainy day.",
                Author  = new UserEntity
                {
                    UserName = "******"
                }
            };

            // Act
            var dto = _commentMapper.ToDto(model);

            // Assert
            Assert.Equal(model.Id, dto.Id);
            Assert.Equal(model.Content, dto.Content);
            Assert.Equal(model.Author.UserName, dto.Author);
        }
コード例 #3
0
        public (CommentDto commentDto, PostDto post) CreateComment(CommentDto commentDto, int?userId)
        {
            Post post = _db.Post
                        .Where(p => p.PostId == commentDto.PostId)
                        .Include(p => p.PostType)
                        .FirstOrDefault() ?? throw new PostNotFoundException();

            Comment comment = CommentMapper.FromDto(commentDto);

            comment.UserId = (int)userId;

            _db.Comment.Add(comment);
            _db.SaveChanges();

            Comment commentWithJoin = _db.Comment
                                      .Where(c => c.CommentId == comment.CommentId)
                                      .Include(c => c.User)
                                      .Include(c => c.CommentType)
                                      .First();

            return(CommentMapper.ToDto(commentWithJoin, userId), PostMapper.ToDtoPostUrl(post));
        }