UnifiedRouter

This commit is contained in:
InsanusMokrassar 2020-11-27 14:35:00 +06:00
parent 26d5f5a5f5
commit ab11e28bf7
23 changed files with 536 additions and 281 deletions

View File

@ -5,10 +5,14 @@
* `Ktor` * `Ktor`
* `Client` * `Client`
* New class `UnifiedRequester` * New class `UnifiedRequester`
* `Server`
* New class `UnifiedRouter`
* `Repos` * `Repos`
* `Ktor` * `Ktor`
* `Client` * `Client`
* Rewriting of all clients on new `UnifiedRequester` * Rewriting of all clients on new `UnifiedRequester`
* `Server`
* Rewriting of all clients on new `UnifiedRouter`
## 0.4.6 ## 0.4.6

View File

@ -8,7 +8,6 @@ import io.ktor.http.cio.websocket.Frame
import io.ktor.http.cio.websocket.readBytes import io.ktor.http.cio.websocket.readBytes
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.flow.channelFlow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
/** /**
@ -66,7 +65,7 @@ inline fun <T> HttpClient.createStandardWebsocketFlow(
url: String, url: String,
crossinline checkReconnection: (Throwable?) -> Boolean = { true }, crossinline checkReconnection: (Throwable?) -> Boolean = { true },
deserializer: DeserializationStrategy<T>, deserializer: DeserializationStrategy<T>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) = createStandardWebsocketFlow( ) = createStandardWebsocketFlow(
url, url,
checkReconnection checkReconnection

View File

@ -10,7 +10,7 @@ typealias BodyPair<T> = Pair<SerializationStrategy<T>, T>
class UnifiedRequester( class UnifiedRequester(
private val client: HttpClient = HttpClient(), private val client: HttpClient = HttpClient(),
private val serialFormat: BinaryFormat = standardKtorSerialFormat private val serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) { ) {
suspend fun <ResultType> uniget( suspend fun <ResultType> uniget(
url: String, url: String,

View File

@ -33,10 +33,11 @@ fun <T> Route.includeWebsocketHandling(
fun <T> Route.includeWebsocketHandling( fun <T> Route.includeWebsocketHandling(
suburl: String, suburl: String,
flow: Flow<T>, flow: Flow<T>,
serializer: SerializationStrategy<T> serializer: SerializationStrategy<T>,
serialFormat: StandardKtorSerialFormat
) = includeWebsocketHandling( ) = includeWebsocketHandling(
suburl, suburl,
flow flow
) { ) {
standardKtorSerialFormat.encodeDefault(serializer, it) serialFormat.encodeDefault(serializer, it)
} }

View File

@ -3,12 +3,87 @@ package dev.inmo.micro_utils.ktor.server
import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.ktor.common.*
import io.ktor.application.ApplicationCall import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.request.receive import io.ktor.request.receive
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.response.respondBytes import io.ktor.response.respondBytes
import kotlinx.serialization.DeserializationStrategy import io.ktor.routing.Route
import kotlinx.serialization.SerializationStrategy import io.ktor.util.pipeline.PipelineContext
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.*
class UnifiedRouter(
private val serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
private val serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) {
fun <T> Route.includeWebsocketHandling(
suburl: String,
flow: Flow<T>,
serializer: SerializationStrategy<T>
) = includeWebsocketHandling(suburl, flow, serializer, serialFormat)
suspend fun <T> PipelineContext<*, ApplicationCall>.unianswer(
answerSerializer: SerializationStrategy<T>,
answer: T
) {
call.respondBytes (
serialFormat.encodeDefault(answerSerializer, answer),
serialFormatContentType
)
}
suspend fun <T> PipelineContext<*, ApplicationCall>.uniload(
deserializer: DeserializationStrategy<T>
) = safely {
serialFormat.decodeDefault(
deserializer,
call.receive()
)
}
suspend fun PipelineContext<*, ApplicationCall>.getParameterOrSendError(
field: String
) = call.parameters[field].also {
if (it == null) {
call.respond(HttpStatusCode.BadRequest, "Request must contains $field")
}
}
fun PipelineContext<*, ApplicationCall>.getQueryParameter(
field: String
) = call.request.queryParameters[field]
suspend fun PipelineContext<*, ApplicationCall>.getQueryParameterOrSendError(
field: String
) = getQueryParameter(field).also {
if (it == null) {
call.respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field")
}
}
fun <T> PipelineContext<*, ApplicationCall>.decodeUrlQueryValue(
field: String,
deserializer: DeserializationStrategy<T>
) = getQueryParameter(field) ?.let {
serialFormat.decodeHex(
deserializer,
it
)
}
suspend fun <T> PipelineContext<*, ApplicationCall>.decodeUrlQueryValueOrSendError(
field: String,
deserializer: DeserializationStrategy<T>
) = decodeUrlQueryValue(field, deserializer).also {
if (it == null) {
call.respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field")
}
}
}
val defaultUnifiedRouter = UnifiedRouter()
suspend fun <T> ApplicationCall.unianswer( suspend fun <T> ApplicationCall.unianswer(
answerSerializer: SerializationStrategy<T>, answerSerializer: SerializationStrategy<T>,

View File

@ -1,13 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.client.crud package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
@ -26,7 +24,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsSerializerNullable: KSerializer<ObjectType?>, objectsSerializerNullable: KSerializer<ObjectType?>,
idsSerializer: KSerializer<IdType>, idsSerializer: KSerializer<IdType>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this ( ) : this (
baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, idsSerializer baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, idsSerializer
) )

View File

@ -1,10 +1,10 @@
package dev.inmo.micro_utils.repos.ktor.client.crud package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.UnifiedRequester import dev.inmo.micro_utils.ktor.client.UnifiedRequester
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
class KtorStandardCrudRepo<ObjectType, IdType, InputValue> ( class KtorStandardCrudRepo<ObjectType, IdType, InputValue> (
@ -39,7 +39,7 @@ class KtorStandardCrudRepo<ObjectType, IdType, InputValue> (
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>, inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>, idsSerializer: KSerializer<IdType>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this( ) : this(
baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer
) )

View File

@ -1,14 +1,12 @@
package dev.inmo.micro_utils.repos.ktor.client.crud package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.UpdatedValuePair import dev.inmo.micro_utils.repos.UpdatedValuePair
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* import kotlinx.serialization.builtins.*
@ -36,7 +34,7 @@ class KtorWriteStandardCrudRepo<ObjectType, IdType, InputValue> (
objectsSerializerNullable: KSerializer<ObjectType?>, objectsSerializerNullable: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>, inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>, idsSerializer: KSerializer<IdType>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this ( ) : this (
baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, inputsSerializer, idsSerializer baseUrl, UnifiedRequester(client, serialFormat), objectsSerializer, objectsSerializerNullable, inputsSerializer, idsSerializer
) )

View File

@ -1,8 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.key_value.*
@ -24,7 +23,7 @@ class KtorReadStandardKeyValueRepo<Key, Value> (
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
valueNullableSerializer: KSerializer<Value?>, valueNullableSerializer: KSerializer<Value?>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this ( ) : this (
baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer
) )

View File

@ -1,6 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.ktor.client.UnifiedRequester import dev.inmo.micro_utils.ktor.client.UnifiedRequester
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
@ -34,6 +35,6 @@ class KtorStandartKeyValueRepo<K, V> (
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>, valueNullableSerializer: KSerializer<V?>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this(baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer) ) : this(baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer)
} }

View File

@ -1,13 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* import kotlinx.serialization.builtins.*
@ -25,7 +23,7 @@ class KtorWriteStandardKeyValueRepo<K, V> (
client: HttpClient, client: HttpClient,
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this ( ) : this (
baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer
) )

View File

@ -1,10 +1,10 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.UnifiedRequester import dev.inmo.micro_utils.ktor.client.UnifiedRequester
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
class KtorOneToManyKeyValueRepo<Key, Value>( class KtorOneToManyKeyValueRepo<Key, Value>(
@ -32,6 +32,6 @@ class KtorOneToManyKeyValueRepo<Key, Value>(
client: HttpClient, client: HttpClient,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) ) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer)
} }

View File

@ -1,8 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.keyParameterName import dev.inmo.micro_utils.repos.ktor.common.keyParameterName
@ -10,7 +9,6 @@ import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
@ -28,7 +26,7 @@ class KtorReadOneToManyKeyValueRepo<Key, Value> (
client: HttpClient, client: HttpClient,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this (baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) ) : this (baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer)
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> = unifiedRequester.uniget( override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> = unifiedRequester.uniget(

View File

@ -1,13 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.ktor.common.*
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.BinaryFormat
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* import kotlinx.serialization.builtins.*
@ -25,7 +23,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
client: HttpClient, client: HttpClient,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
serialFormat: BinaryFormat = standardKtorSerialFormat serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat
) : this ( ) : this (
baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer baseUrl, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer
) )

View File

@ -1,12 +1,14 @@
package dev.inmo.micro_utils.repos.ktor.server.crud package dev.inmo.micro_utils.repos.ktor.server.crud
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.unianswer import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -16,47 +18,65 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>, originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>,
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
idsSerializer: KSerializer<IdType> idsSerializer: KSerializer<IdType>,
unifiedRouter: UnifiedRouter
) { ) {
val paginationResultSerializer = PaginationResult.serializer(objectsSerializer) val paginationResultSerializer = PaginationResult.serializer(objectsSerializer)
get(getByPaginationRouting) { get(getByPaginationRouting) {
unifiedRouter.apply {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
call.unianswer( unianswer(
paginationResultSerializer, paginationResultSerializer,
originalRepo.getByPagination(pagination) originalRepo.getByPagination(pagination)
) )
} }
}
get(getByIdRouting) { get(getByIdRouting) {
val id = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val id = decodeUrlQueryValueOrSendError(
"id", "id",
idsSerializer idsSerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
objectsNullableSerializer, objectsNullableSerializer,
originalRepo.getById(id) originalRepo.getById(id)
) )
} }
}
get(containsRouting) { get(containsRouting) {
val id = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val id = decodeUrlQueryValueOrSendError(
"id", "id",
idsSerializer idsSerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(id) originalRepo.contains(id)
) )
} }
}
get(countRouting) { get(countRouting) {
call.unianswer( unifiedRouter.apply {
unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count() originalRepo.count()
) )
} }
}
} }
inline fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>,
objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,6 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.server.crud package dev.inmo.micro_utils.repos.ktor.server.crud
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.UnifiedRouter
import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType
import dev.inmo.micro_utils.repos.StandardCRUDRepo import dev.inmo.micro_utils.repos.StandardCRUDRepo
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.route import io.ktor.routing.route
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -11,10 +16,24 @@ fun <ObjectType, IdType, InputValue> Route.configureStandardCrudRepoRoutes(
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>, inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType> idsSerializer: KSerializer<IdType>,
unifiedRouter: UnifiedRouter
) { ) {
route(baseSubpart) { route(baseSubpart) {
configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer) configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, unifiedRouter)
configureWriteStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer) configureWriteStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, unifiedRouter)
} }
} }
fun <ObjectType, IdType, InputValue> Route.configureStandardCrudRepoRoutes(
baseSubpart: String,
originalRepo: StandardCRUDRepo<ObjectType, IdType, InputValue>,
objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureStandardCrudRepoRoutes(
baseSubpart, originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,9 +1,12 @@
package dev.inmo.micro_utils.repos.ktor.server.crud package dev.inmo.micro_utils.repos.ktor.server.crud
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -14,7 +17,8 @@ fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
objectsSerializer: KSerializer<ObjectType>, objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>, objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>, inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType> idsSerializer: KSerializer<IdType>,
unifiedRouter: UnifiedRouter
) { ) {
val listObjectsSerializer = ListSerializer(objectsSerializer) val listObjectsSerializer = ListSerializer(objectsSerializer)
val listInputSerializer = ListSerializer(inputsSerializer) val listInputSerializer = ListSerializer(inputsSerializer)
@ -25,6 +29,7 @@ fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
) )
val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer) val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer)
unifiedRouter.apply {
includeWebsocketHandling( includeWebsocketHandling(
newObjectsFlowRouting, newObjectsFlowRouting,
originalRepo.newObjectsFlow, originalRepo.newObjectsFlow,
@ -40,43 +45,64 @@ fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
originalRepo.deletedObjectsIdsFlow, originalRepo.deletedObjectsIdsFlow,
idsSerializer idsSerializer
) )
}
post(createRouting) { post(createRouting) {
call.unianswer( unifiedRouter.apply {
unianswer(
listObjectsSerializer, listObjectsSerializer,
originalRepo.create( originalRepo.create(
call.uniload(listInputSerializer) uniload(listInputSerializer)
) )
) )
} }
}
post(updateRouting) { post(updateRouting) {
val (id, input) = call.uniload(inputUpdateSerializer) unifiedRouter.apply {
call.unianswer( val (id, input) = uniload(inputUpdateSerializer)
unianswer(
objectsNullableSerializer, objectsNullableSerializer,
originalRepo.update( originalRepo.update(
id, input id, input
) )
) )
} }
}
post(updateManyRouting) { post(updateManyRouting) {
val updates = call.uniload(listInputUpdateSerializer) unifiedRouter.apply {
call.unianswer( val updates = uniload(listInputUpdateSerializer)
unianswer(
listObjectsSerializer, listObjectsSerializer,
originalRepo.update( originalRepo.update(
updates updates
) )
) )
} }
}
post(deleteByIdRouting) { post(deleteByIdRouting) {
val ids = call.uniload(listIdsSerializer) unifiedRouter.apply {
call.unianswer( val ids = uniload(listIdsSerializer)
unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.deleteById( originalRepo.deleteById(
ids ids
) )
) )
} }
}
} }
fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
originalRepo: WriteStandardCRUDRepo<ObjectType, IdType, InputValue>,
objectsSerializer: KSerializer<ObjectType>,
objectsNullableSerializer: KSerializer<ObjectType?>,
inputsSerializer: KSerializer<InputValue>,
idsSerializer: KSerializer<IdType>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureWriteStandardCrudRepoRoutes(
originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,16 +1,22 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value package dev.inmo.micro_utils.repos.ktor.server.key_value
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.UnifiedRouter
import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType
import dev.inmo.micro_utils.repos.StandardKeyValueRepo import dev.inmo.micro_utils.repos.StandardKeyValueRepo
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.route import io.ktor.routing.route
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
fun <K, V> Route.configureStandartKeyValueRepoRoutes( fun <K, V> Route.configureStandardKeyValueRepoRoutes(
baseSubpart: String, baseSubpart: String,
originalRepo: StandardKeyValueRepo<K, V>, originalRepo: StandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>, valueNullableSerializer: KSerializer<V?>,
unifiedRouter: UnifiedRouter
) { ) {
route(baseSubpart) { route(baseSubpart) {
configureReadStandartKeyValueRepoRoutes( configureReadStandartKeyValueRepoRoutes(
@ -18,11 +24,23 @@ fun <K, V> Route.configureStandartKeyValueRepoRoutes(
keySerializer, keySerializer,
valueSerializer, valueSerializer,
valueNullableSerializer, valueNullableSerializer,
unifiedRouter
) )
configureWriteStandartKeyValueRepoRoutes( configureWriteStandardKeyValueRepoRoutes(
originalRepo, originalRepo,
keySerializer, keySerializer,
valueSerializer, valueSerializer,
unifiedRouter
) )
} }
} }
fun <K, V> Route.configureStandartKeyValueRepoRoutes(
baseSubpart: String,
originalRepo: StandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureStandardKeyValueRepoRoutes(baseSubpart, originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,5 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value package dev.inmo.micro_utils.repos.ktor.server.key_value
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.pagination.extractPagination
@ -7,6 +9,7 @@ import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -17,62 +20,84 @@ fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>, valueNullableSerializer: KSerializer<V?>,
unifiedRouter: UnifiedRouter
) { ) {
get(getRoute) { get(getRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
valueNullableSerializer, valueNullableSerializer,
originalRepo.get(key) originalRepo.get(key)
) )
} }
}
get(valuesRoute) { get(valuesRoute) {
unifiedRouter.apply {
val parination = call.request.queryParameters.extractPagination; val parination = call.request.queryParameters.extractPagination;
val reversed = call.decodeUrlQueryValueOrSendError( val reversed = decodeUrlQueryValueOrSendError(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
PaginationResult.serializer(valueSerializer), PaginationResult.serializer(valueSerializer),
originalRepo.values(parination, reversed) originalRepo.values(parination, reversed)
) )
} }
}
get(keysRoute) { get(keysRoute) {
unifiedRouter.apply {
val parination = call.request.queryParameters.extractPagination; val parination = call.request.queryParameters.extractPagination;
val reversed = call.decodeUrlQueryValueOrSendError( val reversed = decodeUrlQueryValueOrSendError(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: return@get ) ?: return@get
val value = call.decodeUrlQueryValue(valueParameterName, valueSerializer) val value = decodeUrlQueryValue(valueParameterName, valueSerializer)
call.unianswer( unianswer(
PaginationResult.serializer(keySerializer), PaginationResult.serializer(keySerializer),
value ?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed) value?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed)
) )
} }
}
get(containsRoute) { get(containsRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(key) originalRepo.contains(key)
) )
} }
}
get(countRoute) { get(countRoute) {
call.unianswer( unifiedRouter.apply {
unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count() originalRepo.count()
) )
} }
}
} }
inline fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
originalRepo: ReadStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureReadStandartKeyValueRepoRoutes(
originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,21 +1,25 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value package dev.inmo.micro_utils.repos.ktor.server.key_value
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.application.call import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* import kotlinx.serialization.builtins.*
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes ( fun <K, V> Route.configureWriteStandardKeyValueRepoRoutes (
originalRepo: WriteStandardKeyValueRepo<K, V>, originalRepo: WriteStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>, keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>, valueSerializer: KSerializer<V>,
unifiedRouter: UnifiedRouter
) { ) {
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
val keysListSerializer = ListSerializer(keySerializer) val keysListSerializer = ListSerializer(keySerializer)
unifiedRouter.apply {
includeWebsocketHandling( includeWebsocketHandling(
onNewValueRoute, onNewValueRoute,
originalRepo.onNewValue, originalRepo.onNewValue,
@ -27,18 +31,31 @@ fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
originalRepo.onValueRemoved, originalRepo.onValueRemoved,
keySerializer keySerializer
) )
}
post(setRoute) { post(setRoute) {
val toSet = call.uniload( unifiedRouter.apply {
val toSet = uniload(
keyValueMapSerializer keyValueMapSerializer
) )
call.unianswer(Unit.serializer(), originalRepo.set(toSet)) unianswer(Unit.serializer(), originalRepo.set(toSet))
}
} }
post(unsetRoute) { post(unsetRoute) {
val toUnset = call.uniload(keysListSerializer) unifiedRouter.apply {
val toUnset = uniload(keysListSerializer)
call.unianswer(Unit.serializer(), originalRepo.unset(toUnset)) unianswer(Unit.serializer(), originalRepo.unset(toUnset))
}
} }
} }
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
originalRepo: WriteStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureWriteStandardKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,6 +1,11 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many package dev.inmo.micro_utils.repos.ktor.server.one_to_many
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.UnifiedRouter
import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.route import io.ktor.routing.route
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -9,10 +14,22 @@ fun <Key, Value> Route.configureOneToManyKeyValueRepoRoutes(
baseSubpart: String, baseSubpart: String,
originalRepo: OneToManyKeyValueRepo<Key, Value>, originalRepo: OneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
unifiedRouter: UnifiedRouter
) { ) {
route(baseSubpart) { route(baseSubpart) {
configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer) configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter)
configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer) configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter)
} }
} }
fun <Key, Value> Route.configureOneToManyKeyValueRepoRoutes(
baseSubpart: String,
originalRepo: OneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureOneToManyKeyValueRepoRoutes(
baseSubpart, originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
)

View File

@ -1,5 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many package dev.inmo.micro_utils.repos.ktor.server.one_to_many
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.pagination.extractPagination
@ -10,6 +12,7 @@ import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -18,89 +21,110 @@ import kotlinx.serialization.builtins.serializer
fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes( fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
originalRepo: ReadOneToManyKeyValueRepo<Key, Value>, originalRepo: ReadOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
unifiedRouter: UnifiedRouter
) { ) {
val paginationKeyResult = PaginationResult.serializer(keySerializer) val paginationKeyResult = PaginationResult.serializer(keySerializer)
val paginationValueResult = PaginationResult.serializer(valueSealizer) val paginationValueResult = PaginationResult.serializer(valueSerializer)
get(getRoute) { get(getRoute) {
unifiedRouter.apply {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
val key = call.decodeUrlQueryValueOrSendError( val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
val reversed = call.decodeUrlQueryValue( val reversed = decodeUrlQueryValue(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: false ) ?: false
call.unianswer( unianswer(
paginationValueResult, paginationValueResult,
originalRepo.get(key, pagination, reversed) originalRepo.get(key, pagination, reversed)
) )
} }
}
get(keysRoute) { get(keysRoute) {
unifiedRouter.apply {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
val reversed = call.decodeUrlQueryValue( val reversed = decodeUrlQueryValue(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: false ) ?: false
val value: Value? = call.decodeUrlQueryValue( val value: Value? = decodeUrlQueryValue(
valueParameterName, valueParameterName,
valueSealizer valueSerializer
) )
call.unianswer( unianswer(
paginationKeyResult, paginationKeyResult,
value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed) value?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed)
) )
} }
}
get(containsByKeyRoute) { get(containsByKeyRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(key) originalRepo.contains(key)
) )
} }
}
get(containsByKeyValueRoute) { get(containsByKeyValueRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
val value = call.decodeUrlQueryValueOrSendError( val value = decodeUrlQueryValueOrSendError(
valueParameterName, valueParameterName,
valueSealizer valueSerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Boolean.serializer(), Boolean.serializer(),
originalRepo.contains(key, value) originalRepo.contains(key, value)
) )
} }
}
get(countByKeyRoute) { get(countByKeyRoute) {
val key = call.decodeUrlQueryValueOrSendError( unifiedRouter.apply {
val key = decodeUrlQueryValueOrSendError(
keyParameterName, keyParameterName,
keySerializer keySerializer
) ?: return@get ) ?: return@get
call.unianswer( unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count(key) originalRepo.count(key)
) )
} }
}
get(countRoute) { get(countRoute) {
call.unianswer( unifiedRouter.apply {
unianswer(
Long.serializer(), Long.serializer(),
originalRepo.count() originalRepo.count()
) )
} }
}
} }
inline fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
originalRepo: ReadOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))

View File

@ -1,9 +1,12 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many package dev.inmo.micro_utils.repos.ktor.server.one_to_many
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -13,10 +16,12 @@ fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>, originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
unifiedRouter: UnifiedRouter
) { ) {
val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer)) val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
unifiedRouter.apply {
includeWebsocketHandling( includeWebsocketHandling(
onNewValueRoute, onNewValueRoute,
originalRepo.onNewValue, originalRepo.onNewValue,
@ -32,42 +37,57 @@ fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo.onDataCleared, originalRepo.onDataCleared,
keySerializer keySerializer
) )
}
post(addRoute) { post(addRoute) {
val obj = call.uniload(keyValueMapSerializer) unifiedRouter.apply {
val obj = uniload(keyValueMapSerializer)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.add(obj) originalRepo.add(obj)
) )
} }
}
post(removeRoute) { post(removeRoute) {
val obj = call.uniload( unifiedRouter.apply {
keyValueMapSerializer val obj = uniload(keyValueMapSerializer)
)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.remove(obj), originalRepo.remove(obj),
) )
} }
}
post(clearRoute) { post(clearRoute) {
val key = call.uniload(keySerializer) unifiedRouter.apply {
val key = uniload(keySerializer)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.clear(key), originalRepo.clear(key),
) )
} }
}
post(setRoute) { post(setRoute) {
val obj = call.uniload(keyValueMapSerializer) unifiedRouter.apply {
val obj = uniload(keyValueMapSerializer)
call.unianswer( unianswer(
Unit.serializer(), Unit.serializer(),
originalRepo.set(obj) originalRepo.set(obj)
) )
} }
}
} }
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
) = configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))