Skip to content

Commit bb51dec

Browse files
committed
🐛Fix: 대댓글 깊이 1로 수정
1 parent 6fab053 commit bb51dec

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/main/java/com/be/sportizebe/domain/comment/entity/Comment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public class Comment extends BaseTimeEntity {
3636
private User user; // 작성자 (프록시 객체)
3737

3838
@ManyToOne(fetch = FetchType.LAZY)
39-
@JoinColumn(name = "parent_id")
40-
private Comment parent; // 부모 댓글 (null이면 일반 댓글)
39+
@JoinColumn(name = "parent_id") // 자기 참조
40+
private Comment parent; // 부모 댓글 (null이면 부모 댓글)
4141

4242
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
4343
@Builder.Default

src/main/java/com/be/sportizebe/domain/comment/exception/CommentErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public enum CommentErrorCode implements BaseErrorCode {
1212
COMMENT_DELETE_DENIED("C002", "댓글 삭제 권한이 없습니다.", HttpStatus.FORBIDDEN),
1313
COMMENT_UPDATE_DENIED("C003", "댓글 수정 권한이 없습니다.", HttpStatus.FORBIDDEN),
1414
INVALID_PARENT_COMMENT("C004", "유효하지 않은 부모 댓글입니다.", HttpStatus.BAD_REQUEST),
15-
COMMENT_PARENT_POST_MISMATCH("C005", "부모 댓글 다른 게시글에 속해있습니다.", HttpStatus.BAD_REQUEST);
15+
COMMENT_PARENT_POST_MISMATCH("C005", "부모 댓글 다른 게시글에 속해있습니다.", HttpStatus.BAD_REQUEST),
16+
NESTED_REPLY_NOT_ALLOWED("C006", "대댓글에는 답글을 달 수 없습니다.", HttpStatus.BAD_REQUEST);
1617

1718
private final String code;
1819
private final String message;

src/main/java/com/be/sportizebe/domain/comment/service/CommentServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ public CommentResponse createComment(Long postId, CreateCommentRequest request,
3434

3535
// 부모 댓글 조회 (대댓글인 경우)
3636
Comment parent = null; // null로 초기화
37-
if (request.parentId() != null) { // parentId가 null이 아니면 대댓글이기 때문에 부모 댓글 조회가 필요함
37+
if (request.parentId() != null) { // parentId가 null이 아니면 대댓글이기 때문에 부모 댓글 조회로 검증 필요함
3838
parent = commentRepository.findById(request.parentId())
3939
.orElseThrow(() -> new CustomException(CommentErrorCode.COMMENT_NOT_FOUND));
4040

4141
// 해당 대댓글의 부모 댓글이 같은 게시글에 속하는지 검증
4242
if(parent.getPost().getId() != post.getId()) {
4343
throw new CustomException(CommentErrorCode.COMMENT_PARENT_POST_MISMATCH);
4444
}
45+
46+
// 부모 댓글이 이미 대댓글인 경우 답글 불가 (댓글 깊이 1단계로 제한)
47+
if(parent.getParent() != null) {
48+
throw new CustomException(CommentErrorCode.NESTED_REPLY_NOT_ALLOWED);
49+
}
4550
}
4651

4752
// 댓글 생성

0 commit comments

Comments
 (0)