diff --git a/src/main/java/com/example/spot/repository/PostRepository.java b/src/main/java/com/example/spot/repository/PostRepository.java index 35fda09f..44a7e384 100644 --- a/src/main/java/com/example/spot/repository/PostRepository.java +++ b/src/main/java/com/example/spot/repository/PostRepository.java @@ -8,8 +8,12 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface PostRepository extends JpaRepository, PostRepositoryCustom { + // 정렬 + Page findByBoardAndPostReportListIsEmptyOrderByCreatedAtDesc(Board board, Pageable pageable); Page findByBoardAndPostReportListIsEmpty(Board board, Pageable pageable); + // 정렬 조건 필요 + Page findByPostReportListIsEmptyOrderByCreatedAtDesc(Pageable pageable); Page findByPostReportListIsEmpty(Pageable pageable); // 모든 게시글 조회 } diff --git a/src/main/java/com/example/spot/service/post/PostQueryServiceImpl.java b/src/main/java/com/example/spot/service/post/PostQueryServiceImpl.java index 22da9fa8..be6bd8ed 100644 --- a/src/main/java/com/example/spot/service/post/PostQueryServiceImpl.java +++ b/src/main/java/com/example/spot/service/post/PostQueryServiceImpl.java @@ -101,10 +101,10 @@ public PostPagingResponse getPagingPosts(String type, Pageable pageable) { Page postPage; if (boardType == Board.ALL) { // ALL 타입일 경우 모든 게시글 조회 - postPage = postRepository.findByPostReportListIsEmpty(pageable); + postPage = postRepository.findByPostReportListIsEmptyOrderByCreatedAtDesc(pageable); } else { // 특정 게시판 타입의 게시글 조회 - postPage = postRepository.findByBoardAndPostReportListIsEmpty(boardType, pageable); + postPage = postRepository.findByBoardAndPostReportListIsEmptyOrderByCreatedAtDesc(boardType, pageable); } // PostPagingDetailResponse를 묶어서 응답 리스트 생성 (좋아요 수, 좋아요여부, 스크랩 수, 스크랩여부 포함) diff --git a/src/test/java/com/example/spot/service/post/PostQueryServiceTest.java b/src/test/java/com/example/spot/service/post/PostQueryServiceTest.java index 67e71bed..222e8ea9 100644 --- a/src/test/java/com/example/spot/service/post/PostQueryServiceTest.java +++ b/src/test/java/com/example/spot/service/post/PostQueryServiceTest.java @@ -266,7 +266,7 @@ void getPagingPosts_All_Success() { List posts = List.of(post1, post2); postPage = new PageImpl<>(posts, pageable, 2); - when(postRepository.findByPostReportListIsEmpty(pageable)).thenReturn(postPage); + when(postRepository.findByPostReportListIsEmptyOrderByCreatedAtDesc(pageable)).thenReturn(postPage); // when PostPagingResponse result = postQueryService.getPagingPosts("ALL", pageable); @@ -292,7 +292,7 @@ void getPagingPosts_Type_Success() { List posts = List.of(post2); postPage = new PageImpl<>(posts, pageable, 1); - when(postRepository.findByBoardAndPostReportListIsEmpty(Board.INFORMATION_SHARING, pageable)).thenReturn(postPage); + when(postRepository.findByBoardAndPostReportListIsEmptyOrderByCreatedAtDesc(Board.INFORMATION_SHARING, pageable)).thenReturn(postPage); // when PostPagingResponse result = postQueryService.getPagingPosts("INFORMATION_SHARING", pageable);