feat: total pages returned from paging results (#2669)

pull/2692/head
guqing 2022-11-10 14:31:33 +08:00 committed by GitHub
parent a2bb3d4a00
commit 5c05554bd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -67,8 +67,7 @@ public class ListResult<T> implements Streamable<T> {
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<T> implements Streamable<T> {
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<User>}, {@code ListResult<Post>},
* etc.

View File

@ -244,6 +244,7 @@ class CommentServiceImplTest {
"page": 1,
"size": 10,
"total": 3,
"totalPages": 1,
"items": [
{
"comment": {

View File

@ -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());
}
}