read kerver impl

This commit is contained in:
000Sanya 2020-08-26 12:55:37 +10:00
parent 89bef924be
commit 20dba28f13

View File

@ -0,0 +1,101 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server
import com.insanusmokrassar.postssystem.ktor.server.extractPagination
import com.insanusmokrassar.postssystem.ktor.server.getQueryParameterOrSendError
import com.insanusmokrassar.postssystem.ktor.server.unianswer
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.application.*
import io.ktor.routing.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.decodeFromHexString
fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
originalRepo: OneToManyReadKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>,
) {
val paginationKeyResult = PaginationResult.serializer(keySerializer)
val paginationValueResult = PaginationResult.serializer(valueSealizer)
get(getRoute) {
val pagination = call.request.queryParameters.extractPagination
val key = standardKtorSerialFormat.decodeFromHexString(
keySerializer,
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
) // TODO: Леша, а это можно сократить как-то до call.decodeFromQueryHexParameter(keySerializer, keyParameterName)?
val reversed = standardKtorSerialFormat.decodeFromHexString(
Boolean.serializer(),
call.getQueryParameterOrSendError(reversedParameterName) ?: return@get
)
call.unianswer(
paginationValueResult,
originalRepo.get(key, pagination, reversed)
)
}
get(keysRoute) {
val pagination = call.request.queryParameters.extractPagination
val reversed = standardKtorSerialFormat.decodeFromHexString(
Boolean.serializer(),
call.getQueryParameterOrSendError(reversedParameterName) ?: return@get
)
call.unianswer(
paginationKeyResult,
originalRepo.keys(pagination, reversed)
)
}
get(containsByKeyRoute) {
val key = standardKtorSerialFormat.decodeFromHexString(
keySerializer,
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
call.unianswer(
Boolean.serializer(),
originalRepo.contains(key)
)
}
get(containsByKeyValueRoute) {
val key = standardKtorSerialFormat.decodeFromHexString(
keySerializer,
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
val value = standardKtorSerialFormat.decodeFromHexString(
valueSealizer,
call.getQueryParameterOrSendError(valueParameterName) ?: return@get
)
call.unianswer(
Boolean.serializer(),
originalRepo.contains(key, value)
)
}
get(countByKeyRoute) {
val key = standardKtorSerialFormat.decodeFromHexString(
keySerializer,
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
call.unianswer(
Long.serializer(),
originalRepo.count(key)
)
}
get(countRoute) {
call.unianswer(
Long.serializer(),
originalRepo.count()
)
}
}