Skip to content

Commit 5c8dc60

Browse files
committed
Feat: 댓글 삭제 API 구현
1 parent 14ff8b6 commit 5c8dc60

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/main/java/com/back/domain/board/controller/CommentController.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,20 @@ public ResponseEntity<RsData<CommentResponse>> updateComment(
5050
response
5151
));
5252
}
53+
54+
// 댓글 삭제
55+
@DeleteMapping("/{commentId}")
56+
public ResponseEntity<RsData<Void>> deleteComment(
57+
@PathVariable Long postId,
58+
@PathVariable Long commentId,
59+
@AuthenticationPrincipal CustomUserDetails user
60+
) {
61+
commentService.deleteComment(postId, commentId, user.getUserId());
62+
return ResponseEntity
63+
.status(HttpStatus.OK)
64+
.body(RsData.success(
65+
"댓글이 삭제되었습니다.",
66+
null
67+
));
68+
}
5369
}

src/main/java/com/back/domain/board/service/CommentService.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,28 @@ public CommentResponse updateComment(Long postId, Long commentId, CommentRequest
7474
// 응답 반환
7575
return CommentResponse.from(comment);
7676
}
77+
78+
/**
79+
* 댓글 삭제 서비스
80+
* 1. Post 조회
81+
* 2. Comment 조회
82+
* 3. 작성자 검증
83+
* 4. Comment 삭제
84+
*/
85+
public void deleteComment(Long postId, Long commentId, Long userId) {
86+
// Post 조회
87+
Post post = postRepository.findById(postId)
88+
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
89+
90+
// Comment 조회
91+
Comment comment = commentRepository.findById(commentId)
92+
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));
93+
94+
// 작성자 검증
95+
if (!comment.getUser().getId().equals(userId)) {
96+
throw new CustomException(ErrorCode.COMMENT_NO_PERMISSION);
97+
}
98+
99+
commentRepository.delete(comment);
100+
}
77101
}

0 commit comments

Comments
 (0)