From 1b6706f9d075e6aed12cc783d6a347d5383a26fa Mon Sep 17 00:00:00 2001 From: moonyaeyoon Date: Fri, 26 Jul 2024 14:12:08 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat=20:=20post=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 1 + .../comment/service/CommentService.java | 1 + .../post/controller/PostController.java | 15 +++++++++++++ .../server/post/service/PostService.java | 21 +++++++++++++++++++ .../meltingpot/server/util/ResponseCode.java | 3 +++ 5 files changed, 41 insertions(+) diff --git a/src/main/java/meltingpot/server/comment/controller/CommentController.java b/src/main/java/meltingpot/server/comment/controller/CommentController.java index 9c82896..fef6e37 100644 --- a/src/main/java/meltingpot/server/comment/controller/CommentController.java +++ b/src/main/java/meltingpot/server/comment/controller/CommentController.java @@ -6,6 +6,7 @@ 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; diff --git a/src/main/java/meltingpot/server/comment/service/CommentService.java b/src/main/java/meltingpot/server/comment/service/CommentService.java index 4b2b359..4a13105 100644 --- a/src/main/java/meltingpot/server/comment/service/CommentService.java +++ b/src/main/java/meltingpot/server/comment/service/CommentService.java @@ -133,6 +133,7 @@ public ResponseCode updateComment(CommentCreateRequest updateCommentDTO, Account // } public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) { + Post post = findPostById(postId); List commentDetailDTOs = new ArrayList<>(); int count = 0; Long parentCursor = null; diff --git a/src/main/java/meltingpot/server/post/controller/PostController.java b/src/main/java/meltingpot/server/post/controller/PostController.java index 389ad94..9a37962 100644 --- a/src/main/java/meltingpot/server/post/controller/PostController.java +++ b/src/main/java/meltingpot/server/post/controller/PostController.java @@ -5,6 +5,9 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; +import meltingpot.server.domain.entity.comment.Comment; +import meltingpot.server.domain.entity.post.Post; +import meltingpot.server.domain.repository.CommentRepository; import meltingpot.server.post.dto.PostDetailResponse; import meltingpot.server.post.dto.PostsListResponse; import org.springframework.http.ResponseEntity; @@ -68,4 +71,16 @@ public ResponseEntity> getPostDetail(@CurrentUs return ResponseData.toResponseEntity(ResponseCode.POST_NOT_FOUND, null); } } + + + @DeleteMapping("/{postId}") + @Operation(summary = "커뮤니티 글 삭제", description = "postId로 커뮤니티 글 삭제") + public ResponseEntity deletePost(@CurrentUser Account account,@PathVariable Long postId) { + try { + return ResponseData.toResponseEntity(postService.deletePost(postId, account)); + } catch (NoSuchElementException e) { + return ResponseData.toResponseEntity(ResponseCode.POST_DELETE_FAIL); + } + } + } diff --git a/src/main/java/meltingpot/server/post/service/PostService.java b/src/main/java/meltingpot/server/post/service/PostService.java index e2622b8..4391b32 100644 --- a/src/main/java/meltingpot/server/post/service/PostService.java +++ b/src/main/java/meltingpot/server/post/service/PostService.java @@ -92,6 +92,27 @@ public PostsListResponse getPostsList(Account account, PostType postType, Long return PostsListResponse.from(posts, nextCursor, isLast); } + /*post 삭제하기*/ + public ResponseCode deletePost(Long postId, Account account){ + Post post = findPostById(postId); + Account postAccount = findAccountById(post.getAccount().getId()); + + // 게시물에 연관된 이미지 삭제 + if (!post.getPostImages().isEmpty()) { + postImageRepository.deleteAll(post.getPostImages()); + } + + // 게시물에 연관된 댓글 삭제 + if (!post.getComments().isEmpty()) { + commentRepository.deleteAll(post.getComments()); + } + + // 게시물 삭제 + postRepository.delete(post); + + return ResponseCode.POST_DELETE_SUCCESS; + + } private Account findAccountById(Long accountId) { diff --git a/src/main/java/meltingpot/server/util/ResponseCode.java b/src/main/java/meltingpot/server/util/ResponseCode.java index 7ae42d1..9c24042 100644 --- a/src/main/java/meltingpot/server/util/ResponseCode.java +++ b/src/main/java/meltingpot/server/util/ResponseCode.java @@ -29,6 +29,7 @@ public enum ResponseCode { SOCKET_TOKEN_GET_SUCCESS(OK, "소켓 토큰 조회 성공"), POST_LIST_FETCH_SUCCESS(OK, "게시글 목록 불러오기 성공"), POST_DETAIL_FETCH_SUCCEESS(OK, "게시글 내용 불러오기 성공"), + POST_DELETE_SUCCESS(OK, "게시글 삭제하기 성공"), CHAT_ROOM_USER_DELETE_SUCCESS(OK, "채팅방 나가기 성공"), CHAT_ALARM_UPDATE_SUCCESS(OK, "채팅 알림 설정 수정 성공"), @@ -90,9 +91,11 @@ public enum ResponseCode { DESTINATION_NOT_VALID(BAD_REQUEST, "잘못된 목적지로 접근하였습니다"), COMMENT_CREATE_FAIL(BAD_REQUEST, "댓글 작성 실패 "), COMMENT_UPDATE_FAIL(BAD_REQUEST, "댓글 수정 실패 "), + COMMENT_DELETE_FAIL(BAD_REQUEST,"댓글 삭제 실패"), READ_COMMENT_FAIL(BAD_REQUEST, "댓글 불러오기 실패 "), POST_CREATE_FAIL(BAD_REQUEST, "게시글 작성 실패"), POST_UPDATE_FAIL(BAD_REQUEST,"게시글 수정 실패"), + POST_DELETE_FAIL(BAD_REQUEST,"게시글 삭제 실패"), REPORT_CREATE_FAIL(BAD_REQUEST, "신고 작성 실패"), AREA_FETCH_FAILED(BAD_REQUEST, "지역 조회 실패"), AREA_FETCH_FAILED_NOT_SERVICE_AREA(BAD_REQUEST, "현재 좌표 조회는 국내에서만 사용 가능합니다"), From af207a54c73aa5d536af58fbed1a8604b8ef0791 Mon Sep 17 00:00:00 2001 From: moonyaeyoon Date: Fri, 26 Jul 2024 14:24:52 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix=20:=20cursor=EA=B0=92=20null=20?= =?UTF-8?q?=ED=97=88=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../meltingpot/server/comment/service/CommentService.java | 4 +++- .../meltingpot/server/post/controller/PostController.java | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/meltingpot/server/comment/service/CommentService.java b/src/main/java/meltingpot/server/comment/service/CommentService.java index 4a13105..cb103ab 100644 --- a/src/main/java/meltingpot/server/comment/service/CommentService.java +++ b/src/main/java/meltingpot/server/comment/service/CommentService.java @@ -131,7 +131,8 @@ public ResponseCode updateComment(CommentCreateRequest updateCommentDTO, Account // // return CommentsListResponse.from(commentDetailDTOs,nextCursor,isLast); // } - + /* 댓글 목록 불러오기 */ + @Transactional(readOnly = true) public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) { Post post = findPostById(postId); List commentDetailDTOs = new ArrayList<>(); @@ -193,6 +194,7 @@ public CommentsListResponse getCommentsList(Account account, Long postId, Long c } + /* 댓글 삭제하기 */ private Comment findCommentById(Long commentId) { diff --git a/src/main/java/meltingpot/server/post/controller/PostController.java b/src/main/java/meltingpot/server/post/controller/PostController.java index 9a37962..5d917f0 100644 --- a/src/main/java/meltingpot/server/post/controller/PostController.java +++ b/src/main/java/meltingpot/server/post/controller/PostController.java @@ -54,7 +54,7 @@ public ResponseEntity updatePost(@CurrentUser Account account,@Pat @ApiResponse(responseCode = "OK", description = "커뮤니티 글 목록 조회 성공"), @ApiResponse(responseCode = "NOT_FOUND", description = "커뮤니티 글을 찾을 수 없습니다") }) - public ResponseEntity> getPostList(@CurrentUser Account account, @PathVariable PostType postType , @RequestParam(name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) { + public ResponseEntity> getPostList(@CurrentUser Account account, @PathVariable PostType postType , @RequestParam(required = false, name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) { try { return ResponseData.toResponseEntity(ResponseCode.POST_LIST_FETCH_SUCCESS, postService.getPostsList(account,postType, cursor, pageSize)); } catch (NoSuchElementException e) { @@ -64,7 +64,7 @@ public ResponseEntity> getPostList(@CurrentUser @GetMapping("/{postId}") @Operation(summary = "커뮤니티 글 내용 조회", description = "postId로 커뮤니티 글 내용을 조회합니다.") - public ResponseEntity> getPostDetail(@CurrentUser Account account, @PathVariable Long postId, @RequestParam(name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) { + public ResponseEntity> getPostDetail(@CurrentUser Account account, @PathVariable Long postId, @RequestParam(required = false, name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) { try{ return ResponseData.toResponseEntity(ResponseCode.POST_DETAIL_FETCH_SUCCEESS,postService.getPostDetail(postId,cursor, pageSize)); }catch (NoSuchElementException e) { From e1376285eeb789ee4a6d3a95112f500036fe38ab Mon Sep 17 00:00:00 2001 From: moonyaeyoon Date: Fri, 26 Jul 2024 21:59:50 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat=20:=20comment=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 3 +- .../comment/service/CommentService.java | 28 +++++++++++++++++-- .../server/domain/entity/comment/Comment.java | 4 +++ .../meltingpot/server/util/ResponseCode.java | 1 + 4 files changed, 31 insertions(+), 5 deletions(-) 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, "댓글 삭제하기 성공"), From 352bfc817ea69c9e3611035ef276b74fdbba0e06 Mon Sep 17 00:00:00 2001 From: moonyaeyoon Date: Sat, 27 Jul 2024 00:01:01 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat=20:=20=EB=8C=93=EA=B8=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 13 ++++++++++- .../comment/dto/CommentsListResponse.java | 4 ++-- .../comment/service/CommentService.java | 22 ++++++++++++++----- .../server/domain/entity/comment/Comment.java | 4 ++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/main/java/meltingpot/server/comment/controller/CommentController.java b/src/main/java/meltingpot/server/comment/controller/CommentController.java index f2a2799..b842ae8 100644 --- a/src/main/java/meltingpot/server/comment/controller/CommentController.java +++ b/src/main/java/meltingpot/server/comment/controller/CommentController.java @@ -56,11 +56,22 @@ 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(postId,cursor,pageSize)); + return ResponseData.toResponseEntity(ResponseCode.READ_COMMENTS_LIST_SUCCESS, commentService.getCommentsList(account, postId, cursor,pageSize)); }catch (NoSuchElementException e) { return ResponseData.toResponseEntity(ResponseCode.READ_COMMENT_FAIL, null); } } + + @Operation (summary = "댓글 삭제하기") + @DeleteMapping("{commentId}") + public ResponseEntity createComment( @CurrentUser Account account, @PathVariable Long commentId) { + try { + return ResponseData.toResponseEntity(commentService.deleteComment(commentId,account)); + } catch (NoSuchElementException e) { + return ResponseData.toResponseEntity(ResponseCode.COMMENT_DELETE_FAIL); + } + } + } diff --git a/src/main/java/meltingpot/server/comment/dto/CommentsListResponse.java b/src/main/java/meltingpot/server/comment/dto/CommentsListResponse.java index 4b2e048..589a6f0 100644 --- a/src/main/java/meltingpot/server/comment/dto/CommentsListResponse.java +++ b/src/main/java/meltingpot/server/comment/dto/CommentsListResponse.java @@ -37,9 +37,9 @@ public static CommentDetail from(Comment comment) { return CommentDetail.builder() .commentId(comment.getId()) .parentId(comment.getParent() != null ? comment.getParent().getId() : null) - .userId(comment.getAccount().getId()) + .userId(comment.getAccount()!= null ? comment.getAccount().getId() : null) .content(comment.getContent()) - .name(comment.getAccount().getName()) + .name(comment.getAccount() != null? comment.getAccount().getName() : null) .isAnonymous(comment.getIsAnonymous()) .imageUrl(comment.getCommentImage() != null ? comment.getCommentImage().getImageUrl() : null) .updatedAt(comment.getUpdatedAt()) diff --git a/src/main/java/meltingpot/server/comment/service/CommentService.java b/src/main/java/meltingpot/server/comment/service/CommentService.java index 448be96..9cc4049 100644 --- a/src/main/java/meltingpot/server/comment/service/CommentService.java +++ b/src/main/java/meltingpot/server/comment/service/CommentService.java @@ -133,7 +133,7 @@ public ResponseCode updateComment(CommentCreateRequest updateCommentDTO, Account // } /* 댓글 목록 불러오기 */ @Transactional(readOnly = true) - public CommentsListResponse getCommentsList(Long postId, Long cursor, int pageSize) { + public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) { Post post = findPostById(postId); List commentDetailDTOs = new ArrayList<>(); int count = 0; @@ -195,12 +195,12 @@ public CommentsListResponse getCommentsList(Long postId, Long cursor, int pageSi /* 댓글 삭제하기 */ - + @Transactional public ResponseCode deleteComment(Long commentId, Account account) { Comment comment = findCommentById(commentId); if (!comment.getAccount().getId().equals(account.getId())) { - throw new ResponseStatusException(HttpStatus.FORBIDDEN, "삭제된 댓글입니다."); + throw new ResponseStatusException(HttpStatus.FORBIDDEN, "삭제 권한이 없습니다."); } if (comment.getParent() == null) { comment.setContent("삭제된 댓글입니다."); @@ -208,17 +208,27 @@ public ResponseCode deleteComment(Long commentId, Account account) { comment.setIsAnonymous(true); if (comment.getCommentImage() != null) { - comment.setCommentImage(null); // 관계 제거 - commentImageRepository.delete(comment.getCommentImage()); // 이미지 엔티티 삭제 + CommentImage commentImage = comment.getCommentImage(); + comment.setCommentImage(null); + commentImageRepository.delete(commentImage); + } + // 자식 댓글이 남아있지 않으면 부모 댓글도 삭제 + if (comment.getChildren().isEmpty()) { + commentRepository.delete(comment); } + } else { + Comment parentComment = comment.getParent(); commentRepository.delete(comment); + parentComment.getChildren().remove(comment); + if (parentComment.getChildren().isEmpty()) { + commentRepository.delete(parentComment); + } } return ResponseCode.COMMENT_DELETE_SUCCESS; } - private Comment findCommentById(Long commentId) { return commentRepository.findById(commentId) .orElseThrow(() -> new RuntimeException("댓글을 찾을 수 없습니다.")); 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 41f50c7..2c763db 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 setAccount(Account account) { + this.account = account; + } + public void setIsAnonymous (Boolean isAnonymous) { this.isAnonymous = isAnonymous; }