From 6e07aedd199a5d0b650ab34a062f210ae4ab326d Mon Sep 17 00:00:00 2001 From: kkw610 Date: Tue, 2 Jun 2026 15:02:37 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20resolveParentComment()=EC=97=90=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C/=EC=86=8C=EC=86=8D=20=EC=A7=88=EB=AC=B8/2dep?= =?UTF-8?q?th=20=EC=B4=88=EA=B3=BC=20=EA=B2=80=EC=A6=9D=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../question/service/QuestionService.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/example/Piroin/project/domain/question/service/QuestionService.java b/backend/src/main/java/com/example/Piroin/project/domain/question/service/QuestionService.java index a1aca36..1f0fa78 100644 --- a/backend/src/main/java/com/example/Piroin/project/domain/question/service/QuestionService.java +++ b/backend/src/main/java/com/example/Piroin/project/domain/question/service/QuestionService.java @@ -132,7 +132,7 @@ public QuestionResDTO.CommentCreateRes createComment( Question question = findQuestion(questionId); // 1. 대댓글 여부 확인: parentCommentId가 있으면 부모 댓글 조회 - QuestionComment parentComment = resolveParentComment(request.getParentCommentId()); + QuestionComment parentComment = resolveParentComment(request.getParentCommentId(), question); // 2. 댓글 엔티티 생성 및 저장 LocalDateTime now = LocalDateTime.now(); @@ -168,12 +168,27 @@ public QuestionResDTO.CommentCreateRes createComment( } // parentCommentId가 있으면 해당 댓글 조회, 없으면 null 반환 - private QuestionComment resolveParentComment(Long parentCommentId) { + private QuestionComment resolveParentComment(Long parentCommentId, Question question) { if (parentCommentId == null) { return null; } - return questionCommentRepository.findById(parentCommentId) + QuestionComment parent = questionCommentRepository.findById(parentCommentId) .orElseThrow(() -> new QuestionException(HttpStatus.NOT_FOUND, "부모 댓글을 찾을 수 없습니다.")); + + // 삭제된 댓글에는 대댓글을 달 수 없음 + if (parent.getDeletedAt() != null) { + throw new QuestionException(HttpStatus.BAD_REQUEST, "삭제된 댓글에는 대댓글을 달 수 없습니다."); + } + // 다른 질문의 댓글을 부모로 붙이는 것 방지 + if (!parent.getQuestion().getId().equals(question.getId())) { + throw new QuestionException(HttpStatus.BAD_REQUEST, "다른 질문의 댓글에는 대댓글을 달 수 없습니다."); + } + // 대댓글에 또 대댓글을 다는 것 방지 (2depth 제한) + if (parent.getParentComment() != null) { + throw new QuestionException(HttpStatus.BAD_REQUEST, "대댓글에는 대댓글을 달 수 없습니다."); + } + + return parent; } /*