コード例 #1
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));
        }