add methode$ "isLastPage" to pagination
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
import kotlin.math.ceil
|
||||||
|
|
||||||
data class Paginated<T : EntityI>(
|
data class Paginated<T : EntityI>(
|
||||||
val result: List<T>,
|
val result: List<T>,
|
||||||
@@ -10,10 +11,15 @@ data class Paginated<T : EntityI>(
|
|||||||
) {
|
) {
|
||||||
val currentPage: Int = (offset / limit) + 1
|
val currentPage: Int = (offset / limit) + 1
|
||||||
val count: Int = result.size
|
val count: Int = result.size
|
||||||
|
val totalPages: Int = (total.toDouble() / limit.toDouble()).ceil()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (offset < 0) error("offset must be greather or equal than 0")
|
if (offset < 0) error("offset must be greather or equal than 0")
|
||||||
if (limit < 1) error("limit must be greather or equal than 1")
|
if (limit < 1) error("limit must be greather or equal than 1")
|
||||||
if (total < 0) error("total must be greather or equal than 0")
|
if (total < 0) error("total must be greather or equal than 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isLastPage(): Boolean = currentPage >= totalPages
|
||||||
|
|
||||||
|
private fun Double.ceil(): Int = ceil(this).toInt()
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user