From d21144e267699697a9ca6a35aefb7cca17640ed2 Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Thu, 17 Oct 2024 12:38:03 +0900 Subject: [PATCH] =?UTF-8?q?:bug:=20fix:=20=EB=A9=94=EC=8B=9C=EC=A7=80?= =?UTF-8?q?=ED=95=A8=20=EC=BB=A4=EC=84=9C=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20?= =?UTF-8?q?limit=20=EC=B2=98=EB=A6=AC=EB=90=9C=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=EB=A1=9C=20=EB=B0=94=EC=9D=B8=EB=94=A9=20(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/RollingPaperService.java | 13 ++- .../khu/toyouserver/dto/CursorPageInfo.java | 4 +- .../persistance/RollingPaperRepository.java | 7 +- .../application/RollingPaperServiceTest.java | 102 ++++++++++++++++++ 4 files changed, 116 insertions(+), 10 deletions(-) diff --git a/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java index 4c4f7f9..5f2b344 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java @@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Slice; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import slvtwn.khu.toyouserver.common.response.ResponseType; @@ -77,15 +78,17 @@ public RollingPaperResponse findById(Long userId, Long rollingPaperId) { public RollingPaperPagedResponse findReceivedRollingPapers(Long userId, Long groupId, Long targetId, Integer limit) { List memberIds = getMemberIds(userId, groupId); - PageRequest pageRequest = PageRequest.ofSize(limit); - Slice rollingPapers = rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId, + PageRequest pageRequest = PageRequest.of(0, limit + 1, Sort.by(Sort.Direction.DESC, "id")); + Slice rollingPapers = rollingPaperRepository.findByMembersWithCursor(memberIds, targetId, pageRequest); - Long nextCursorId = rollingPapers.hasNext() ? - rollingPapers.getContent().get(rollingPapers.getContent().size() - 1).getId() : null; - CursorPageInfo cursorPageInfo = CursorPageInfo.from(rollingPapers, nextCursorId); + Long nextCursorId = + rollingPapers.getContent().size() > limit ? rollingPapers.getContent().get(0).getId() : null; List responses = rollingPapers.stream() + .limit(limit) .map(RollingPaperResponse::from) .toList(); + CursorPageInfo cursorPageInfo = CursorPageInfo.from(rollingPapers, nextCursorId, responses.size(), + nextCursorId != null); return RollingPaperPagedResponse.from(cursorPageInfo, responses); } diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java b/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java index 7b80f24..f3055be 100644 --- a/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java +++ b/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java @@ -3,7 +3,7 @@ import org.springframework.data.domain.Slice; public record CursorPageInfo(Long nextCursorId, int numberOfElements, boolean hasNext) { - public static CursorPageInfo from(Slice data, Long lastCursor) { - return new CursorPageInfo(lastCursor, data.getNumberOfElements(), data.hasNext()); + public static CursorPageInfo from(Slice data, Long nextCursorId, int numberOfElements, boolean hasNext) { + return new CursorPageInfo(nextCursorId, data.getNumberOfElements(), data.hasNext()); } } \ No newline at end of file diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java index 87b236b..0dd6172 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java @@ -12,10 +12,11 @@ public interface RollingPaperRepository extends JpaRepository { @Query("SELECT r FROM RollingPaper r WHERE r.member.id IN :memberIds AND r.id >= :targetId ORDER BY r.id DESC") - Slice findAllByMembersAfterCursor(@Param("memberIds") List memberIds, - @Param("targetId") Long targetId, - Pageable pageable); + Slice findByMembersWithCursor(@Param("memberIds") List memberIds, + @Param("targetId") Long targetId, + Pageable pageable); List findAllByMemberIn(List members); + void deleteAllByMemberIn(List members); } diff --git a/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java index d6d725d..e21b70f 100644 --- a/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java +++ b/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java @@ -283,4 +283,106 @@ class RollingPaperServiceTest { assertThat(response.contents()).usingRecursiveComparison() .isEqualTo(expectedResponse); } + + @Test + void 수신한_롤링페이퍼_조회는_커서가_존재할_경우_커서_이후의_롤링페이퍼만_조회된다() { + // given + User sender = new User("sender", LocalDate.now(), "introduction", "profile_picture", null); + User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null); + Group group = new Group("group"); + Member member = new Member(user, group); + + RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + + entityManager.persist(user); + entityManager.persist(group); + entityManager.persist(member); + entityManager.persist(rollingPaper1); + entityManager.persist(rollingPaper2); + entityManager.persist(rollingPaper3); + + // when + RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers( + user.getId(), + group.getId(), + rollingPaper2.getId(), 10); + + // then + List expectedResponse = List.of( + RollingPaperResponse.from(rollingPaper3), + RollingPaperResponse.from(rollingPaper2) + ); + + assertThat(response.contents()).usingRecursiveComparison() + .isEqualTo(expectedResponse); + } + + @Test + void 커서_정보는_다음_페이지가_존재할_경우_다음_페이지의_커서를_포함한다() { + // given + User sender = new User("sender", LocalDate.now(), "introduction", "profile_picture", null); + User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null); + Group group = new Group("group"); + Member member = new Member(user, group); + + RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + + entityManager.persist(user); + entityManager.persist(group); + entityManager.persist(member); + entityManager.persist(rollingPaper1); + entityManager.persist(rollingPaper2); + entityManager.persist(rollingPaper3); + + // when + RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers( + user.getId(), + group.getId(), + rollingPaper1.getId(), 2); // 1번부터 2개 조회 -> expected 1,2 / next 3 + + // then + assertThat(response.cursorPageInfo().nextCursorId()).isEqualTo(rollingPaper3.getId()); + } + + @Test + void 조회_가능_최대치_도달_경우_다음_페이지_존재_여부는_False_이다() { + // given + User sender = new User("sender", LocalDate.now(), "introduction", "profile_picture", null); + User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null); + Group group = new Group("group"); + Member member = new Member(user, group); + + RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3", + 1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture())); + + entityManager.persist(user); + entityManager.persist(group); + entityManager.persist(member); + entityManager.persist(rollingPaper1); + entityManager.persist(rollingPaper2); + entityManager.persist(rollingPaper3); + + // when + RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers( + user.getId(), + group.getId(), + rollingPaper1.getId(), 2); + + // then + assertThat(response.cursorPageInfo().hasNext()).isFalse(); + } }