Skip to content

Commit

Permalink
refactor: 페이지네이션 리팩토링 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeonJuuuun authored Jun 29, 2024
1 parent d571fe7 commit 9df1302
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public interface ArticleRepository extends JpaRepository<Article, Long> {

Optional<Page<Article>> findByUserAndStatusIn(User user, List<ArticleStatus> status, Pageable pageable);

@Query("SELECT a FROM Article a JOIN a.location l WHERE l.city LIKE %:keyword% AND a.status = :status")
List<Article> findByLocationCityContainingAndStatusActive(
@Param("keyword") String keyword,
@Param("status") ArticleStatus status);

@Query("SELECT a FROM Article a JOIN a.location l WHERE l.city LIKE %:keyword% AND a.status = :status")
Page<Article> findByLocationCityContainingAndStatusActive(
@Param("keyword") String keyword,
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/site/travellaboratory/be/service/ArticleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,24 @@ public Page<ArticleTotalResponse> searchArticlesByKeyWord(
final Long loginId,
final String sort
) {
Page<Article> articles;
List<Article> articles;
Page<Article> newArticles;

if (sort.equals("popularity")) {
articles = articleRepository.findByLocationCityContainingAndStatusActive(keyword, Pageable.unpaged(),
ArticleStatus.ACTIVE);
articles = articleRepository.findByLocationCityContainingAndStatusActive(keyword, ArticleStatus.ACTIVE);
articles = sortByBookmarkCount(articles);

articles = slicePage(pageable, articles);
newArticles = slicePage(pageable, articles);
} else {
String[] sortParams = sort.split(",");
Sort.Direction direction = Sort.Direction.fromString(sortParams[1]);
Sort sortOrder = Sort.by(direction, sortParams[0]);

Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sortOrder);
articles = articleRepository.findByLocationCityContainingAndStatusActive(keyword, sortedPageable,
newArticles = articleRepository.findByLocationCityContainingAndStatusActive(keyword, sortedPageable,
ArticleStatus.ACTIVE);
}

List<ArticleTotalResponse> articleResponses = articles.getContent().stream()
List<ArticleTotalResponse> articleResponses = newArticles.getContent().stream()
.map(article -> {
boolean isEditable = article.getUser().getId().equals(loginId);
Long bookmarkCount = bookmarkRepository.countByArticleIdAndStatus(article.getId(),
Expand All @@ -208,32 +207,29 @@ public Page<ArticleTotalResponse> searchArticlesByKeyWord(
isEditable
);
})
.toList();
.collect(Collectors.toList());

return new PageImpl<>(articleResponses, pageable, articles.getTotalElements());
return new PageImpl<>(articleResponses, pageable, newArticles.getTotalElements());
}

private Page<Article> slicePage(Pageable pageable, Page<Article> articles) {
private Page<Article> slicePage(Pageable pageable, List<Article> articles) {
int pageSize = pageable.getPageSize();
int pageNumber = pageable.getPageNumber();
int fromIndex = pageNumber * pageSize;
int toIndex = Math.min(fromIndex + pageSize, articles.getContent().size());
int toIndex = Math.min(fromIndex + pageSize, articles.size());

List<Article> pagedArticles = articles.getContent().subList(fromIndex, toIndex);
articles = new PageImpl<>(pagedArticles, pageable, articles.getTotalElements());
return articles;
List<Article> pagedArticles = articles.subList(fromIndex, toIndex);
return new PageImpl<>(pagedArticles, pageable, articles.size());
}

private Page<Article> sortByBookmarkCount(Page<Article> articles) {
List<Article> sortedArticles = articles.stream()
private List<Article> sortByBookmarkCount(List<Article> articles) {
return articles.stream()
.sorted((a1, a2) -> {
Long count1 = bookmarkRepository.countByArticleIdAndStatus(a1.getId(), BookmarkStatus.ACTIVE);
Long count2 = bookmarkRepository.countByArticleIdAndStatus(a2.getId(), BookmarkStatus.ACTIVE);
return count2.compareTo(count1); // 북마크 수가 많은 순으로 내림차순 정렬
})
.collect(Collectors.toList());

return new PageImpl<>(sortedArticles, articles.getPageable(), articles.getTotalElements());
}

// 아티클 삭제
Expand Down

0 comments on commit 9df1302

Please sign in to comment.