Compare commits
1 Commits
v0.1.0
...
feat/pagin
| Author | SHA1 | Date | |
|---|---|---|---|
| 56203dfb71 |
@@ -3,7 +3,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "ai.neuon.utility"
|
group = "ai.neuon.utility"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.kotlinx.serialization.json)
|
implementation(libs.kotlinx.serialization.json)
|
||||||
testImplementation(kotlin("test"))
|
implementation(libs.bundles.ktor.server)
|
||||||
|
implementation(libs.bundles.slf4j)
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package ai.neuon.utility.paging
|
||||||
|
|
||||||
|
import io.ktor.server.request.ApplicationRequest
|
||||||
|
import kotlin.math.max
|
||||||
|
|
||||||
|
sealed class PagingRequest {
|
||||||
|
companion object {
|
||||||
|
fun from(request: ApplicationRequest): PagingRequest {
|
||||||
|
val params = request.queryParameters
|
||||||
|
val pagingType = params["paging"]?.let {
|
||||||
|
Type.from(it)
|
||||||
|
} ?: Type.OFFSET
|
||||||
|
|
||||||
|
return when (pagingType) {
|
||||||
|
Type.NONE -> None
|
||||||
|
Type.OFFSET -> {
|
||||||
|
val i = params["page"]?.toInt()
|
||||||
|
?.let { max(0, it) } ?: 0
|
||||||
|
val limit = params["pageSize"]?.toInt()
|
||||||
|
?.let { max(1, it) } ?: 10
|
||||||
|
Offset(index = i, limit = limit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data object None : PagingRequest()
|
||||||
|
|
||||||
|
data class Offset(val index: Int, val limit: Int) : PagingRequest() {
|
||||||
|
val skip: Int
|
||||||
|
get() = index * limit
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class Type(val key: String) {
|
||||||
|
NONE(key = "none"), OFFSET(key = "offset");
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* @throws IllegalArgumentException
|
||||||
|
*/
|
||||||
|
fun from(type: String): Type {
|
||||||
|
return when (type) {
|
||||||
|
"none" -> NONE
|
||||||
|
"offset" -> OFFSET
|
||||||
|
else -> throw IllegalArgumentException("Unsupported pagination type, $type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,44 @@
|
|||||||
dokka = "2.0.0"
|
dokka = "2.0.0"
|
||||||
kotlin = "2.2.0"
|
kotlin = "2.2.0"
|
||||||
kotlinx-serialization-json = "1.9.0"
|
kotlinx-serialization-json = "1.9.0"
|
||||||
|
ktor = "3.4.1"
|
||||||
|
logback = "1.5.32"
|
||||||
|
slf4j = "2.0.17"
|
||||||
|
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" }
|
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ktor-server-caching-headers = { module = "io.ktor:ktor-server-caching-headers", version.ref = "ktor" }
|
||||||
|
ktor-server-cio = { module = "io.ktor:ktor-server-cio", version.ref = "ktor" }
|
||||||
|
ktor-server-config-yaml = { module = "io.ktor:ktor-server-config-yaml", version.ref = "ktor" }
|
||||||
|
ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" }
|
||||||
|
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
|
||||||
|
ktor-server-di = { module = "io.ktor:ktor-server-di", version.ref = "ktor" }
|
||||||
|
ktor-server-status-pages = { module = "io.ktor:ktor-server-status-pages", version.ref = "ktor" }
|
||||||
|
|
||||||
|
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
|
||||||
|
|
||||||
|
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
|
||||||
|
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
|
||||||
|
|
||||||
|
[bundles]
|
||||||
|
ktor-server = [
|
||||||
|
"ktor-server-cio",
|
||||||
|
"ktor-server-caching-headers",
|
||||||
|
"ktor-server-core",
|
||||||
|
"ktor-server-config-yaml",
|
||||||
|
"ktor-server-content-negotiation",
|
||||||
|
"ktor-server-di",
|
||||||
|
"ktor-server-status-pages",
|
||||||
|
"ktor-serialization-kotlinx-json",
|
||||||
|
"logback",
|
||||||
|
]
|
||||||
|
slf4j = ["slf4j-api", "logback"]
|
||||||
|
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
|
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
|
||||||
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
||||||
|
|||||||
Reference in New Issue
Block a user