From 6c4f701aa0e484127fd538bd91063acc3660fa06 Mon Sep 17 00:00:00 2001 From: ybchar Date: Sun, 7 Apr 2024 04:45:25 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=BC=EC=A7=80=20comments=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/FindOneBoardResponse.java | 5 ++++- .../daybook/domain/board/entity/Hashtag.java | 3 ++- .../domain/board/service/BoardService.java | 11 +++++++++-- .../dto/response/FindOneCommentResponse.java | 12 ++++++++++++ .../daybook/domain/comment/entity/Comment.java | 3 ++- .../repository/CommentRepositoryCustom.java | 6 ++++++ .../repository/CommentRepositoryImpl.java | 18 ++++++++++++++++-- .../domain/reaction/entity/Reaction.java | 3 ++- 8 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java diff --git a/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java b/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java index 9f8db83..29eab8b 100644 --- a/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java +++ b/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java @@ -5,6 +5,7 @@ import com.unit.daybook.domain.board.entity.Board; import com.unit.daybook.domain.board.entity.Hashtag; +import com.unit.daybook.domain.comment.dto.response.FindOneCommentResponse; import com.unit.daybook.domain.reaction.dto.response.ReactionTypeAndCount; import com.unit.daybook.domain.reaction.entity.Reaction; @@ -19,9 +20,10 @@ public record FindOneBoardResponse( List hashtags, String paperType, List reactions, + List comments, LocalDateTime createdAt ) { - public static FindOneBoardResponse of(Board board, List reactions) { + public static FindOneBoardResponse of(Board board, List reactions, List comments) { List hashContents = board.getHashtags().stream().map(Hashtag::getContent).toList(); Long heartsFromReactions = reactions.stream().mapToLong(ReactionTypeAndCount::count).sum(); return new FindOneBoardResponse( @@ -34,6 +36,7 @@ public static FindOneBoardResponse of(Board board, List re hashContents, board.getPaperType(), reactions, + comments, board.getCreatedAt() ); } diff --git a/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java b/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java index bccbfd8..49795ec 100644 --- a/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java +++ b/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java @@ -2,6 +2,7 @@ import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto; +import com.unit.daybook.domain.common.model.BaseTimeEntity; import com.unit.daybook.domain.member.domain.Member; import jakarta.persistence.*; import lombok.AccessLevel; @@ -15,7 +16,7 @@ @Entity @Table(name = "hashtag") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Hashtag { +public class Hashtag extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "hashtag_id") diff --git a/src/main/java/com/unit/daybook/domain/board/service/BoardService.java b/src/main/java/com/unit/daybook/domain/board/service/BoardService.java index 52007ae..0fe67b1 100644 --- a/src/main/java/com/unit/daybook/domain/board/service/BoardService.java +++ b/src/main/java/com/unit/daybook/domain/board/service/BoardService.java @@ -10,6 +10,9 @@ import com.unit.daybook.domain.board.entity.ReadBoard; import com.unit.daybook.domain.board.repository.*; +import com.unit.daybook.domain.comment.dto.response.FindOneCommentResponse; +import com.unit.daybook.domain.comment.entity.Comment; +import com.unit.daybook.domain.comment.repository.CommentRepository; import com.unit.daybook.domain.member.domain.Member; import com.unit.daybook.domain.member.repository.MemberRepository; import com.unit.daybook.domain.reaction.dto.response.ReactionTypeAndCount; @@ -33,6 +36,7 @@ public class BoardService { private final ReadBoardRepository readBoardRepository; private final HashtagRepository hashtagRepository; private final ReactionRepository reactionRepository; + private final CommentRepository commentRepository; public BoardResponseDto addBoard(AddBoardRequestDto addBoardRequestDto, Long memberId) { Member member = memberRepository @@ -60,8 +64,11 @@ public FindOneBoardResponse getBoard(Long boardId) { Board board = boardRepository.findById(boardId) .orElseThrow(() -> new CustomException(ErrorCode.BOARD_NOT_FOUND)); List reactions = reactionRepository.findAllByBoardGroupByReactionType(board); - - return FindOneBoardResponse.of(board, reactions); + List comments = commentRepository.findCommentByBoard(boardId) + .stream() + .map(FindOneCommentResponse::from) + .toList(); + return FindOneBoardResponse.of(board, reactions, comments); } @Transactional(readOnly = true) diff --git a/src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java b/src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java new file mode 100644 index 0000000..3a168c5 --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java @@ -0,0 +1,12 @@ +package com.unit.daybook.domain.comment.dto.response; + +import com.unit.daybook.domain.comment.entity.Comment; + +public record FindOneCommentResponse( + Long commentId, + String content +) { + public static FindOneCommentResponse from(Comment comment) { + return new FindOneCommentResponse(comment.getCommentId(), comment.getContent()); + } +} diff --git a/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java b/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java index ac41cd1..4ba1237 100644 --- a/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java +++ b/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java @@ -1,6 +1,7 @@ package com.unit.daybook.domain.comment.entity; import com.unit.daybook.domain.board.entity.Board; +import com.unit.daybook.domain.common.model.BaseTimeEntity; import com.unit.daybook.domain.member.domain.Member; import jakarta.persistence.Column; @@ -21,7 +22,7 @@ @Entity @Table(name = "comment") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Comment { +public class Comment extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java index 4aeac96..3350b82 100644 --- a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java +++ b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java @@ -1,4 +1,10 @@ package com.unit.daybook.domain.comment.repository; +import java.util.List; + +import com.unit.daybook.domain.comment.dto.response.FindOneCommentResponse; +import com.unit.daybook.domain.comment.entity.Comment; + public interface CommentRepositoryCustom { + List findCommentByBoard(Long boardId); } diff --git a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java index 277bdea..a7baf09 100644 --- a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java +++ b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java @@ -1,16 +1,30 @@ package com.unit.daybook.domain.comment.repository; +import static com.unit.daybook.domain.board.entity.QBoard.*; +import static com.unit.daybook.domain.comment.entity.QComment.*; + +import java.util.List; + import org.springframework.stereotype.Repository; import com.querydsl.jpa.impl.JPAQueryFactory; +import com.unit.daybook.domain.comment.entity.Comment; import lombok.RequiredArgsConstructor; @Repository @RequiredArgsConstructor -public class CommentRepositoryImpl { +public class CommentRepositoryImpl implements CommentRepositoryCustom{ private final JPAQueryFactory jpaQueryFactory; - + @Override + public List findCommentByBoard(Long boardId) { + return jpaQueryFactory.selectFrom(comment) + .leftJoin(comment.board, board) + .fetchJoin() + .where(board.boardId.eq(boardId)) + .orderBy(comment.createdAt.desc()) + .fetch(); + } } diff --git a/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java b/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java index 6c78e71..e9391f4 100644 --- a/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java +++ b/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java @@ -1,6 +1,7 @@ package com.unit.daybook.domain.reaction.entity; import com.unit.daybook.domain.board.entity.Board; +import com.unit.daybook.domain.common.model.BaseTimeEntity; import com.unit.daybook.domain.member.domain.Member; import jakarta.persistence.*; @@ -14,7 +15,7 @@ @Entity @Table(name = "reaction") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Reaction { +public class Reaction extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)