Skip to content

Commit

Permalink
fix: fix pagination logic error
Browse files Browse the repository at this point in the history
  • Loading branch information
Ji-soo708 committed Dec 11, 2024
1 parent 9637783 commit c9cadb5
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions post/src/main/java/com/oing/controller/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

Expand Down Expand Up @@ -95,7 +96,6 @@ private List<PostCommentResponseV2> combineComments(List<Comment> comments, List
List<PostCommentResponseV2> textComments = comments.stream()
.map(this::mapToTextComment)
.toList();

List<PostCommentResponseV2> voiceCommentResponses = voiceComments.stream()
.map(this::mapToVoiceComment)
.toList();
Expand Down Expand Up @@ -131,7 +131,7 @@ private PostCommentResponseV2 mapToVoiceComment(VoiceComment voiceComment) {
);
}

private Comparator<? super PostCommentResponseV2> getCommentComparator(String sort) {
private Comparator<PostCommentResponseV2> getCommentComparator(String sort) {
return (c1, c2) -> {
if (sort == null || sort.equalsIgnoreCase("ASC")) {
return c1.commentId().compareTo(c2.commentId());
Expand All @@ -144,9 +144,17 @@ private Comparator<? super PostCommentResponseV2> getCommentComparator(String so
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);

Expand Down

0 comments on commit c9cadb5

Please sign in to comment.