From 207c44aca09b2f188e895f2852b26954607982a8 Mon Sep 17 00:00:00 2001 From: kdkdhoho Date: Sun, 4 Feb 2024 14:55:40 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EB=B0=A9=EC=8B=9D=EC=9D=84=20offset=20?= =?UTF-8?q?=EB=B0=A9=EC=8B=9D=EC=97=90=EC=84=9C=20cursorId=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95=20(#43)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../list/application/service/CommentService.java | 14 +++++++++----- .../repository/CustomCommentRepositoryImpl.java | 8 +++++--- 2 files changed, 14 insertions(+), 8 deletions(-) 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(); } }