From 744e9fde84a28da157d896344224a85ab3c234f1 Mon Sep 17 00:00:00 2001 From: pipinet Date: Fri, 1 Nov 2024 22:32:16 +0800 Subject: [PATCH] feature: add PageRequests --- .../com/qwlabs/jakarta/data/PageRequests.java | 8 ++++++++ .../src/test/java/com/qwlabs/jakarta/.gitkeep | 0 .../qwlabs/jakarta/data/PageRequestsTest.java | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+) delete mode 100644 jakarta-data/src/test/java/com/qwlabs/jakarta/.gitkeep diff --git a/jakarta-data/src/main/java/com/qwlabs/jakarta/data/PageRequests.java b/jakarta-data/src/main/java/com/qwlabs/jakarta/data/PageRequests.java index 097b83c..f36bda1 100644 --- a/jakarta-data/src/main/java/com/qwlabs/jakarta/data/PageRequests.java +++ b/jakarta-data/src/main/java/com/qwlabs/jakarta/data/PageRequests.java @@ -20,4 +20,12 @@ static PageRequest of(@Nullable String after, int first) { var pageNumber = (startAt + first) / first; return PageRequest.ofPage(pageNumber, first, true); } + + static int firstResult(PageRequest request){ + return Long.valueOf((request.page() - 1) * request.size()).intValue(); + } + + static int maxResults(PageRequest request){ + return request.size(); + } } diff --git a/jakarta-data/src/test/java/com/qwlabs/jakarta/.gitkeep b/jakarta-data/src/test/java/com/qwlabs/jakarta/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/jakarta-data/src/test/java/com/qwlabs/jakarta/data/PageRequestsTest.java b/jakarta-data/src/test/java/com/qwlabs/jakarta/data/PageRequestsTest.java index 071c3e5..ad24969 100644 --- a/jakarta-data/src/test/java/com/qwlabs/jakarta/data/PageRequestsTest.java +++ b/jakarta-data/src/test/java/com/qwlabs/jakarta/data/PageRequestsTest.java @@ -20,6 +20,26 @@ void should_of() { checkRequest("2", 4, 1L); } + @Test + void should_firstResult() { + assertThat(PageRequests.firstResult(PageRequests.of("0", 3)), is(0)); + assertThat(PageRequests.firstResult(PageRequests.of("0", 1)), is(0)); + assertThat(PageRequests.firstResult(PageRequests.of("1", 2)), is(0)); + assertThat(PageRequests.firstResult(PageRequests.of("2", 2)), is(2)); + assertThat(PageRequests.firstResult(PageRequests.of("3", 2)), is(2)); + assertThat(PageRequests.firstResult(PageRequests.of("4", 2)), is(4)); + } + + @Test + void should_maxResults() { + assertThat(PageRequests.maxResults(PageRequests.of("0", 3)), is(3)); + assertThat(PageRequests.maxResults(PageRequests.of("0", 1)), is(1)); + assertThat(PageRequests.maxResults(PageRequests.of("1", 2)), is(2)); + assertThat(PageRequests.maxResults(PageRequests.of("2", 2)), is(2)); + assertThat(PageRequests.maxResults(PageRequests.of("3", 2)), is(2)); + assertThat(PageRequests.maxResults(PageRequests.of("4", 2)), is(2)); + } + void checkRequest(String after, Integer first, long page) { var pageRequest = PageRequests.of(after, first); assertThat(pageRequest.page(), is(page));