diff --git a/src/main/java/meltingpot/server/comment/controller/CommentController.java b/src/main/java/meltingpot/server/comment/controller/CommentController.java index fef6e37..f2a2799 100644 --- a/src/main/java/meltingpot/server/comment/controller/CommentController.java +++ b/src/main/java/meltingpot/server/comment/controller/CommentController.java @@ -6,7 +6,6 @@ import meltingpot.server.comment.dto.CommentsListResponse; import meltingpot.server.comment.service.CommentService; import meltingpot.server.domain.entity.Account; -import meltingpot.server.domain.entity.comment.Comment; import meltingpot.server.util.CurrentUser; import meltingpot.server.util.ResponseCode; import meltingpot.server.util.ResponseData; @@ -57,7 +56,7 @@ public ResponseEntity> getCommentsList (@Curr @RequestParam(required = false) Long cursor, @RequestParam(defaultValue = "10") int pageSize){ try{ - return ResponseData.toResponseEntity(ResponseCode.READ_COMMENTS_LIST_SUCCESS, commentService.getCommentsList(account,postId,cursor,pageSize)); + return ResponseData.toResponseEntity(ResponseCode.READ_COMMENTS_LIST_SUCCESS, commentService.getCommentsList(postId,cursor,pageSize)); }catch (NoSuchElementException e) { return ResponseData.toResponseEntity(ResponseCode.READ_COMMENT_FAIL, null); } diff --git a/src/main/java/meltingpot/server/comment/service/CommentService.java b/src/main/java/meltingpot/server/comment/service/CommentService.java index cb103ab..448be96 100644 --- a/src/main/java/meltingpot/server/comment/service/CommentService.java +++ b/src/main/java/meltingpot/server/comment/service/CommentService.java @@ -16,11 +16,11 @@ import org.springframework.data.domain.PageRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - +import org.springframework.web.server.ResponseStatusException; import java.util.*; -import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -133,7 +133,7 @@ public ResponseCode updateComment(CommentCreateRequest updateCommentDTO, Account // } /* 댓글 목록 불러오기 */ @Transactional(readOnly = true) - public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) { + public CommentsListResponse getCommentsList(Long postId, Long cursor, int pageSize) { Post post = findPostById(postId); List commentDetailDTOs = new ArrayList<>(); int count = 0; @@ -196,6 +196,28 @@ public CommentsListResponse getCommentsList(Account account, Long postId, Long c /* 댓글 삭제하기 */ + public ResponseCode deleteComment(Long commentId, Account account) { + Comment comment = findCommentById(commentId); + + if (!comment.getAccount().getId().equals(account.getId())) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN, "삭제된 댓글입니다."); + } + if (comment.getParent() == null) { + comment.setContent("삭제된 댓글입니다."); + comment.setAccount(null); + comment.setIsAnonymous(true); + + if (comment.getCommentImage() != null) { + comment.setCommentImage(null); // 관계 제거 + commentImageRepository.delete(comment.getCommentImage()); // 이미지 엔티티 삭제 + } + + } else { + commentRepository.delete(comment); + } + return ResponseCode.COMMENT_DELETE_SUCCESS; + } + private Comment findCommentById(Long commentId) { return commentRepository.findById(commentId) diff --git a/src/main/java/meltingpot/server/domain/entity/comment/Comment.java b/src/main/java/meltingpot/server/domain/entity/comment/Comment.java index 8646bef..41f50c7 100644 --- a/src/main/java/meltingpot/server/domain/entity/comment/Comment.java +++ b/src/main/java/meltingpot/server/domain/entity/comment/Comment.java @@ -80,6 +80,10 @@ public void setContent(String content) { this.content = content; } + public void setIsAnonymous (Boolean isAnonymous) { + this.isAnonymous = isAnonymous; + } + public Comment getParent() { return parent;} diff --git a/src/main/java/meltingpot/server/util/ResponseCode.java b/src/main/java/meltingpot/server/util/ResponseCode.java index 9c24042..9aa2556 100644 --- a/src/main/java/meltingpot/server/util/ResponseCode.java +++ b/src/main/java/meltingpot/server/util/ResponseCode.java @@ -52,6 +52,7 @@ public enum ResponseCode { UPDATE_POST_SUCCESS(OK,"게시물 수정 성공"), UPDATE_COMMENT_SUCCESS(OK, "댓글 수정 성공"), READ_COMMENTS_LIST_SUCCESS(OK,"댓글 목록 불러오기 성공"), + COMMENT_DELETE_SUCCESS(OK, "댓글 삭제하기 성공"),