Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”จ Refactor: ๊ฒŒ์‹œ๊ธ€, ๋Œ“๊ธ€, ๋‹ต๊ธ€ hard delete๋˜๊ฒŒ ์ˆ˜์ • #143

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,9 +77,4 @@ public void validateCommentLike(Member member) {
throw new AppException(ErrorCode.CANNOT_LIKE_OWN_COMMENT);
}
}

public void deleteComment() {
isDeleted = true;
likeCount = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public record CommentResponse(
LocalDateTime modifiedAt,
String content,
Integer likeCount,
Boolean isDeleted,
Long memberId,
String profileImageUrl,
String memberNickname,
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

public interface CommentRepository extends JpaRepository<Comment, Long> {

@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<CommentResponse> 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<CommentResponse> 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<CommentResponse> 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<CommentResponse> findByPostId(@Param("postId") Long postId);

List<Comment> findAllByPostIdAndIsDeletedFalse(Long postId);
List<Comment> findAllByPostId(Long postId);

Optional<Comment> findAllByIdAndMemberIdAndIsDeletedFalse(Long commentId, Long memberId);
Optional<Comment> findAllByIdAndMemberId(Long commentId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,39 @@ public void registerComment(CommentRegisterRequest request, Long memberId, Long

@Transactional(readOnly = true)
public CommentListResponse getCommentsByMemberId(Long memberId) {
List<CommentResponse> commentList = commentRepository.findByMemberIdAndIsDeletedFalse(memberId);
List<CommentResponse> commentList = commentRepository.findByMemberId(memberId);
return new CommentListResponse(commentList);
}

@Transactional(readOnly = true)
public CommentListResponse getCommentsByPostId(Long postId) {
List<CommentResponse> commentList = commentRepository.findByPostIdAndIsDeletedFalse(postId);
List<CommentResponse> commentList = commentRepository.findByPostId(postId);
return new CommentListResponse(commentList);
}

@Transactional(readOnly = true)
public CommentListResponse getAllPostsAndCommentsByMemberId(Long memberId) {
// ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ๋ชจ๋“  ํฌ์ŠคํŠธ ์กฐํšŒ
List<Post> posts = postRepository.findAllByMemberIdAndIsDeletedFalse(memberId);
List<Post> posts = postRepository.findAllByMemberId(memberId);
List<CommentResponse> commentList = new ArrayList<>();

// ๊ฐ ํฌ์ŠคํŠธ์˜ ๋Œ“๊ธ€ ์กฐํšŒ
for (Post post : posts) {
List<CommentResponse> comments = commentRepository.findByPostIdAndIsDeletedFalse(post.getId());
List<CommentResponse> comments = commentRepository.findByPostId(post.getId());
commentList.addAll(comments);
}
return new CommentListResponse(commentList);
}

@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<ReplyComment> replyComments = replyCommentRepository.findReplyCommentByCommentIdAndIsDeletedFalse(commentId);
List<ReplyComment> replyComments = replyCommentRepository.findReplyCommentByCommentId(commentId);
replyCommentRepository.deleteAll(replyComments);

commentLikeRepository.deleteByCommentId(commentId);

comment.deleteComment();
commentRepository.save(comment);
commentRepository.delete(comment);
}
}
7 changes: 0 additions & 7 deletions src/main/java/cotato/growingpain/post/domain/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public record PostResponse(
String subCategory,
int likeCount,
int commentCount,
Boolean isDeleted,
String memberNickname,
String profileImageUrl,
String memberField
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

public interface PostRepository extends JpaRepository<Post, Long> {

List<Post> findAllByMemberIdAndIsDeletedFalse(Long memberId);
List<Post> findAllByMemberId(Long memberId);

@Query("SELECT p FROM Post p WHERE p.isDeleted = false AND (p.parentCategory = :category OR p.subCategory = :category)")
List<Post> findAllByCategoryAndIsDeletedFalse(@Param("category") PostCategory category);
@Query("SELECT p FROM Post p WHERE p.parentCategory = :category OR p.subCategory = :category")
List<Post> findAllByCategory(@Param("category") PostCategory category);

@Query("SELECT p FROM Post p WHERE p.isDeleted = false")
List<Post> findAllByIsDeletedFalse();
@Query("SELECT p FROM Post p")
List<Post> findAll();

Optional<Post> findAllByIdAndMemberIdAndIsDeletedFalse(Long postId, Long memberId);
Optional<Post> findAllByIdAndMemberId(Long postId, Long memberId);
}
21 changes: 6 additions & 15 deletions src/main/java/cotato/growingpain/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,24 @@ public void registerPost(PostRequest request, Long memberId) throws ImageExcepti

@Transactional
public List<Post> getPostsByMemberId(Long memberId) {
return postRepository.findAllByMemberIdAndIsDeletedFalse(memberId);
return postRepository.findAllByMemberId(memberId);
}

@Transactional
public List<Post> getPostsByCategory(PostCategory category) {
return postRepository.findAllByCategoryAndIsDeletedFalse(category);
return postRepository.findAllByCategory(category);
}

@Transactional
public List<Post> 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<Comment> comments = commentRepository.findAllByPostIdAndIsDeletedFalse(postId);
List<Comment> comments = commentRepository.findAllByPostId(postId);
for (Comment comment : comments) {
replyCommentRepository.deleteAllByCommentId(comment.getId());
commentRepository.delete(comment);
Expand All @@ -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());
Expand All @@ -106,7 +97,7 @@ public List<PostImage> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public record ReplyCommentResponse(
LocalDateTime modifiedAt,
String content,
Integer likeCount,
Boolean isDeleted,
Long memberId,
String profileImageUrl,
String memberNickname,
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

public interface ReplyCommentRepository extends JpaRepository<ReplyComment, Long> {

@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<ReplyCommentResponse> 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<ReplyCommentResponse> findByCommentId(Long commentId);

List<ReplyComment> findReplyCommentByCommentIdAndIsDeletedFalse(Long commentId);
List<ReplyComment> findReplyCommentByCommentId(Long commentId);

@Modifying
@Query("delete from ReplyComment r where r.comment.id = :commentId")
void deleteAllByCommentId(Long commentId);

Optional<ReplyComment> findAllByIdAndMemberIdAndIsDeletedFalse(Long replyCommentId, Long memberId);
Optional<ReplyComment> findAllByIdAndMemberId(Long replyCommentId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,17 @@ public void registerReplyComment(ReplyCommentRegisterRequest request, Long postI

@Transactional(readOnly = true)
public ReplyCommentListResponse getReplyCommentsByCommentId(Long commentId) {
List<ReplyCommentResponse> replyCommentList = replyCommentRepository.findByCommentIdAndIsDeletedFalse(commentId);
List<ReplyCommentResponse> 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);
}
}