From a4a6faf2d60bf105e887c6e0ac5ca7b8617d0462 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Thu, 26 Sep 2024 20:24:47 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20post=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=EC=8B=9C=20hard=20delete=20=EB=90=98=EA=B2=8C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/service/CommentService.java | 4 ++-- .../growingpain/post/domain/entity/Post.java | 7 ------- .../post/dto/response/PostResponse.java | 2 -- .../post/repository/PostRepository.java | 12 ++++++------ .../growingpain/post/service/PostService.java | 19 +++++-------------- 5 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/main/java/cotato/growingpain/comment/service/CommentService.java b/src/main/java/cotato/growingpain/comment/service/CommentService.java index 01de5c4..77a9f1c 100644 --- a/src/main/java/cotato/growingpain/comment/service/CommentService.java +++ b/src/main/java/cotato/growingpain/comment/service/CommentService.java @@ -60,7 +60,7 @@ public CommentListResponse getCommentsByPostId(Long postId) { @Transactional(readOnly = true) public CommentListResponse getAllPostsAndCommentsByMemberId(Long memberId) { // 사용자가 작성한 모든 포스트 조회 - List posts = postRepository.findAllByMemberIdAndIsDeletedFalse(memberId); + List posts = postRepository.findAllByMemberId(memberId); List commentList = new ArrayList<>(); // 각 포스트의 댓글 조회 @@ -80,7 +80,7 @@ public void deleteComment(Long commentId, Long memberId) { throw new AppException(ErrorCode.ALREADY_DELETED); } - List replyComments = replyCommentRepository.findReplyCommentByCommentIdAndIsDeletedFalse(commentId); + List replyComments = replyCommentRepository.findReplyCommentByCommentId(commentId); replyCommentRepository.deleteAll(replyComments); commentLikeRepository.deleteByCommentId(commentId); diff --git a/src/main/java/cotato/growingpain/post/domain/entity/Post.java b/src/main/java/cotato/growingpain/post/domain/entity/Post.java index 052f850..f903d2c 100644 --- a/src/main/java/cotato/growingpain/post/domain/entity/Post.java +++ b/src/main/java/cotato/growingpain/post/domain/entity/Post.java @@ -53,8 +53,6 @@ public class Post extends BaseTimeEntity { private int likeCount = 0; - private boolean isDeleted = false; - @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") @JsonIgnore @@ -100,11 +98,6 @@ public void validatePostLike(Member member) { } } - public void deletePost() { - isDeleted = true; - likeCount = 0; - } - public void updatePost(String title, String content, PostCategory category) { this.title = title; this.content = content; diff --git a/src/main/java/cotato/growingpain/post/dto/response/PostResponse.java b/src/main/java/cotato/growingpain/post/dto/response/PostResponse.java index 86fb5f9..9eb46d2 100644 --- a/src/main/java/cotato/growingpain/post/dto/response/PostResponse.java +++ b/src/main/java/cotato/growingpain/post/dto/response/PostResponse.java @@ -15,7 +15,6 @@ public record PostResponse( String subCategory, int likeCount, int commentCount, - Boolean isDeleted, String memberNickname, String profileImageUrl, String memberField @@ -33,7 +32,6 @@ public static PostResponse from(Post post) { post.getSubCategory() != null ? post.getSubCategory().name() : null, post.getLikeCount(), post.getComments().size(), - post.isDeleted(), post.getMember().getName(), post.getMember().getProfileImageUrl(), post.getMember().getField() diff --git a/src/main/java/cotato/growingpain/post/repository/PostRepository.java b/src/main/java/cotato/growingpain/post/repository/PostRepository.java index 6cc29a7..24e96aa 100644 --- a/src/main/java/cotato/growingpain/post/repository/PostRepository.java +++ b/src/main/java/cotato/growingpain/post/repository/PostRepository.java @@ -10,13 +10,13 @@ public interface PostRepository extends JpaRepository { - List findAllByMemberIdAndIsDeletedFalse(Long memberId); + List findAllByMemberId(Long memberId); - @Query("SELECT p FROM Post p WHERE p.isDeleted = false AND (p.parentCategory = :category OR p.subCategory = :category)") - List findAllByCategoryAndIsDeletedFalse(@Param("category") PostCategory category); + @Query("SELECT p FROM Post p WHERE p.parentCategory = :category OR p.subCategory = :category") + List findAllByCategory(@Param("category") PostCategory category); - @Query("SELECT p FROM Post p WHERE p.isDeleted = false") - List findAllByIsDeletedFalse(); + @Query("SELECT p FROM Post p") + List findAll(); - Optional findAllByIdAndMemberIdAndIsDeletedFalse(Long postId, Long memberId); + Optional findAllByIdAndMemberId(Long postId, Long memberId); } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostService.java b/src/main/java/cotato/growingpain/post/service/PostService.java index 8aa72c5..bab4b37 100644 --- a/src/main/java/cotato/growingpain/post/service/PostService.java +++ b/src/main/java/cotato/growingpain/post/service/PostService.java @@ -49,27 +49,23 @@ public void registerPost(PostRequest request, Long memberId) throws ImageExcepti @Transactional public List getPostsByMemberId(Long memberId) { - return postRepository.findAllByMemberIdAndIsDeletedFalse(memberId); + return postRepository.findAllByMemberId(memberId); } @Transactional public List getPostsByCategory(PostCategory category) { - return postRepository.findAllByCategoryAndIsDeletedFalse(category); + return postRepository.findAllByCategory(category); } @Transactional public List getAllPostsByCategory() { - return postRepository.findAllByIsDeletedFalse(); + return postRepository.findAll(); } @Transactional public void deletePost(Long postId, Long memberId) { Post post = findByPostIdAndMemberId(postId, memberId); - if (post.isDeleted()) { - throw new AppException(ErrorCode.ALREADY_DELETED); - } - List comments = commentRepository.findAllByPostIdAndIsDeletedFalse(postId); for (Comment comment : comments) { replyCommentRepository.deleteAllByCommentId(comment.getId()); @@ -79,18 +75,13 @@ public void deletePost(Long postId, Long memberId) { postImageRepository.deleteAllByPostId(postId); postLikeRepository.deleteAllByPostId(postId); - post.deletePost(); - postRepository.save(post); + postRepository.delete(post); } @Transactional public void updatePost(Long postId, PostRequest request, Long memberId) throws ImageException { Post post = findByPostIdAndMemberId(postId, memberId); - if (post.isDeleted()) { - throw new AppException(ErrorCode.ALREADY_DELETED); - } - postImageRepository.deleteAllByPostId(postId); post.updatePost(request.title(), request.content(), request.category()); @@ -106,7 +97,7 @@ public List getPostImageByPostId(Long postId) { } private Post findByPostIdAndMemberId(Long postId, Long memberId) { - return postRepository.findAllByIdAndMemberIdAndIsDeletedFalse(postId, memberId) + return postRepository.findAllByIdAndMemberId(postId, memberId) .orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND)); } From 87e5205ee687bc2f94edfd878d83a745437ec782 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Thu, 26 Sep 2024 20:45:57 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20comment=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=EC=8B=9C=20hard=20delete=20=EB=90=98=EA=B2=8C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/domain/entity/Comment.java | 7 ------- .../comment/dto/response/CommentResponse.java | 2 -- .../comment/repository/CommentRepository.java | 12 ++++++------ .../comment/service/CommentService.java | 16 +++++----------- .../growingpain/post/service/PostService.java | 2 +- 5 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/main/java/cotato/growingpain/comment/domain/entity/Comment.java b/src/main/java/cotato/growingpain/comment/domain/entity/Comment.java index 8abf4f3..89ebdfe 100644 --- a/src/main/java/cotato/growingpain/comment/domain/entity/Comment.java +++ b/src/main/java/cotato/growingpain/comment/domain/entity/Comment.java @@ -38,8 +38,6 @@ public class Comment extends BaseTimeEntity { private int likeCount = 0; - private boolean isDeleted = false; - @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") @JsonIgnore @@ -79,9 +77,4 @@ public void validateCommentLike(Member member) { throw new AppException(ErrorCode.CANNOT_LIKE_OWN_COMMENT); } } - - public void deleteComment() { - isDeleted = true; - likeCount = 0; - } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/comment/dto/response/CommentResponse.java b/src/main/java/cotato/growingpain/comment/dto/response/CommentResponse.java index 0845bc4..b8a8a3b 100644 --- a/src/main/java/cotato/growingpain/comment/dto/response/CommentResponse.java +++ b/src/main/java/cotato/growingpain/comment/dto/response/CommentResponse.java @@ -10,7 +10,6 @@ public record CommentResponse( LocalDateTime modifiedAt, String content, Integer likeCount, - Boolean isDeleted, Long memberId, String profileImageUrl, String memberNickname, @@ -25,7 +24,6 @@ public static CommentResponse from(Comment comment) { comment.getModifiedAt(), comment.getContent(), comment.getLikeCount(), - comment.isDeleted(), comment.getMember().getId(), comment.getMember().getProfileImageUrl(), comment.getMember().getName(), diff --git a/src/main/java/cotato/growingpain/comment/repository/CommentRepository.java b/src/main/java/cotato/growingpain/comment/repository/CommentRepository.java index 31b735d..739d1c2 100644 --- a/src/main/java/cotato/growingpain/comment/repository/CommentRepository.java +++ b/src/main/java/cotato/growingpain/comment/repository/CommentRepository.java @@ -10,13 +10,13 @@ public interface CommentRepository extends JpaRepository { - @Query("SELECT new cotato.growingpain.comment.dto.response.CommentResponse(c.post.id, c.id, c.createdAt, c.modifiedAt, c.content, c.likeCount, c.isDeleted, c.member.id, c.member.profileImageUrl, c.member.name, c.member.field) FROM Comment c WHERE c.member.id = :memberId AND c.isDeleted = false") - List findByMemberIdAndIsDeletedFalse(@Param("memberId") Long memberId); + @Query("SELECT new cotato.growingpain.comment.dto.response.CommentResponse(c.post.id, c.id, c.createdAt, c.modifiedAt, c.content, c.likeCount, c.member.id, c.member.profileImageUrl, c.member.name, c.member.field) FROM Comment c WHERE c.member.id = :memberId") + List findByMemberId(@Param("memberId") Long memberId); - @Query("SELECT new cotato.growingpain.comment.dto.response.CommentResponse(c.post.id, c.id, c.createdAt, c.modifiedAt, c.content, c.likeCount, c.isDeleted, c.member.id, c.member.profileImageUrl, c.member.name, c.member.field) FROM Comment c WHERE c.post.id = :postId AND c.isDeleted = false") - List findByPostIdAndIsDeletedFalse(@Param("postId") Long postId); + @Query("SELECT new cotato.growingpain.comment.dto.response.CommentResponse(c.post.id, c.id, c.createdAt, c.modifiedAt, c.content, c.likeCount, c.member.id, c.member.profileImageUrl, c.member.name, c.member.field) FROM Comment c WHERE c.post.id = :postId") + List findByPostId(@Param("postId") Long postId); - List findAllByPostIdAndIsDeletedFalse(Long postId); + List findAllByPostId(Long postId); - Optional findAllByIdAndMemberIdAndIsDeletedFalse(Long commentId, Long memberId); + Optional findAllByIdAndMemberId(Long commentId, Long memberId); } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/comment/service/CommentService.java b/src/main/java/cotato/growingpain/comment/service/CommentService.java index 77a9f1c..32d2717 100644 --- a/src/main/java/cotato/growingpain/comment/service/CommentService.java +++ b/src/main/java/cotato/growingpain/comment/service/CommentService.java @@ -47,13 +47,13 @@ public void registerComment(CommentRegisterRequest request, Long memberId, Long @Transactional(readOnly = true) public CommentListResponse getCommentsByMemberId(Long memberId) { - List commentList = commentRepository.findByMemberIdAndIsDeletedFalse(memberId); + List commentList = commentRepository.findByMemberId(memberId); return new CommentListResponse(commentList); } @Transactional(readOnly = true) public CommentListResponse getCommentsByPostId(Long postId) { - List commentList = commentRepository.findByPostIdAndIsDeletedFalse(postId); + List commentList = commentRepository.findByPostId(postId); return new CommentListResponse(commentList); } @@ -65,7 +65,7 @@ public CommentListResponse getAllPostsAndCommentsByMemberId(Long memberId) { // 각 포스트의 댓글 조회 for (Post post : posts) { - List comments = commentRepository.findByPostIdAndIsDeletedFalse(post.getId()); + List comments = commentRepository.findByPostId(post.getId()); commentList.addAll(comments); } return new CommentListResponse(commentList); @@ -73,19 +73,13 @@ public CommentListResponse getAllPostsAndCommentsByMemberId(Long memberId) { @Transactional public void deleteComment(Long commentId, Long memberId) { - Comment comment = commentRepository.findAllByIdAndMemberIdAndIsDeletedFalse(commentId, memberId) + Comment comment = commentRepository.findAllByIdAndMemberId(commentId, memberId) .orElseThrow(() -> new AppException(ErrorCode.COMMENT_NOT_FOUND)); - if(comment.isDeleted()) { - throw new AppException(ErrorCode.ALREADY_DELETED); - } - List replyComments = replyCommentRepository.findReplyCommentByCommentId(commentId); replyCommentRepository.deleteAll(replyComments); commentLikeRepository.deleteByCommentId(commentId); - - comment.deleteComment(); - commentRepository.save(comment); + commentRepository.delete(comment); } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostService.java b/src/main/java/cotato/growingpain/post/service/PostService.java index bab4b37..8c91cfa 100644 --- a/src/main/java/cotato/growingpain/post/service/PostService.java +++ b/src/main/java/cotato/growingpain/post/service/PostService.java @@ -66,7 +66,7 @@ public List getAllPostsByCategory() { public void deletePost(Long postId, Long memberId) { Post post = findByPostIdAndMemberId(postId, memberId); - List comments = commentRepository.findAllByPostIdAndIsDeletedFalse(postId); + List comments = commentRepository.findAllByPostId(postId); for (Comment comment : comments) { replyCommentRepository.deleteAllByCommentId(comment.getId()); commentRepository.delete(comment); From 970946a08ab1908344301ff8c787d2a7a8ae69cb Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Thu, 26 Sep 2024 20:46:07 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20reply=20comment=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=EC=8B=9C=20hard=20delete=20=EB=90=98=EA=B2=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../replycomment/domain/entity/ReplyComment.java | 7 ------- .../dto/response/ReplyCommentResponse.java | 2 -- .../repository/ReplyCommentRepository.java | 8 ++++---- .../replycomment/service/ReplyCommentService.java | 11 +++-------- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/main/java/cotato/growingpain/replycomment/domain/entity/ReplyComment.java b/src/main/java/cotato/growingpain/replycomment/domain/entity/ReplyComment.java index 366ff81..3056e47 100644 --- a/src/main/java/cotato/growingpain/replycomment/domain/entity/ReplyComment.java +++ b/src/main/java/cotato/growingpain/replycomment/domain/entity/ReplyComment.java @@ -38,8 +38,6 @@ public class ReplyComment extends BaseTimeEntity { private int likeCount = 0; - private boolean isDeleted = false; - @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") @JsonIgnore @@ -85,9 +83,4 @@ public void validateReplyCommentLike(Member member) { throw new AppException(ErrorCode.CANNOT_LIKE_OWN_REPLY_COMMENT); } } - - public void deleteReplyComment() { - isDeleted = true; - likeCount = 0; - } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/replycomment/dto/response/ReplyCommentResponse.java b/src/main/java/cotato/growingpain/replycomment/dto/response/ReplyCommentResponse.java index be857d8..29ea5a3 100644 --- a/src/main/java/cotato/growingpain/replycomment/dto/response/ReplyCommentResponse.java +++ b/src/main/java/cotato/growingpain/replycomment/dto/response/ReplyCommentResponse.java @@ -10,7 +10,6 @@ public record ReplyCommentResponse( LocalDateTime modifiedAt, String content, Integer likeCount, - Boolean isDeleted, Long memberId, String profileImageUrl, String memberNickname, @@ -26,7 +25,6 @@ public static ReplyCommentResponse from(ReplyComment replyComment) { replyComment.getModifiedAt(), replyComment.getContent(), replyComment.getLikeCount(), - replyComment.isDeleted(), replyComment.getMember().getId(), replyComment.getMember().getProfileImageUrl(), replyComment.getMember().getName(), diff --git a/src/main/java/cotato/growingpain/replycomment/repository/ReplyCommentRepository.java b/src/main/java/cotato/growingpain/replycomment/repository/ReplyCommentRepository.java index af6bc06..209e4fd 100644 --- a/src/main/java/cotato/growingpain/replycomment/repository/ReplyCommentRepository.java +++ b/src/main/java/cotato/growingpain/replycomment/repository/ReplyCommentRepository.java @@ -10,14 +10,14 @@ public interface ReplyCommentRepository extends JpaRepository { - @Query("SELECT new cotato.growingpain.replycomment.dto.response.ReplyCommentResponse(r.comment.id, r.id, r.createdAt, r.modifiedAt, r.content, r.likeCount, r.isDeleted, r.member.id, r.member.profileImageUrl, r.member.name, r.member.field) FROM ReplyComment r WHERE r.comment.id = :commentId AND r.isDeleted = false") - List findByCommentIdAndIsDeletedFalse(Long commentId); + @Query("SELECT new cotato.growingpain.replycomment.dto.response.ReplyCommentResponse(r.comment.id, r.id, r.createdAt, r.modifiedAt, r.content, r.likeCount, r.member.id, r.member.profileImageUrl, r.member.name, r.member.field) FROM ReplyComment r WHERE r.comment.id = :commentId") + List findByCommentId(Long commentId); - List findReplyCommentByCommentIdAndIsDeletedFalse(Long commentId); + List findReplyCommentByCommentId(Long commentId); @Modifying @Query("delete from ReplyComment r where r.comment.id = :commentId") void deleteAllByCommentId(Long commentId); - Optional findAllByIdAndMemberIdAndIsDeletedFalse(Long replyCommentId, Long memberId); + Optional findAllByIdAndMemberId(Long replyCommentId, Long memberId); } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/replycomment/service/ReplyCommentService.java b/src/main/java/cotato/growingpain/replycomment/service/ReplyCommentService.java index 1c97d15..f46ace1 100644 --- a/src/main/java/cotato/growingpain/replycomment/service/ReplyCommentService.java +++ b/src/main/java/cotato/growingpain/replycomment/service/ReplyCommentService.java @@ -48,22 +48,17 @@ public void registerReplyComment(ReplyCommentRegisterRequest request, Long postI @Transactional(readOnly = true) public ReplyCommentListResponse getReplyCommentsByCommentId(Long commentId) { - List replyCommentList = replyCommentRepository.findByCommentIdAndIsDeletedFalse(commentId); + List replyCommentList = replyCommentRepository.findByCommentId(commentId); return new ReplyCommentListResponse(replyCommentList); } @Transactional public void deleteReplyComment(Long replyCommentId, Long memberId) { - ReplyComment replyComment = replyCommentRepository.findAllByIdAndMemberIdAndIsDeletedFalse(replyCommentId, memberId) + ReplyComment replyComment = replyCommentRepository.findAllByIdAndMemberId(replyCommentId, memberId) .orElseThrow(() -> new AppException(ErrorCode.REPLY_COMMENT_NOT_FOUND)); - if(replyComment.isDeleted()) { - throw new AppException(ErrorCode.ALREADY_DELETED); - } replyCommentLikeRepository.deleteByReplyCommentId(replyCommentId); - - replyComment.deleteReplyComment(); - replyCommentRepository.save(replyComment); + replyCommentRepository.delete(replyComment); } }