diff --git a/src/main/java/com/listywave/list/application/service/CommentService.java b/src/main/java/com/listywave/list/application/service/CommentService.java index e436508a..740987ab 100644 --- a/src/main/java/com/listywave/list/application/service/CommentService.java +++ b/src/main/java/com/listywave/list/application/service/CommentService.java @@ -46,8 +46,16 @@ 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 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> result = comments.stream() .collect(toMap( identity(), @@ -55,10 +63,6 @@ public CommentFindResponse getComments(Long listId, int size, Long cursorId) { (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); } diff --git a/src/main/java/com/listywave/list/repository/CustomCommentRepositoryImpl.java b/src/main/java/com/listywave/list/repository/CustomCommentRepositoryImpl.java index e9bb3ed1..10a59d2b 100644 --- a/src/main/java/com/listywave/list/repository/CustomCommentRepositoryImpl.java +++ b/src/main/java/com/listywave/list/repository/CustomCommentRepositoryImpl.java @@ -17,10 +17,12 @@ public class CustomCommentRepositoryImpl implements CustomCommentRepository { public List 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(); } }