From 6adaba44737a3b262cea3f9630ee0e1d477d129a Mon Sep 17 00:00:00 2001 From: Sangyoo Date: Tue, 21 Feb 2023 00:14:45 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20#63?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 1 + .../comment/dto/CommentDetailResponseDto.java | 2 ++ .../pikcha/domain/comment/entity/Comment.java | 4 ++++ .../comment/service/CommentService.java | 21 ++++++++++++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java b/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java index c77c3002..ea884b48 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java @@ -50,6 +50,7 @@ public ResponseEntity> postComment(Member loginUser, .commentContent(commentPostDto.getCommentContent()) .member(loginUser) .post(postService.findPostNoneSetView(postId)) + .status(Comment.CommentStatus.Alive) .build() ,commentPostDto.getParentId()); diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/dto/CommentDetailResponseDto.java b/server/src/main/java/com/main36/pikcha/domain/comment/dto/CommentDetailResponseDto.java index 77fb73f8..dc37b180 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/dto/CommentDetailResponseDto.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/dto/CommentDetailResponseDto.java @@ -19,6 +19,7 @@ public class CommentDetailResponseDto { private String username; private String memberPicture; private String commentContent; + private Comment.CommentStatus status; private LocalDateTime createdAt; private LocalDateTime modifiedAt; private List children; @@ -31,6 +32,7 @@ public static CommentDetailResponseDto convertCommentToDto(Comment comment){ .username(comment.getMember().getUsername()) .memberPicture(comment.getMember().getPicture()) .commentContent(comment.getCommentContent()) + .status(comment.getStatus()) .createdAt(comment.getCreatedAt()) .modifiedAt(comment.getModifiedAt()) .children(new ArrayList<>()) diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/entity/Comment.java b/server/src/main/java/com/main36/pikcha/domain/comment/entity/Comment.java index e6e8b70c..ed6c924f 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/entity/Comment.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/entity/Comment.java @@ -57,6 +57,10 @@ public List getChildren(){ return this.children; } + public void changeStatus(CommentStatus commentStatus) { + this.status = commentStatus; + } + @Getter public enum CommentStatus { Alive("생존", "1"), diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java b/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java index c752940b..4ac29f7d 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java @@ -59,7 +59,26 @@ public List findComments(Post post){ return customRepository.findCommentByPost(post); } public void deleteComment(Comment comment) { - commentRepository.delete(comment); + // 자식이 있는 댓글이라면 + if(comment.getChildren().size() != 0) { + // 삭제 상태로 변경 + comment.changeStatus(Comment.CommentStatus.Dead); + } + // 자식이 없는 댓글이라면 + else { + // 삭제 가능한 조상 댓글을 전부 삭제 + commentRepository.delete(getDeletableAncestorComment(comment)); + } + } + + public Comment getDeletableAncestorComment(Comment comment) { + Comment parent = comment.getParent(); + // 1. 부모 댓글이 존재하고 2. 부모의 자식이 1개이며 3. 부모가 상태가 dead인 경우 + if(parent != null && parent.getChildren().size() == 1 && parent.getStatus() == Comment.CommentStatus.Dead){ + // 재귀로 삭제할 조상을 모두 리턴한다 + return getDeletableAncestorComment(parent); + } + return comment; } @Transactional(readOnly = true)