From 3d47b15f1a3145ce73d3759b9ce6a9f45e09ae24 Mon Sep 17 00:00:00 2001 From: yr0202 Date: Thu, 23 Jan 2025 11:24:30 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Feat:=20=EB=8C=93=EA=B8=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommentController.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/palbang/unsemawang/community/controller/CommentController.java b/src/main/java/com/palbang/unsemawang/community/controller/CommentController.java index a9cba5aa..07ee3b23 100644 --- a/src/main/java/com/palbang/unsemawang/community/controller/CommentController.java +++ b/src/main/java/com/palbang/unsemawang/community/controller/CommentController.java @@ -3,6 +3,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -94,4 +95,23 @@ public ResponseEntity updateCommentByPostId( return ResponseEntity.status(HttpStatus.OK).build(); } + + @Operation( + description = "내가 작성한 댓글을 삭제하는 기능입니다", + summary = "댓글 삭제" + ) + @DeleteMapping("/{commentId}") + public ResponseEntity deleteCommentByPostId( + @AuthenticationPrincipal CustomOAuth2User auth, + @PathVariable Long postId, + @PathVariable Long commentId) { + + if (auth == null || auth.getId() == null) { + throw new GeneralException(ResponseCode.EMPTY_TOKEN); + } + + commentService.deleteComment(postId, commentId, auth.getId()); + + return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); + } } \ No newline at end of file From ebb68317bed02e71068420c39d0015ef5b4d7e9e Mon Sep 17 00:00:00 2001 From: yr0202 Date: Thu, 23 Jan 2025 11:27:32 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Feat:=20=EB=8C=93=EA=B8=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?=EB=B0=8F=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../community/service/CommentService.java | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/palbang/unsemawang/community/service/CommentService.java b/src/main/java/com/palbang/unsemawang/community/service/CommentService.java index 33085732..5d7fd30c 100644 --- a/src/main/java/com/palbang/unsemawang/community/service/CommentService.java +++ b/src/main/java/com/palbang/unsemawang/community/service/CommentService.java @@ -106,21 +106,50 @@ public void registerComment(Long postId, CommentRegisterRequest request, String commentRepository.save(comment); } - public void updateComment(Long postId, Long commentUd, CommentUpdateRequest request, String memberId) { - Member member = memberRepository.findById(memberId) + public void updateComment(Long postId, Long commentId, CommentUpdateRequest request, String memberId) { + Member member = validateMember(memberId); + + validatePost(postId); + + Comment comment = validateComment(commentId); + + validateCommentOwnerMatch(member.getId(), comment.getMember().getId()); + + comment.updateComment(request.getContent(), request.getIsAnonymous()); // dirty-check -> save() 필요없음 + } + + public void deleteComment(Long postId, Long commentId, String memberId) { + Member member = validateMember(memberId); + + validatePost(postId); + + Comment comment = validateComment(commentId); + + validateCommentOwnerMatch(member.getId(), comment.getMember().getId()); + + comment.deleteComment(); + } + + // 유효성 검증 메서드 + private Member validateMember(String memberId) { + return memberRepository.findById(memberId) .orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_MEMBER_ID)); + } + private void validatePost(Long postId) { postRepository.findByIdAndIsDeletedFalse(postId) .orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_POST)); + } - Comment comment = commentRepository.findById(commentUd) + private Comment validateComment(Long commentId) { + return commentRepository.findById(commentId) .orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_COMMENT)); + } - // 로그인한 사용자와 댓글 작성자가 일치하는지 확인 - if (!Objects.equals(comment.getMember().getId(), member.getId())) { + // 로그인한 사용자와 댓글 작성자가 일치하는지 확인 + private void validateCommentOwnerMatch(String commentOwnerId, String memberId) { + if (!Objects.equals(commentOwnerId, memberId)) { throw new GeneralException(ResponseCode.NOT_AUTHORIZED_COMMENT_MODIFICATION); } - - comment.updateComment(request.getContent(), request.getIsAnonymous()); // dirty-check -> save() 필요없음 } } \ No newline at end of file