Skip to content

Commit

Permalink
feat: 페이지네이션 방식을 offset 방식에서 cursorId 방식으로 수정 (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Feb 4, 2024
1 parent 7426084 commit 207c44a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,23 @@ public CommentCreateResponse create(Long listId, String accessToken, String cont
public CommentFindResponse getComments(Long listId, int size, Long cursorId) {
Lists list = listRepository.getById(listId);
Long totalCount = commentRepository.countByList(list);

List<Comment> comments = commentRepository.getComments(listId, size, cursorId);

boolean hasNext = false;
if (comments.size() > size) {
hasNext = true;
comments.remove(comments.size() - 1);
}
Long cursorIdOfResult = comments.get(comments.size() - 1).getId();

Map<Comment, List<Reply>> result = comments.stream()
.collect(toMap(
identity(),
replyRepository::getAllByComment,
(exists, newValue) -> exists,
LinkedHashMap::new
));

Long cursorIdOfResult = comments.get(comments.size() - 1).getId();
boolean hasNext = comments.size() >= size;

return CommentFindResponse.from(totalCount, cursorIdOfResult, hasNext, result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public class CustomCommentRepositoryImpl implements CustomCommentRepository {
public List<Comment> getComments(Long listId, int size, Long cursorId) {
return queryFactory.selectFrom(comment)
.join(comment.list, lists)
.where(comment.list.id.eq(listId))
.where(
comment.list.id.eq(listId),
comment.id.gt(cursorId)
)
.orderBy(comment.id.asc())
.limit(size)
.offset(cursorId + 1)
.limit(size + 1)
.fetch();
}
}

0 comments on commit 207c44a

Please sign in to comment.