From 5c05554bd8a555085b716c2e781efda8be2f35cd Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Thu, 10 Nov 2022 14:31:33 +0800 Subject: [PATCH] feat: total pages returned from paging results (#2669) --- .../java/run/halo/app/extension/ListResult.java | 9 +++++++-- .../content/comment/CommentServiceImplTest.java | 1 + .../run/halo/app/extension/ListResultTest.java | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/run/halo/app/extension/ListResult.java b/src/main/java/run/halo/app/extension/ListResult.java index 605577eaa..ffd68c606 100644 --- a/src/main/java/run/halo/app/extension/ListResult.java +++ b/src/main/java/run/halo/app/extension/ListResult.java @@ -67,8 +67,7 @@ public class ListResult implements Streamable { if (page <= 0) { return false; } - var totalPages = size == 0 ? 1 : (int) Math.ceil((double) total / (double) size); - return page < totalPages; + return page < getTotalPages(); } @Schema(description = "Indicates whether current page has previous page.", required = true) @@ -88,6 +87,12 @@ public class ListResult implements Streamable { return Streamable.super.isEmpty(); } + @Schema(description = "Indicates total pages.", required = true) + @JsonProperty("totalPages") + public long getTotalPages() { + return size == 0 ? 1 : (total + size - 1) / size; + } + /** * Generate generic ListResult class. Like {@code ListResult}, {@code ListResult}, * etc. diff --git a/src/test/java/run/halo/app/content/comment/CommentServiceImplTest.java b/src/test/java/run/halo/app/content/comment/CommentServiceImplTest.java index fe6a9bfed..6a404774f 100644 --- a/src/test/java/run/halo/app/content/comment/CommentServiceImplTest.java +++ b/src/test/java/run/halo/app/content/comment/CommentServiceImplTest.java @@ -244,6 +244,7 @@ class CommentServiceImplTest { "page": 1, "size": 10, "total": 3, + "totalPages": 1, "items": [ { "comment": { diff --git a/src/test/java/run/halo/app/extension/ListResultTest.java b/src/test/java/run/halo/app/extension/ListResultTest.java index b64f59348..f707dc8b0 100644 --- a/src/test/java/run/halo/app/extension/ListResultTest.java +++ b/src/test/java/run/halo/app/extension/ListResultTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.ParameterizedType; +import java.util.List; import org.junit.jupiter.api.Test; class ListResultTest { @@ -27,4 +28,19 @@ class ListResultTest { .getActualTypeArguments()[0]); assertEquals("FakeExtensionList", fakeListClass.getSimpleName()); } + + @Test + void totalPages() { + var listResult = new ListResult<>(1, 10, 100, List.of()); + assertEquals(10, listResult.getTotalPages()); + + listResult = new ListResult<>(1, 10, 1, List.of()); + assertEquals(1, listResult.getTotalPages()); + + listResult = new ListResult<>(1, 10, 9, List.of()); + assertEquals(1, listResult.getTotalPages()); + + listResult = new ListResult<>(1, 0, 100, List.of()); + assertEquals(1, listResult.getTotalPages()); + } } \ No newline at end of file