Skip to content

Commit

Permalink
feat: 댓글 삭제 구현 #63
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangyoo committed Feb 20, 2023
1 parent c3c732a commit 6adaba4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public ResponseEntity<DataResponseDto<?>> postComment(Member loginUser,
.commentContent(commentPostDto.getCommentContent())
.member(loginUser)
.post(postService.findPostNoneSetView(postId))
.status(Comment.CommentStatus.Alive)
.build()
,commentPostDto.getParentId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommentDetailResponseDto> children;
Expand All @@ -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<>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public List<Comment> getChildren(){
return this.children;
}

public void changeStatus(CommentStatus commentStatus) {
this.status = commentStatus;
}

@Getter
public enum CommentStatus {
Alive("생존", "1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,26 @@ public List<Comment> 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)
Expand Down

0 comments on commit 6adaba4

Please sign in to comment.