mirror of https://github.com/halo-dev/halo
feat: total pages returned from paging results (#2669)
parent
a2bb3d4a00
commit
5c05554bd8
|
@ -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.
|
||||
|
|
|
@ -244,6 +244,7 @@ class CommentServiceImplTest {
|
|||
"page": 1,
|
||||
"size": 10,
|
||||
"total": 3,
|
||||
"totalPages": 1,
|
||||
"items": [
|
||||
{
|
||||
"comment": {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue