diff --git a/src/main/java/com/leets/xcellentbe/domain/article/controller/ArticleController.java b/src/main/java/com/leets/xcellentbe/domain/article/controller/ArticleController.java index 78ce665..2f7929b 100644 --- a/src/main/java/com/leets/xcellentbe/domain/article/controller/ArticleController.java +++ b/src/main/java/com/leets/xcellentbe/domain/article/controller/ArticleController.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.UUID; +import org.springframework.data.domain.Page; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -88,11 +89,11 @@ public ResponseEntity> getArticle( //메인 페이지 게시글 조회 @GetMapping @Operation(summary = "게시글 목록 조회(스크롤)", description = "페이징을 적용하여 게시글 목록을 조회합니다.") - public ResponseEntity>> getArticles( + public ResponseEntity>> getArticles( HttpServletRequest request, @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime cursor, @RequestParam(defaultValue = "10") int size) { - List articles = articleService.getArticles(request, cursor, size); + Page articles = articleService.getArticles(request, cursor, size); return ResponseEntity.status(HttpStatus.OK).body(GlobalResponseDto.success(articles)); } diff --git a/src/main/java/com/leets/xcellentbe/domain/article/domain/repository/ArticleRepository.java b/src/main/java/com/leets/xcellentbe/domain/article/domain/repository/ArticleRepository.java index c7b7489..f5d19c4 100644 --- a/src/main/java/com/leets/xcellentbe/domain/article/domain/repository/ArticleRepository.java +++ b/src/main/java/com/leets/xcellentbe/domain/article/domain/repository/ArticleRepository.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.UUID; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -17,11 +18,8 @@ public interface ArticleRepository extends JpaRepository { @Query("SELECT new com.leets.xcellentbe.domain.article.dto.ArticlesWithMediaDto(p, pm.filePath) FROM Article p LEFT JOIN ArticleMedia pm ON p.articleId = pm.article.articleId WHERE p.writer = :user") List findPostsByWriter(User user); - @Query("SELECT a FROM Article a WHERE a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC") - List
findRecentArticles(Pageable pageable); - - @Query("SELECT a FROM Article a WHERE a.createdAt < :cursor AND a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC") - List
findRecentArticles(@Param("cursor") LocalDateTime cursor, Pageable pageable); + @Query("SELECT a FROM Article a WHERE (:cursor IS NULL OR a.createdAt < :cursor) AND a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC") + Page
findRecentArticles(@Param("cursor") LocalDateTime cursor, Pageable pageable); @Query("SELECT COUNT(a) FROM Article a WHERE a.rePost = :article AND a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED") long countReposts(@Param("article") Article article); diff --git a/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java b/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java index 130632c..bae8430 100644 --- a/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java +++ b/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java @@ -8,6 +8,7 @@ import java.util.UUID; import java.util.stream.Collectors; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -175,24 +176,17 @@ public ArticleResponseDto getArticle(HttpServletRequest request, UUID articleId) } //게시글 전체 조회 - public List getArticles(HttpServletRequest request, LocalDateTime cursor, int size) { - + public Page getArticles(HttpServletRequest request, LocalDateTime cursor, int size) { User user = getUser(request); - Pageable pageable = PageRequest.of(0, size); - List
articles = (cursor == null) ? - articleRepository.findRecentArticles(pageable) : // 처음 로드 시 - articleRepository.findRecentArticles(cursor, pageable); - - return articles - .stream() - .map(article -> { - boolean isOwner = article.getWriter().getUserId().equals(user.getUserId()); - ArticleStatsDto stats = findArticleStats(article); - return ArticleResponseDto.fromWithoutComments(article, isOwner, stats); - }) - .collect(Collectors.toList()); + Page
articles = articleRepository.findRecentArticles(cursor, pageable); + + return articles.map(article -> { + boolean isOwner = article.getWriter().getUserId().equals(user.getUserId()); + ArticleStatsDto stats = findArticleStats(article); + return ArticleResponseDto.fromWithoutComments(article, isOwner, stats); + }); } //리포스트 작성 (인용 x, 단순)