From 6e99da47085b03ce25febe301d153e9e7965e4b0 Mon Sep 17 00:00:00 2001 From: HyoBN Date: Tue, 9 Jul 2024 17:55:37 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EB=8C=80=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EC=9C=84=ED=95=9C=20=EC=97=94=ED=8B=B0=ED=8B=B0=20?= =?UTF-8?q?=ED=95=84=EB=93=9C=20=EB=B0=8F=20Enum=20=EC=B6=94=EA=B0=80"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../treehouse/server/global/entity/comment/Comment.java | 7 +++++++ .../server/global/entity/comment/CommentType.java | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 src/main/java/treehouse/server/global/entity/comment/CommentType.java diff --git a/src/main/java/treehouse/server/global/entity/comment/Comment.java b/src/main/java/treehouse/server/global/entity/comment/Comment.java index 3dfd030..6b135c6 100644 --- a/src/main/java/treehouse/server/global/entity/comment/Comment.java +++ b/src/main/java/treehouse/server/global/entity/comment/Comment.java @@ -25,4 +25,11 @@ public class Comment extends BaseDateTimeEntity { @JoinColumn(name = "postId") @ManyToOne(fetch = FetchType.LAZY) private Post post; + + @JoinColumn(name = "parentId") + private Long parentId; + + @JoinColumn(name = "type") + @Enumerated(EnumType.STRING) + private CommentType type; } \ No newline at end of file diff --git a/src/main/java/treehouse/server/global/entity/comment/CommentType.java b/src/main/java/treehouse/server/global/entity/comment/CommentType.java new file mode 100644 index 0000000..db84650 --- /dev/null +++ b/src/main/java/treehouse/server/global/entity/comment/CommentType.java @@ -0,0 +1,6 @@ +package treehouse.server.global.entity.comment; + +public enum CommentType { + + PARENT, CHILD; +} From 46e95e6d5fb6895acc4a102d9311fda158e32131 Mon Sep 17 00:00:00 2001 From: HyoBN Date: Tue, 9 Jul 2024 17:56:14 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20api=20-=20=EB=8C=80=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=ED=95=A8=EA=BB=98=20=EC=A1=B0=ED=9A=8C=EB=90=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/comment/business/CommentMapper.java | 15 ++++++- .../api/comment/business/CommentService.java | 40 ++++++++++++++++++- .../implementation/CommentQueryAdapter.java | 19 +++++++++ .../persistence/CommentRepository.java | 5 +++ .../presentation/dto/CommentResponseDTO.java | 14 +++++++ 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/main/java/treehouse/server/api/comment/business/CommentMapper.java b/src/main/java/treehouse/server/api/comment/business/CommentMapper.java index 4286ac6..db66f49 100644 --- a/src/main/java/treehouse/server/api/comment/business/CommentMapper.java +++ b/src/main/java/treehouse/server/api/comment/business/CommentMapper.java @@ -12,8 +12,21 @@ public class CommentMapper { - public static CommentResponseDTO.CommentInfoDto toCommentInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList) { + public static CommentResponseDTO.CommentInfoDto toCommentInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList, + List replyInfoDtoList) { return CommentResponseDTO.CommentInfoDto.builder() + .commentedAt(comment.getCreatedAt().toString()) + .commentId(comment.getId()) + .context(comment.getContent()) + .reactionList(reactionList) + .replyList(replyInfoDtoList) + .memberProfile(MemberMapper.toGetWriterProfile(comment.getWriter())) + .build(); + + } + + public static CommentResponseDTO.ReplyInfoDto toReplyInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList) { + return CommentResponseDTO.ReplyInfoDto.builder() .commentedAt(comment.getCreatedAt().toString()) .commentId(comment.getId()) .context(comment.getContent()) diff --git a/src/main/java/treehouse/server/api/comment/business/CommentService.java b/src/main/java/treehouse/server/api/comment/business/CommentService.java index e883b3a..5d8f1ae 100644 --- a/src/main/java/treehouse/server/api/comment/business/CommentService.java +++ b/src/main/java/treehouse/server/api/comment/business/CommentService.java @@ -87,13 +87,17 @@ public CommentResponseDTO.CommentListDto getCommentResponseList(User user, Long Member member = memberQueryAdapter.findByUserAndTreehouse(user, treehouse); Pageable pageable = PageRequest.of(page, 10, Sort.by(Sort.Direction.DESC, "createdAt")); - List commentListByPostId = commentQueryAdapter.getCommentListByPostId(postId, pageable); +// List commentListByPostId = commentQueryAdapter.getCommentListByPostId(postId, pageable); + List commentListByPostId = commentQueryAdapter.getParentCommentListByPostId(postId, pageable); + log.error("PO size : {}", commentListByPostId.size()); // 신고된 댓글을 필터링 List filteredComments = commentListByPostId.stream() .filter(comment -> !reportQueryAdapter.isReportedComment(comment)) .collect(Collectors.toList()); + log.error("size : {}", filteredComments.size()); + // 각 댓글마다 reactionList를 생성 List commentInfoDtoList = filteredComments.stream() .map(comment -> { @@ -111,10 +115,42 @@ public CommentResponseDTO.CommentListDto getCommentResponseList(User user, Long )); ReactionResponseDTO.getReactionList reactionDtoList = ReactionMapper.toGetReactionList(reactionMap); - return CommentMapper.toCommentInfoDto(comment, reactionDtoList); + // 여기서 comment 에 대한 reply 목록을 조회해서 List 를 만든 다음, mapper에 매개변수로 넣고, mapper 코드 업데이트 하자. + + // 1. queryAdapter 에 parentId 가지고 childList 조회하는 메서드 만들기 + List childCommentList = commentQueryAdapter.getChildCommentListByPostIdAndParentId(postId, comment.getId(), pageable); + + // 2. mapper 에 childList를 각각 comment 에서 ReplyInfoDto 로 바꾸는 메서드 만들기 + // 각 답글마다 reactionList를 생성 + List replyInfoDtoList = childCommentList.stream() + .map(reply -> { + List replyReactions = reactionQueryAdapter.findAllByComment(reply); + Map replyReactionMap = replyReactions.stream() + .collect(Collectors.toMap( + Reaction::getReactionName, + reaction -> { + String reactionName = reaction.getReactionName(); + Integer reactionCount = reactionQueryAdapter.countReactionsByReactionNameAndCommentId(reactionName, reply.getId()); + Boolean isPushed = reactionQueryAdapter.existByMemberAndCommentAndReactionName(member, reply, reactionName); + return ReactionMapper.toGetReaction(reaction, reactionCount, isPushed); + }, + (existing, replacement) -> existing // 중복되는 경우 기존 값을 사용 + )); + ReactionResponseDTO.getReactionList replyReactionDtoList = ReactionMapper.toGetReactionList(replyReactionMap); + + return CommentMapper.toReplyInfoDto(reply, replyReactionDtoList); + }) + .collect(Collectors.toList()); + + + // 3. mapper 에 이렇게 만든 childList 를 포함해서 최종 응답 형태인 CommentList 로 바꾸는 메서드 만들고 호출하기 + + return CommentMapper.toCommentInfoDto(comment, reactionDtoList,replyInfoDtoList); }) .collect(Collectors.toList()); + log.error("commentListDto size : {}", commentListByPostId.size()); + CommentResponseDTO.CommentListDto commentListDto = CommentMapper.toCommentListDto(commentInfoDtoList); return commentListDto; } diff --git a/src/main/java/treehouse/server/api/comment/implementation/CommentQueryAdapter.java b/src/main/java/treehouse/server/api/comment/implementation/CommentQueryAdapter.java index 0f3597b..693bec8 100644 --- a/src/main/java/treehouse/server/api/comment/implementation/CommentQueryAdapter.java +++ b/src/main/java/treehouse/server/api/comment/implementation/CommentQueryAdapter.java @@ -2,9 +2,15 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import treehouse.server.api.comment.business.CommentMapper; import treehouse.server.api.comment.persistence.CommentRepository; +import treehouse.server.api.comment.presentation.dto.CommentResponseDTO; +import treehouse.server.api.reaction.business.ReactionMapper; +import treehouse.server.api.reaction.presentation.dto.ReactionResponseDTO; import treehouse.server.global.annotations.Adapter; import treehouse.server.global.entity.comment.Comment; +import treehouse.server.global.entity.comment.CommentType; +import treehouse.server.global.entity.reaction.Reaction; import treehouse.server.global.exception.GlobalErrorCode; import treehouse.server.global.exception.ThrowClass.CommentException; @@ -12,6 +18,8 @@ import org.springframework.data.domain.Pageable; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Adapter @RequiredArgsConstructor @@ -27,4 +35,15 @@ public Comment getCommentById(Long commentId){ public List getCommentListByPostId(Long postId, Pageable pageable) { return commentRepository.findAllByPostId(postId, pageable); } + + public List getParentCommentListByPostId(Long postId, Pageable pageable) { + log.error("post id : {}",postId); + return commentRepository.findAllByPostIdAndType(postId, CommentType.PARENT, pageable); + } + + public List getChildCommentListByPostIdAndParentId(Long postId, Long parentId, Pageable pageable) { + return commentRepository.findAllByPostIdAndTypeAndParentId(postId, CommentType.CHILD, parentId, pageable); + } + } + diff --git a/src/main/java/treehouse/server/api/comment/persistence/CommentRepository.java b/src/main/java/treehouse/server/api/comment/persistence/CommentRepository.java index 4e98f6a..a057f81 100644 --- a/src/main/java/treehouse/server/api/comment/persistence/CommentRepository.java +++ b/src/main/java/treehouse/server/api/comment/persistence/CommentRepository.java @@ -3,6 +3,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import treehouse.server.global.entity.comment.Comment; +import treehouse.server.global.entity.comment.CommentType; import java.util.List; @@ -10,4 +11,8 @@ public interface CommentRepository extends JpaRepository { List findAllByPostId(Long postId, Pageable pageable); + List findAllByPostIdAndType(Long postId, CommentType type, Pageable pageable); + + List findAllByPostIdAndTypeAndParentId(Long postId, CommentType type, Long parentId, Pageable pageable); + } diff --git a/src/main/java/treehouse/server/api/comment/presentation/dto/CommentResponseDTO.java b/src/main/java/treehouse/server/api/comment/presentation/dto/CommentResponseDTO.java index 49fd2c8..76732e1 100644 --- a/src/main/java/treehouse/server/api/comment/presentation/dto/CommentResponseDTO.java +++ b/src/main/java/treehouse/server/api/comment/presentation/dto/CommentResponseDTO.java @@ -22,9 +22,23 @@ public static class CommentInfoDto{ ReactionResponseDTO.getReactionList reactionList; Long commentId; String context; + List replyList; String commentedAt; } + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class ReplyInfoDto{ + MemberResponseDTO.getWriterProfile memberProfile; + ReactionResponseDTO.getReactionList reactionList; + Long commentId; + String context; + String commentedAt; + } + + @Builder @Getter @NoArgsConstructor From aff3a737edbf9f09b652e7562830fb4e2bc93784 Mon Sep 17 00:00:00 2001 From: HyoBN Date: Tue, 9 Jul 2024 18:12:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EB=8C=80=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EC=9E=91=EC=84=B1=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/api/comment/business/CommentMapper.java | 5 ++++- .../api/comment/business/CommentService.java | 14 +++++++++++++- .../api/comment/presentation/CommentApi.java | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/treehouse/server/api/comment/business/CommentMapper.java b/src/main/java/treehouse/server/api/comment/business/CommentMapper.java index db66f49..8b6148a 100644 --- a/src/main/java/treehouse/server/api/comment/business/CommentMapper.java +++ b/src/main/java/treehouse/server/api/comment/business/CommentMapper.java @@ -4,6 +4,7 @@ import treehouse.server.api.member.business.MemberMapper; import treehouse.server.api.reaction.presentation.dto.ReactionResponseDTO; import treehouse.server.global.entity.comment.Comment; +import treehouse.server.global.entity.comment.CommentType; import treehouse.server.global.entity.member.Member; import treehouse.server.global.entity.post.Post; @@ -42,11 +43,13 @@ public static CommentResponseDTO.CommentListDto toCommentListDto(List createComment( return CommonResponse.onSuccess(commentService.createComment(user, treehouseId, postId, request)); } + @PostMapping("/{commentId}") + @Operation(summary = "대댓글 작성 API 🔑", description = "특정 Comment에 대해서 대댓글을 작성하는 API 입니다.") + public CommonResponse createReply( + @PathVariable(name = "treehouseId")Long treehouseId, + @PathVariable(name = "postId")Long postId, + @PathVariable(name = "commentId")Long commentId, + @AuthMember @Parameter(hidden = true) User user, + @RequestBody CommentRequestDTO.createComment request + ) + { + return CommonResponse.onSuccess(commentService.createReply(user, treehouseId, postId, commentId, request)); + } + @DeleteMapping("/{commentId}") @Operation(summary = "댓글 삭제 API 🔑", description = "댓글을 삭제하는 API 입니다.") public CommonResponse deleteComment(