Skip to content

Commit

Permalink
Fix: isDeleted가 true인 게시글, 댓글, 답글은 목록으로 반환되지 않게 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhacandy authored Aug 21, 2024
2 parents 3ab48e8 + 9bba460 commit f9183ed
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
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")
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.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.isDeleted, 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);
@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);

List<Comment> findCommentsByPostId(Long postId);
List<Comment> findCommentsByPostIdAndIsDeletedFalse(Long postId);

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

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

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

@Transactional(readOnly = true)
public CommentListResponse getAllPostsAndCommentsByMemberId(Long memberId) {
// 사용자가 작성한 모든 포스트 조회
List<Post> posts = postRepository.findByMemberId(memberId);
List<Post> posts = postRepository.findByMemberIdAndIsDeletedFalse(memberId);
List<CommentResponse> commentList = new ArrayList<>();

// 각 포스트의 댓글 조회
for (Post post : posts) {
List<CommentResponse> comments = commentRepository.findByPostId(post.getId());
List<CommentResponse> comments = commentRepository.findByPostIdAndIsDeletedFalse(post.getId());
commentList.addAll(comments);
}
return new CommentListResponse(commentList);
}

@Transactional
public void deleteComment(Long commentId, Long memberId) {
Comment comment = commentRepository.findByIdAndMemberId(commentId, memberId)
Comment comment = commentRepository.findByIdAndMemberIdAndIsDeletedFalse(commentId, memberId)
.orElseThrow(() -> new AppException(ErrorCode.COMMENT_NOT_FOUND));

if(comment.isDeleted()) {
throw new AppException(ErrorCode.ALREADY_DELETED);
}

List<ReplyComment> replyComments = replyCommentRepository.findReplyCommentByCommentId(commentId);
List<ReplyComment> replyComments = replyCommentRepository.findReplyCommentByCommentIdAndIsDeletedFalse(commentId);
replyCommentRepository.deleteAll(replyComments);

commentLikeRepository.deleteByCommentId(commentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

public interface PostRepository extends JpaRepository<Post, Long> {

List<Post> findByMemberId(Long memberId);
List<Post> findByMemberIdAndIsDeletedFalse(Long memberId);

@Query("SELECT p FROM Post p WHERE p.parentCategory = :category OR p.subCategory = :category")
List<Post> findByCategory(@Param("category") PostCategory category);
List<Post> findByCategoryAndIsDeletedFalse(@Param("category") PostCategory category);

Optional<Post> findByIdAndMemberId(Long postId, Long memberId);
Optional<Post> findByIdAndMemberIdAndIsDeletedFalse(Long postId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public void registerPost(PostRequest request, Long memberId) {
}

public List<Post> getPostsByMemberId(Long memberId) {
return postRepository.findByMemberId(memberId);
return postRepository.findByMemberIdAndIsDeletedFalse(memberId);
}

public List<Post> getPostsByCategory(PostCategory category){
return postRepository.findByCategory(category);
return postRepository.findByCategoryAndIsDeletedFalse(category);
}

public List<Post> getAllPosts() {
Expand All @@ -59,7 +59,7 @@ public void deletePost(Long postId, Long memberId) {
throw new AppException(ErrorCode.ALREADY_DELETED);
}

List<Comment> comments = commentRepository.findCommentsByPostId(postId);
List<Comment> comments = commentRepository.findCommentsByPostIdAndIsDeletedFalse(postId);
for (Comment comment : comments) {
replyCommentRepository.deleteAllByCommentId(comment.getId());
commentRepository.delete(comment);
Expand All @@ -84,7 +84,7 @@ public void updatePost(Long postId, PostRequest request, Long memberId) {
}

private Post findByPostIdAndMemberId(Long postId, Long memberId) {
return postRepository.findByIdAndMemberId(postId, memberId)
return postRepository.findByIdAndMemberIdAndIsDeletedFalse(postId, memberId)
.orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND));
}
}
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")
List<ReplyCommentResponse> findByCommentId(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.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);

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

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

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

@Transactional(readOnly = true)
public ReplyCommentListResponse getReplyCommentsByCommentId(Long commentId) {
List<ReplyCommentResponse> replyCommentList = replyCommentRepository.findByCommentId(commentId);
List<ReplyCommentResponse> replyCommentList = replyCommentRepository.findByCommentIdAndIsDeletedFalse(commentId);
return new ReplyCommentListResponse(replyCommentList);
}

@Transactional
public void deleteReplyComment(Long replyCommentId, Long memberId) {
ReplyComment replyComment = replyCommentRepository.findByIdAndMemberId(replyCommentId, memberId)
ReplyComment replyComment = replyCommentRepository.findByIdAndMemberIdAndIsDeletedFalse(replyCommentId, memberId)
.orElseThrow(() -> new AppException(ErrorCode.REPLY_COMMENT_NOT_FOUND));

if(replyComment.isDeleted()) {
Expand Down

0 comments on commit f9183ed

Please sign in to comment.