diff --git a/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java index ab2be80..670b944 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java @@ -3,6 +3,7 @@ import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import slvtwn.khu.toyouserver.common.response.ResponseType; @@ -12,6 +13,8 @@ import slvtwn.khu.toyouserver.domain.StickerSide; import slvtwn.khu.toyouserver.domain.User; import slvtwn.khu.toyouserver.dto.CoverRequest; +import slvtwn.khu.toyouserver.dto.CursorPageInfo; +import slvtwn.khu.toyouserver.dto.RollingPaperPagedResponse; import slvtwn.khu.toyouserver.dto.RollingPaperRequest; import slvtwn.khu.toyouserver.dto.RollingPaperResponse; import slvtwn.khu.toyouserver.exception.ToyouException; @@ -63,13 +66,19 @@ public RollingPaperResponse findById(Long userId, Long rollingPaperId) { return RollingPaperResponse.from(rollingPaper); } - public List findReceivedRollingPapers(Long userId, Long groupId, - Long targetId, Integer limit) { + public RollingPaperPagedResponse findReceivedRollingPapers(Long userId, Long groupId, + Long targetId, Integer limit) { List memberIds = getMemberIds(userId, groupId); PageRequest pageRequest = PageRequest.ofSize(limit); - return rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId, pageRequest).stream() + Slice rollingPapers = rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId, + pageRequest); + Long nextCursorId = rollingPapers.hasNext() ? + rollingPapers.getContent().get(rollingPapers.getContent().size() - 1).getId() : null; + CursorPageInfo cursorPageInfo = CursorPageInfo.from(rollingPapers, nextCursorId); + List responses = rollingPapers.stream() .map(RollingPaperResponse::from) .toList(); + return RollingPaperPagedResponse.from(cursorPageInfo, responses); } private List parseStickers(RollingPaperRequest request, RollingPaper rollingPaper) { diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java b/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java new file mode 100644 index 0000000..7b80f24 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java @@ -0,0 +1,9 @@ +package slvtwn.khu.toyouserver.dto; + +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()); + } +} \ No newline at end of file diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperPagedResponse.java b/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperPagedResponse.java new file mode 100644 index 0000000..37a629e --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperPagedResponse.java @@ -0,0 +1,10 @@ +package slvtwn.khu.toyouserver.dto; + +import java.util.List; + +public record RollingPaperPagedResponse(CursorPageInfo cursorPageInfo, List contents) { + + public static RollingPaperPagedResponse from(CursorPageInfo cursorPageInfo, List contents) { + return new RollingPaperPagedResponse(cursorPageInfo, contents); + } +} diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperResponse.java b/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperResponse.java index 60c1fb6..7702dfc 100644 --- a/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperResponse.java +++ b/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperResponse.java @@ -2,17 +2,16 @@ import java.util.List; import slvtwn.khu.toyouserver.domain.RollingPaper; -import slvtwn.khu.toyouserver.domain.Sticker; -public record RollingPaperResponse(String coverImageUrl, String title, String content, Long themeId, +public record RollingPaperResponse(Long id, String coverImageUrl, String title, String content, Long themeId, List stickers) { - public static RollingPaperResponse from(RollingPaper rollingPaper) { - List stickerResponses = rollingPaper.getStickers().stream() - .map(StickerResponse::from) - .toList(); + public static RollingPaperResponse from(RollingPaper rollingPaper) { + List stickerResponses = rollingPaper.getStickers().stream() + .map(StickerResponse::from) + .toList(); - return new RollingPaperResponse(rollingPaper.getCoverImageUrl(), rollingPaper.getTitle(), - rollingPaper.getContent(), rollingPaper.getThemeId(), stickerResponses); - } + return new RollingPaperResponse(rollingPaper.getId(), rollingPaper.getCoverImageUrl(), rollingPaper.getTitle(), + rollingPaper.getContent(), rollingPaper.getThemeId(), stickerResponses); + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java index c08a4dc..3579858 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java @@ -2,6 +2,7 @@ import java.util.List; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -10,7 +11,7 @@ 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") - List findAllByMembersAfterCursor(@Param("memberIds") List memberIds, - @Param("targetId") Long targetId, - Pageable pageable); + Slice findAllByMembersAfterCursor(@Param("memberIds") List memberIds, + @Param("targetId") Long targetId, + Pageable pageable); } diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/RollingPaperController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/RollingPaperController.java index 0479c52..9887f9e 100644 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/RollingPaperController.java +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/RollingPaperController.java @@ -1,6 +1,5 @@ package slvtwn.khu.toyouserver.presentation; -import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -12,6 +11,7 @@ import slvtwn.khu.toyouserver.common.authentication.UserAuthentication; import slvtwn.khu.toyouserver.common.response.ToyouResponse; import slvtwn.khu.toyouserver.dto.CoverRequest; +import slvtwn.khu.toyouserver.dto.RollingPaperPagedResponse; import slvtwn.khu.toyouserver.dto.RollingPaperRequest; import slvtwn.khu.toyouserver.dto.RollingPaperResponse; @@ -28,10 +28,10 @@ public ToyouResponse findById(@UserAuthentication Long use } @GetMapping("/rollingpapers") - public ToyouResponse> findReceivedRollingPapers(@UserAuthentication Long userId, - @RequestParam(required = false) Long groupId, - @RequestParam(defaultValue = "0") Long targetId, - @RequestParam(defaultValue = "10") int limit) { + public ToyouResponse findReceivedRollingPapers(@UserAuthentication Long userId, + @RequestParam(required = false) Long groupId, + @RequestParam(defaultValue = "0") Long targetId, + @RequestParam(defaultValue = "10") int limit) { return ToyouResponse.from(rollingPaperService.findReceivedRollingPapers(userId, groupId, targetId, limit)); } diff --git a/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java index 75e0a97..486e6a8 100644 --- a/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java +++ b/src/test/java/slvtwn/khu/toyouserver/application/RollingPaperServiceTest.java @@ -17,6 +17,7 @@ import slvtwn.khu.toyouserver.domain.Member; import slvtwn.khu.toyouserver.domain.RollingPaper; import slvtwn.khu.toyouserver.domain.User; +import slvtwn.khu.toyouserver.dto.RollingPaperPagedResponse; import slvtwn.khu.toyouserver.dto.RollingPaperRequest; import slvtwn.khu.toyouserver.dto.RollingPaperResponse; @@ -120,11 +121,11 @@ class RollingPaperServiceTest { entityManager.persist(rollingPaper); // when - List response = rollingPaperService.findReceivedRollingPapers(user.getId(), group.getId(), + RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers(user.getId(), group.getId(), 0L, 10); // then - assertThat(response).usingRecursiveComparison() + assertThat(response.contents()).usingRecursiveComparison() .isEqualTo(List.of(RollingPaperResponse.from(rollingPaper))); } @@ -148,13 +149,13 @@ class RollingPaperServiceTest { entityManager.persist(anotherRollingPaper); // when - List responseWithoutGroupId = rollingPaperService.findReceivedRollingPapers( + RollingPaperPagedResponse responseWithoutGroupId = rollingPaperService.findReceivedRollingPapers( user.getId(), null, 0L, 10); // then - assertThat(responseWithoutGroupId).usingRecursiveComparison() + assertThat(responseWithoutGroupId.contents()).usingRecursiveComparison() .isEqualTo(List.of( RollingPaperResponse.from(anotherRollingPaper), RollingPaperResponse.from(rollingPaper))); @@ -181,13 +182,13 @@ class RollingPaperServiceTest { entityManager.persist(anotherRollingPaper); // when - List responseWithGroupId = rollingPaperService.findReceivedRollingPapers( + RollingPaperPagedResponse responseWithGroupId = rollingPaperService.findReceivedRollingPapers( user.getId(), group1.getId(), 0L, 10); // then - assertThat(responseWithGroupId).usingRecursiveComparison() + assertThat(responseWithGroupId.contents()).usingRecursiveComparison() .isEqualTo(List.of(RollingPaperResponse.from(rollingPaper))); } @@ -214,7 +215,7 @@ class RollingPaperServiceTest { entityManager.persist(rollingPaper3); // when - List response = rollingPaperService.findReceivedRollingPapers( + RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers( user.getId(), group1.getId(), 0L, 10); @@ -225,7 +226,7 @@ class RollingPaperServiceTest { RollingPaperResponse.from(rollingPaper1) ); - assertThat(response).usingRecursiveComparison() + assertThat(response.contents()).usingRecursiveComparison() .isEqualTo(expectedResponse); } @@ -248,7 +249,7 @@ class RollingPaperServiceTest { entityManager.persist(rollingPaper3); // when - List response = rollingPaperService.findReceivedRollingPapers( + RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers( user.getId(), group.getId(), 0L, 10); @@ -260,7 +261,7 @@ class RollingPaperServiceTest { RollingPaperResponse.from(rollingPaper1) ); - assertThat(response).usingRecursiveComparison() + assertThat(response.contents()).usingRecursiveComparison() .isEqualTo(expectedResponse); } }