diff --git a/src/main/java/com/back/domain/post/comment/repository/CommentRepository.java b/src/main/java/com/back/domain/post/comment/repository/CommentRepository.java index 97fa3c8..f141c1e 100644 --- a/src/main/java/com/back/domain/post/comment/repository/CommentRepository.java +++ b/src/main/java/com/back/domain/post/comment/repository/CommentRepository.java @@ -1,15 +1,17 @@ package com.back.domain.post.comment.repository; import com.back.domain.post.comment.entity.Comment; +import com.back.domain.post.comment.enums.CommentStatus; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; - @Repository public interface CommentRepository extends JpaRepository { - List findTop10ByPostIdOrderByIdDesc(Long postId); + // 첫 페이지 (lastId == null) + List findTop10ByPostIdAndStatusNotOrderByIdDesc(Long postId, CommentStatus status); - List findTop10ByPostIdAndIdLessThanOrderByIdDesc(Long postId, Long lastId); + // 무한스크롤 (lastId != null) + List findTop10ByPostIdAndIdLessThanAndStatusNotOrderByIdDesc(Long postId, Long id, CommentStatus status); } diff --git a/src/main/java/com/back/domain/post/comment/service/CommentService.java b/src/main/java/com/back/domain/post/comment/service/CommentService.java index dff2468..1d0aaca 100644 --- a/src/main/java/com/back/domain/post/comment/service/CommentService.java +++ b/src/main/java/com/back/domain/post/comment/service/CommentService.java @@ -68,12 +68,12 @@ public CommentResponseDto createComment(Long postId, CommentCreateRequestDto req @Transactional(readOnly = true) public List getComments(Long postId, Long lastId) { if (lastId == null) { - return commentRepository.findTop10ByPostIdOrderByIdDesc(postId) + return commentRepository.findTop10ByPostIdAndStatusNotOrderByIdDesc(postId, CommentStatus.DELETED) .stream() .map(CommentResponseDto::new) .toList(); } else { - return commentRepository.findTop10ByPostIdAndIdLessThanOrderByIdDesc(postId, lastId) + return commentRepository.findTop10ByPostIdAndIdLessThanAndStatusNotOrderByIdDesc(postId, lastId, CommentStatus.DELETED) .stream() .map(CommentResponseDto::new) .toList(); diff --git a/src/main/java/com/back/domain/post/post/service/PostService.java b/src/main/java/com/back/domain/post/post/service/PostService.java index 5dbbe26..b5a2161 100644 --- a/src/main/java/com/back/domain/post/post/service/PostService.java +++ b/src/main/java/com/back/domain/post/post/service/PostService.java @@ -116,7 +116,7 @@ public List getPosts(PostSortScrollRequestDto reqBody) { } // 게시글 단건 조회 로직 - @Transactional(readOnly = true) + @Transactional public PostResponseDto getPost(Long postId) { Post post = postRepository.findById(postId) .orElseThrow(() -> new NoSuchElementException("해당 게시글을 찾을 수 없습니다. ID: " + postId));