Skip to content

Commit

Permalink
refactor: refact pagination code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ji-soo708 committed Dec 12, 2024
1 parent c9cadb5 commit f139660
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
38 changes: 38 additions & 0 deletions common/src/main/java/com/oing/util/Paginator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.oing.util;

import com.oing.domain.PaginationDTO;

import java.util.Collections;
import java.util.List;

public class Paginator<T> {
private final List<T> items;
private final int pageSize;

public Paginator(List<T> items, int pageSize) {
if (pageSize <= 0) {
throw new IllegalArgumentException("Page size must be greater than 0.");
}
this.items = items != null ? items : Collections.emptyList();
this.pageSize = pageSize;
}

public PaginationDTO<T> getPage(int pageNumber) {
if (pageNumber <= 0) {
throw new IllegalArgumentException("Page number must be greater than 0.");
}

int totalItems = items.size();
int totalPages = (int) Math.ceil((double) totalItems / pageSize);

if (pageNumber > totalPages) {
return new PaginationDTO<>(totalPages, Collections.emptyList());
}

int startIndex = (pageNumber - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, totalItems);

List<T> pageItems = items.subList(startIndex, endIndex);
return new PaginationDTO<>(totalPages, pageItems);
}
}
19 changes: 3 additions & 16 deletions post/src/main/java/com/oing/controller/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import com.oing.service.MemberBridge;
import com.oing.service.PostService;
import com.oing.service.VoiceCommentService;
import com.oing.util.Paginator;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand Down Expand Up @@ -142,21 +142,8 @@ private Comparator<PostCommentResponseV2> getCommentComparator(String sort) {
}

private PaginationResponse<PostCommentResponseV2> paginateComments(List<PostCommentResponseV2> comments, Integer page, Integer size) {
int total = comments.size();
int totalPage = (int) Math.ceil((double) total / size);
if (page > totalPage) {
// 데이터가 없는 페이지 요청 시 빈 목록 반환
return PaginationResponse.of(
new PaginationDTO<>(totalPage, Collections.emptyList()),
page,
size
);
}

int start = (page - 1) * size;
int end = Math.min(start + size, total);
List<PostCommentResponseV2> paginatedComments = comments.subList(start, end);
PaginationDTO<PostCommentResponseV2> paginationDTO = new PaginationDTO<>(totalPage, paginatedComments);
Paginator<PostCommentResponseV2> paginator = new Paginator<>(comments, size);
PaginationDTO<PostCommentResponseV2> paginationDTO = paginator.getPage(page);

return PaginationResponse.of(paginationDTO, page, size);
}
Expand Down

0 comments on commit f139660

Please sign in to comment.