diff --git a/src/main/java/likelion/MZConnent/api/review/CommentController.java b/src/main/java/likelion/MZConnent/api/review/CommentController.java index 87c4eb6..8537a0a 100644 --- a/src/main/java/likelion/MZConnent/api/review/CommentController.java +++ b/src/main/java/likelion/MZConnent/api/review/CommentController.java @@ -27,4 +27,10 @@ public ResponseEntity saveComment(@RequestBody Map> deleteComment(@PathVariable("commentId") Long commentId, @AuthenticationPrincipal UserPrinciple userPrinciple) { + commentService.deleteComment(userPrinciple.getEmail(), commentId); + return ResponseEntity.ok(Map.of("message", "댓글 삭제 성공")); + } + } diff --git a/src/main/java/likelion/MZConnent/service/review/CommentService.java b/src/main/java/likelion/MZConnent/service/review/CommentService.java index d0b4a2d..aa53d1e 100644 --- a/src/main/java/likelion/MZConnent/service/review/CommentService.java +++ b/src/main/java/likelion/MZConnent/service/review/CommentService.java @@ -15,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; +import java.util.Optional; @Service @Slf4j @@ -24,6 +25,8 @@ public class CommentService { private final ReviewCommentRepository reviewCommentRepository; private final ReviewRepository reviewRepository; + + // 후기 댓글 작성 @Transactional public SaveCommentResponse saveComment(String email, Long reviewId, String content) { Member member = findMemberByEmail(email); @@ -41,6 +44,25 @@ public SaveCommentResponse saveComment(String email, Long reviewId, String conte return SaveCommentResponse.builder().comment(comment).build(); } + // 후기 댓글 삭제 + @Transactional + public void deleteComment(String email, Long commentId) { + ReviewComment comment = findCommentById(commentId); + Member member = findMemberByEmail(email); + Review review = findReviewById(comment.getReview().getReviewId()); + + member.getReviewComments().remove(comment); + review.getReviewComments().remove(comment); + reviewCommentRepository.delete(comment); + } + + private ReviewComment findCommentById(Long commentId) { + return reviewCommentRepository.findById(commentId).orElseThrow(() -> { + log.info("해당 후기 댓글이 존재하지 않음."); + return new IllegalArgumentException("해당 후기 댓글이 존재하지 않습니다."); + }); + } + private Review findReviewById(Long reviewId ) { return reviewRepository.findById(reviewId).orElseThrow(() -> { log.info("후기가 존재하지 않음."); @@ -48,10 +70,13 @@ private Review findReviewById(Long reviewId ) { }); } + private Member findMemberByEmail(String email) { return memberRepository.findByEmail(email).orElseThrow(() -> { log.info("회원이 존재하지 않음."); return new IllegalArgumentException("회원이 존재하지 않습니다."); }); } + + }