From c8608b4525dfcb8533289db076979c6737769623 Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Tue, 1 Oct 2024 02:13:34 +0900 Subject: [PATCH 1/4] =?UTF-8?q?:recycle:=20refactor:=20=EC=88=98=EC=8B=A0?= =?UTF-8?q?=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=A1=B0=ED=9A=8C=20API=20?= =?UTF-8?q?=EC=BB=A4=EC=84=9C=20=ED=8E=98=EC=9D=B4=EC=A7=95=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=A7=81=EB=A0=AC=ED=99=94=20=EC=A0=81=EC=9A=A9=20?= =?UTF-8?q?(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../slvtwn/khu/toyouserver/dto/CursorPageInfo.java | 11 +++++++++++ .../toyouserver/dto/RollingPaperPagedResponse.java | 10 ++++++++++ .../presentation/RollingPaperController.java | 10 +++++----- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java create mode 100644 src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperPagedResponse.java 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..5cdff80 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java @@ -0,0 +1,11 @@ +package slvtwn.khu.toyouserver.dto; + +import org.springframework.data.domain.Slice; + +public record CursorPageInfo(Long nextCursorId, int numberOfElements, boolean hasNext, int currentPage, + boolean isFirst, boolean isLast) { + public static CursorPageInfo from(Slice data, Long lastCursor) { + return new CursorPageInfo(lastCursor, data.getNumberOfElements(), data.hasNext(), data.getNumber(), + data.isFirst(), data.isLast()); + } +} \ 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/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)); } From 76ed401849c8f64966cec2548464d432d4ffa622 Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Tue, 1 Oct 2024 02:13:47 +0900 Subject: [PATCH 2/4] =?UTF-8?q?:recycle:=20refactor:=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=ED=95=A8=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=95=20=EC=A7=80=EC=9B=90=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=ED=83=80=EC=9E=85=20=EB=B3=80=EA=B2=BD=20(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/RollingPaperService.java | 15 ++++++++++--- .../persistance/RollingPaperRepository.java | 7 ++++--- .../application/RollingPaperServiceTest.java | 21 ++++++++++--------- 3 files changed, 27 insertions(+), 16 deletions(-) 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/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/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); } } From 3c60f56ccc3b2d42c970e128749c545ba8f5f811 Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Tue, 1 Oct 2024 02:17:33 +0900 Subject: [PATCH 3/4] =?UTF-8?q?:recycle:=20refactor:=20=EC=BB=A4=EC=84=9C?= =?UTF-8?q?=20=ED=8E=98=EC=9D=B4=EC=A7=95=EC=97=90=20=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=A0=95=EB=B3=B4=EB=A7=8C=20=EC=A7=81=EB=A0=AC?= =?UTF-8?q?=ED=99=94=20(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java b/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java index 5cdff80..7b80f24 100644 --- a/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java +++ b/src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java @@ -2,10 +2,8 @@ import org.springframework.data.domain.Slice; -public record CursorPageInfo(Long nextCursorId, int numberOfElements, boolean hasNext, int currentPage, - boolean isFirst, boolean isLast) { +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(), data.getNumber(), - data.isFirst(), data.isLast()); + return new CursorPageInfo(lastCursor, data.getNumberOfElements(), data.hasNext()); } } \ No newline at end of file From c13c147aeeadb76e7f9de55e57ac55861303990f Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Tue, 1 Oct 2024 02:23:24 +0900 Subject: [PATCH 4/4] =?UTF-8?q?:recycle:=20refactor:=20=EB=A9=94=EC=84=B8?= =?UTF-8?q?=EC=A7=80=ED=95=A8=20=EC=A1=B0=ED=9A=8C=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EC=8B=9D=EB=B3=84=EC=9E=90=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toyouserver/dto/RollingPaperResponse.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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); + } }