From 2046761e1022c5c7a5a277f41dce052cafccb76c Mon Sep 17 00:00:00 2001 From: jamie Date: Thu, 15 Aug 2024 20:35:59 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[FEAT]=20=EB=8C=93=EA=B8=80=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20API=20=EC=98=A4=EB=A5=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/CommentConverter.java | 31 +++++++++++++++++++ .../stepperbackend/domain/Comment.java | 1 - .../repository/CommentRepository.java | 4 +-- .../CommentService/CommentService.java | 2 -- .../CommentService/CommentServiceImpl.java | 24 +++++++------- .../web/controller/CommentController.java | 7 ----- .../stepperbackend/web/dto/CommentDto.java | 10 ++++++ 7 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/example/stepperbackend/converter/CommentConverter.java b/src/main/java/com/example/stepperbackend/converter/CommentConverter.java index 132c9ec..b871cee 100644 --- a/src/main/java/com/example/stepperbackend/converter/CommentConverter.java +++ b/src/main/java/com/example/stepperbackend/converter/CommentConverter.java @@ -1,11 +1,16 @@ package com.example.stepperbackend.converter; +import com.example.stepperbackend.domain.BadgeCategory; import com.example.stepperbackend.domain.Comment; import com.example.stepperbackend.domain.Member; import com.example.stepperbackend.domain.Post; +import com.example.stepperbackend.domain.mapping.Badge; +import com.example.stepperbackend.web.dto.BadgeDto; import com.example.stepperbackend.web.dto.CommentDto; import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; public class CommentConverter { @@ -31,6 +36,32 @@ public static CommentDto.CommentResponseDto toDto(Comment comment) { .build(); } + public static CommentDto.ReplyResponseDto toReplyDto(Comment comment) { + return CommentDto.ReplyResponseDto.builder() + .postId(comment.getPost().getId()) + .parentCommentId(comment.getParentComment().getId()) + .content(comment.getContent()) + .anonymous(comment.isAnonymous()) + .build(); + + } + + public static CommentDto.CommentResponseDto toCommentResponseDto(List replyList, Comment comment){ + + List replyDtoList = replyList.stream() + .map(CommentConverter::toReplyDto).collect(Collectors.toList()); + + return CommentDto.CommentResponseDto.builder() + .postId(comment.getPost().getId()) + .commentId(comment.getId()) + .memberName(comment.getAnonymousName()) + .profileImage(comment.getMember().getProfileImage()) + .content(comment.getContent()) + .dateTime(comment.getCreatedAt()) + .replyList(replyDtoList) + .build(); + } + public static Comment toReplyEntity(CommentDto.ReplyRequestDto dto, Member member, Post post, String memberName, Comment parentComment) { return Comment.builder() .content(dto.getContent()) diff --git a/src/main/java/com/example/stepperbackend/domain/Comment.java b/src/main/java/com/example/stepperbackend/domain/Comment.java index 0d9007a..e3a25ef 100644 --- a/src/main/java/com/example/stepperbackend/domain/Comment.java +++ b/src/main/java/com/example/stepperbackend/domain/Comment.java @@ -39,7 +39,6 @@ public class Comment extends BaseEntity { @JoinColumn(name = "post_id") private Post post; - @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_comment_id") private Comment parentComment; diff --git a/src/main/java/com/example/stepperbackend/repository/CommentRepository.java b/src/main/java/com/example/stepperbackend/repository/CommentRepository.java index 7f22cef..e3bbdd3 100644 --- a/src/main/java/com/example/stepperbackend/repository/CommentRepository.java +++ b/src/main/java/com/example/stepperbackend/repository/CommentRepository.java @@ -21,9 +21,9 @@ public interface CommentRepository extends JpaRepository { List findByMember(Member member); - @Query("SELECT e FROM Comment e WHERE e.post.id = :postId") - List findByPostId(@Param("postId") Long postId); + List findAllByPostId(@Param("postId") Long postId); // 특정 댓글의 대댓글들을 조회하는 메소드 + @Query("SELECT e FROM Comment e WHERE e.parentComment = :parentComment") List findByParentComment(Comment parentComment); } diff --git a/src/main/java/com/example/stepperbackend/service/CommentService/CommentService.java b/src/main/java/com/example/stepperbackend/service/CommentService/CommentService.java index a7270ef..0ad3cbf 100644 --- a/src/main/java/com/example/stepperbackend/service/CommentService/CommentService.java +++ b/src/main/java/com/example/stepperbackend/service/CommentService/CommentService.java @@ -11,8 +11,6 @@ public interface CommentService { List getComment(Long postId); CommentDto.CommentResponseDto writeReply(CommentDto.ReplyRequestDto request, String email); - - List getReply(Long parentCommentId); } diff --git a/src/main/java/com/example/stepperbackend/service/CommentService/CommentServiceImpl.java b/src/main/java/com/example/stepperbackend/service/CommentService/CommentServiceImpl.java index e94bc30..11b5fca 100644 --- a/src/main/java/com/example/stepperbackend/service/CommentService/CommentServiceImpl.java +++ b/src/main/java/com/example/stepperbackend/service/CommentService/CommentServiceImpl.java @@ -1,9 +1,11 @@ package com.example.stepperbackend.service.CommentService; import com.example.stepperbackend.apiPayload.code.status.ErrorStatus; +import com.example.stepperbackend.apiPayload.exception.handler.BadgeHandler; import com.example.stepperbackend.apiPayload.exception.handler.CommentHandler; import com.example.stepperbackend.apiPayload.exception.handler.MemberHandler; import com.example.stepperbackend.apiPayload.exception.handler.PostHandler; +import com.example.stepperbackend.converter.BadgeConverter; import com.example.stepperbackend.converter.CommentConverter; import com.example.stepperbackend.converter.PostConverter; import com.example.stepperbackend.domain.Comment; @@ -12,6 +14,7 @@ import com.example.stepperbackend.repository.CommentRepository; import com.example.stepperbackend.repository.MemberRepository; import com.example.stepperbackend.repository.PostRepository; +import com.example.stepperbackend.web.dto.BadgeDto; import com.example.stepperbackend.web.dto.CommentDto; import com.example.stepperbackend.web.dto.PostDto; import lombok.RequiredArgsConstructor; @@ -112,21 +115,16 @@ private String getAnonymousName(List commentList, Long postId) { @Override public List getComment(Long postId) { - List commentList = commentRepository.findByPostId(postId); + List commentList = commentRepository.findAllByPostId(postId).stream().collect(Collectors.toList()); - if (commentList.isEmpty()) { - throw new CommentHandler(ErrorStatus.COMMENTS_NOT_FOUND); - } - - return commentList.stream().map(CommentConverter::toDto).collect(Collectors.toList()); - } + List commentResponseList = commentList.stream() + .map(comment -> { + List replyList = commentRepository.findByParentComment(comment); + comment.setReplies(replyList); + return CommentConverter.toCommentResponseDto(replyList, comment); + }).collect(Collectors.toList()); - @Override - public List getReply(Long parentCommentId) { - Comment parentComment = commentRepository.findById(parentCommentId) - .orElseThrow(() -> new RuntimeException("Parent comment not found")); - List replyList = commentRepository.findByParentComment(parentComment); - return replyList.stream().map(CommentConverter::toDto).collect(Collectors.toList()); + return commentResponseList; } } diff --git a/src/main/java/com/example/stepperbackend/web/controller/CommentController.java b/src/main/java/com/example/stepperbackend/web/controller/CommentController.java index 0e64185..2b716b9 100644 --- a/src/main/java/com/example/stepperbackend/web/controller/CommentController.java +++ b/src/main/java/com/example/stepperbackend/web/controller/CommentController.java @@ -40,13 +40,6 @@ public ApiResponse> getComment(@PathVariable return ApiResponse.onSuccess(response); } - @Operation(summary = "대댓글 조회 API", description = "대댓글 조회") - @GetMapping("/{parentCommentId}/reply") - public ResponseEntity> getReply(@PathVariable Long parentCommentId) { - List reply = commentService.getReply(parentCommentId); - return ResponseEntity.ok(reply); - } - @Operation(summary = "대댓글 작성 API", description = "대댓글 작성") @PostMapping("/reply") diff --git a/src/main/java/com/example/stepperbackend/web/dto/CommentDto.java b/src/main/java/com/example/stepperbackend/web/dto/CommentDto.java index ed29350..02c20cf 100644 --- a/src/main/java/com/example/stepperbackend/web/dto/CommentDto.java +++ b/src/main/java/com/example/stepperbackend/web/dto/CommentDto.java @@ -7,6 +7,7 @@ import lombok.NoArgsConstructor; import java.time.LocalDateTime; +import java.util.List; public class CommentDto { @@ -28,6 +29,7 @@ public static class CommentResponseDto { private String memberName; private String content; private LocalDateTime dateTime; + private List replyList; } @Getter @@ -38,4 +40,12 @@ public static class ReplyRequestDto { private boolean anonymous; } + @Builder + @Getter + public static class ReplyResponseDto { + private Long postId; + private Long parentCommentId; + private String content; + private boolean anonymous; + } } From a3d0256e537dd375c6cd53096c5892cc4d0e7345 Mon Sep 17 00:00:00 2001 From: jamie Date: Thu, 15 Aug 2024 20:51:22 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[FEAT]=20=EB=8C=93=EA=B8=80=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/stepperbackend/domain/Comment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/stepperbackend/domain/Comment.java b/src/main/java/com/example/stepperbackend/domain/Comment.java index e3a25ef..ba2d508 100644 --- a/src/main/java/com/example/stepperbackend/domain/Comment.java +++ b/src/main/java/com/example/stepperbackend/domain/Comment.java @@ -43,6 +43,6 @@ public class Comment extends BaseEntity { @JoinColumn(name = "parent_comment_id") private Comment parentComment; - @OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL) private List replies = new ArrayList<>(); }