Merge branch 'onetomanyktorimpl' of PostsSystem/Core into master

This commit is contained in:
Aleksei Ovsiannikov 2020-08-26 05:54:59 +00:00 committed by Gogs
commit 00068a064f
29 changed files with 405 additions and 62 deletions

View File

@ -11,7 +11,6 @@ import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.soywiz.klock.DateTime
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import kotlinx.serialization.builtins.nullable
class ReadPostsRepoKtorClient(

View File

@ -5,7 +5,6 @@ import com.insanusmokrassar.postssystem.core.content.api.WriteContentRepo
import com.insanusmokrassar.postssystem.core.ktor.*
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.post

View File

@ -5,7 +5,6 @@ import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.post.repo.WritePostsRepo
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.post

View File

@ -0,0 +1,6 @@
package com.insanusmokrassar.postssystem.ktor
import kotlinx.serialization.*
fun <T> T.toHex(with: SerializationStrategy<T>) = standardKtorSerialFormat.encodeToHexString(with, this)
fun <T> String.fromHex(with: DeserializationStrategy<T>): T = standardKtorSerialFormat.decodeFromHexString(with, this)

View File

@ -8,7 +8,6 @@ import io.ktor.routing.Route
import io.ktor.websocket.webSocket
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
private suspend fun DefaultWebSocketSession.checkReceivedAndCloseIfExists() {

View File

@ -1,5 +1,6 @@
package com.insanusmokrassar.postssystem.ktor.server
import com.insanusmokrassar.postssystem.ktor.fromHex
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.ApplicationCall
import io.ktor.http.HttpStatusCode
@ -26,6 +27,18 @@ suspend fun <T> ApplicationCall.uniload(
request.receiveChannel().toByteArray()
)
fun <T> ApplicationCall.uniloadFromQuery(
parameterName: String,
deserializer: DeserializationStrategy<T>
): T? = getQueryParameter(parameterName) ?.fromHex(deserializer)
suspend fun <T> ApplicationCall.uniloadFromQueryOrSendError(
parameterName: String,
deserializer: DeserializationStrategy<T>
): T? = uniloadFromQuery(parameterName, deserializer) ?: null.also {
respond(HttpStatusCode.BadRequest, "Request query parameters must contains $parameterName")
}
suspend fun ApplicationCall.getParameterOrSendError(
field: String
) = parameters[field].also {
@ -34,10 +47,14 @@ suspend fun ApplicationCall.getParameterOrSendError(
}
}
fun ApplicationCall.getQueryParameter(
field: String
) = request.queryParameters[field]
suspend fun ApplicationCall.getQueryParameterOrSendError(
field: String
) = request.queryParameters[field].also {
) = getQueryParameter(field).also {
if (it == null) {
respond(HttpStatusCode.BadRequest, "Request query parametersmust contains $field")
respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field")
}
}

View File

@ -3,7 +3,7 @@ package com.insanusmokrassar.postssystem.ktor.server.configurators
import io.ktor.application.Application
import io.ktor.application.install
import io.ktor.features.StatusPages
import kotlinx.serialization.*
import kotlinx.serialization.Contextual
class StatusPagesConfigurator(
private val elements: List<@Contextual Element>

View File

@ -3,7 +3,6 @@ package com.insanusmokrassar.postssystem.ktor.tests
import com.insanusmokrassar.postssystem.ktor.client.createStandardWebsocketFlow
import com.insanusmokrassar.postssystem.ktor.server.createKtorServer
import com.insanusmokrassar.postssystem.ktor.server.includeWebsocketHandling
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.install
import io.ktor.client.HttpClient
import io.ktor.routing.routing

View File

@ -1,8 +1,6 @@
package com.insanusmokrassar.postssystem.publishing.ktor.client
import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo
import com.insanusmokrassar.postssystem.core.publishing.repos.ReadPublishingKeysRepo
import com.insanusmokrassar.postssystem.core.publishing.repos.WritePublishingKeysRepo
import com.insanusmokrassar.postssystem.core.publishing.repos.*
import com.insanusmokrassar.postssystem.publishing.ktor.publishingKeysRootRoute
import io.ktor.client.HttpClient
import io.ktor.client.features.websocket.WebSockets

View File

@ -6,9 +6,7 @@ import com.insanusmokrassar.postssystem.core.publishing.repos.WritePublishingKey
import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
import com.insanusmokrassar.postssystem.ktor.client.BodyPair
import com.insanusmokrassar.postssystem.ktor.client.unipost
import com.insanusmokrassar.postssystem.publishing.ktor.SetPostTriggerControlKeyObject
import com.insanusmokrassar.postssystem.publishing.ktor.setPostTriggerControlKeyRoute
import com.insanusmokrassar.postssystem.publishing.ktor.unsetPostTriggerControlKeyRoute
import com.insanusmokrassar.postssystem.publishing.ktor.*
import io.ktor.client.HttpClient
import kotlinx.serialization.builtins.serializer

View File

@ -4,9 +4,7 @@ import com.insanusmokrassar.postssystem.core.post.PostId
import com.insanusmokrassar.postssystem.core.publishing.repos.WritePublishingKeysRepo
import com.insanusmokrassar.postssystem.ktor.server.unianswer
import com.insanusmokrassar.postssystem.ktor.server.uniload
import com.insanusmokrassar.postssystem.publishing.ktor.SetPostTriggerControlKeyObject
import com.insanusmokrassar.postssystem.publishing.ktor.setPostTriggerControlKeyRoute
import com.insanusmokrassar.postssystem.publishing.ktor.unsetPostTriggerControlKeyRoute
import com.insanusmokrassar.postssystem.publishing.ktor.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.post

View File

@ -1,6 +1,6 @@
package com.insanusmokrassar.postssystem.utils.common
import kotlinx.serialization.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ByteArraySerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder

View File

@ -1,9 +1,11 @@
package com.insanusmokrassar.postssystem.utils.common
import kotlinx.serialization.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.collections.set
private val mimesCache = mutableMapOf<String, MimeType>().also {
knownMimeTypes.forEach { mimeType -> it[mimeType.raw] = mimeType }

View File

@ -1,15 +1,14 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client
package com.insanusmokrassar.postssystem.utils.repos.ktor.client.crud
import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.uniget
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.crud.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encodeToHexString
class KtorReadStandardCrudRepo<ObjectType, IdType> (
private val baseUrl: String,
@ -30,7 +29,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
baseUrl,
getByIdRouting,
mapOf(
"id" to standardKtorSerialFormat.encodeToHexString(idsSerializer, id)
"id" to id.toHex(idsSerializer)
)
),
objectsSerializerNullable
@ -41,7 +40,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
baseUrl,
containsRouting,
mapOf(
"id" to standardKtorSerialFormat.encodeToHexString(idsSerializer, id)
"id" to id.toHex(idsSerializer)
)
),
Boolean.serializer()

View File

@ -1,4 +1,4 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client
package com.insanusmokrassar.postssystem.utils.repos.ktor.client.crud
import com.insanusmokrassar.postssystem.utils.repos.*
import io.ktor.client.HttpClient

View File

@ -1,9 +1,10 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client
package com.insanusmokrassar.postssystem.utils.repos.ktor.client.crud
import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
import com.insanusmokrassar.postssystem.ktor.client.*
import com.insanusmokrassar.postssystem.utils.repos.*
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import com.insanusmokrassar.postssystem.utils.repos.UpdatedValuePair
import com.insanusmokrassar.postssystem.utils.repos.WriteStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.crud.*
import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.KSerializer

View File

@ -0,0 +1,25 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client.one_to_many
import com.insanusmokrassar.postssystem.utils.repos.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
class KtorOneToManyKeyValueRepo<Key, Value>(
baseUrl: String,
baseSubpart: String,
client: HttpClient,
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
) : OneToManyKeyValueRepo<Key, Value>,
OneToManyReadKeyValueRepo<Key, Value> by KtorOneToManyReadKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
keySerializer,
valueSerializer,
),
OneToManyWriteKeyValueRepo<Key, Value> by KtorOneToManyWriteKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
keySerializer,
valueSerializer,
)

View File

@ -0,0 +1,85 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client.one_to_many
import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.uniget
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.one_to_many.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
class KtorOneToManyReadKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>,
) : OneToManyReadKeyValueRepo<Key, Value> {
private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer)
private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer)
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> = client.uniget(
buildStandardUrl(
baseUrl,
getRoute,
mapOf(
keyParameterName to k.toHex(keySerializer),
reversedParameterName to reversed.toHex(Boolean.serializer())
) + pagination.asUrlQueryParts
),
paginationValueResultSerializer
)
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget(
buildStandardUrl(
baseUrl,
keysRoute,
mapOf(
reversedParameterName to reversed.toHex(Boolean.serializer())
) + pagination.asUrlQueryParts
),
paginationKeyResultSerializer
)
override suspend fun contains(k: Key): Boolean = client.uniget(
buildStandardUrl(
baseUrl,
containsByKeyRoute,
mapOf(keyParameterName to k.toHex(keySerializer))
),
Boolean.serializer()
)
override suspend fun contains(k: Key, v: Value): Boolean = client.uniget(
buildStandardUrl(
baseUrl,
containsByKeyValueRoute,
mapOf(
keyParameterName to k.toHex(keySerializer),
valueParameterName to v.toHex(valueSerializer),
)
),
Boolean.serializer()
)
override suspend fun count(k: Key): Long = client.uniget(
buildStandardUrl(
baseUrl,
countByKeyRoute,
mapOf(
keyParameterName to k.toHex(keySerializer)
)
),
Long.serializer()
)
override suspend fun count(): Long = client.uniget(
buildStandardUrl(
baseUrl,
countRoute,
),
Long.serializer()
)
}

View File

@ -0,0 +1,47 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client.one_to_many
import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
import com.insanusmokrassar.postssystem.ktor.client.BodyPair
import com.insanusmokrassar.postssystem.ktor.client.unipost
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
class KtorOneToManyWriteKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>,
) : OneToManyWriteKeyValueRepo<Key, Value> {
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
override suspend fun add(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
addRoute,
),
BodyPair(keyValueSerializer, k to v),
Unit.serializer(),
)
override suspend fun remove(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
removeRoute,
),
BodyPair(keyValueSerializer, k to v),
Unit.serializer(),
)
override suspend fun clear(k: Key) = client.unipost(
buildStandardUrl(
baseUrl,
clearRoute,
),
BodyPair(keySerializer, k),
Unit.serializer(),
)
}

View File

@ -1,4 +1,4 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.common
package com.insanusmokrassar.postssystem.utils.repos.ktor.common.crud
const val getByPaginationRouting = "getByPagination"
const val getByIdRouting = "getById"

View File

@ -1,4 +1,4 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.common
package com.insanusmokrassar.postssystem.utils.repos.ktor.common.crud
const val newObjectsFlowRouting = "newObjectsFlow"
const val updatedObjectsFlowRouting = "updatedObjectsFlow"

View File

@ -0,0 +1,5 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.common.one_to_many
const val keyParameterName = "key"
const val valueParameterName = "value"
const val reversedParameterName = "reversed"

View File

@ -0,0 +1,12 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.common.one_to_many
const val getRoute = "get"
const val keysRoute = "keys"
const val containsByKeyRoute = "containsByKey"
const val containsByKeyValueRoute = "containsByKeyValue"
const val countByKeyRoute = "countByKey"
const val countRoute = "count"
const val addRoute = "add"
const val removeRoute = "remove"
const val clearRoute = "clear"

View File

@ -1,16 +1,14 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server
package com.insanusmokrassar.postssystem.utils.repos.ktor.server.crud
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.crud.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.get
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.decodeFromHexString
fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>,
@ -30,10 +28,10 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
}
get(getByIdRouting) {
val id = standardKtorSerialFormat.decodeFromHexString(
idsSerializer,
call.getQueryParameterOrSendError("id") ?: return@get
)
val id = call.uniloadFromQueryOrSendError(
"id",
idsSerializer
) ?: return@get
call.unianswer(
objectsNullableSerializer,
@ -42,10 +40,10 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
}
get(containsRouting) {
val id = standardKtorSerialFormat.decodeFromHexString(
idsSerializer,
call.getQueryParameterOrSendError("id") ?: return@get
)
val id = call.uniloadFromQueryOrSendError(
"id",
idsSerializer
) ?: return@get
call.unianswer(
Boolean.serializer(),

View File

@ -1,16 +1,9 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server
package com.insanusmokrassar.postssystem.utils.repos.ktor.server.crud
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.*
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.application.call
import io.ktor.routing.*
import io.ktor.routing.get
import com.insanusmokrassar.postssystem.utils.repos.StandardCRUDRepo
import io.ktor.routing.Route
import io.ktor.routing.route
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.*
import kotlinx.serialization.decodeFromHexString
fun <ObjectType, IdType, InputValue> Route.configureStandardCrudRepoRoutes(
baseSubpart: String,

View File

@ -1,17 +1,13 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server
package com.insanusmokrassar.postssystem.utils.repos.ktor.server.crud
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.WriteStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.crud.*
import io.ktor.application.call
import io.ktor.routing.*
import io.ktor.routing.get
import io.ktor.routing.Route
import io.ktor.routing.post
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.*
import kotlinx.serialization.decodeFromHexString
fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
originalRepo: WriteStandardCRUDRepo<ObjectType, IdType, InputValue>,

View File

@ -0,0 +1,18 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server.one_to_many
import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
import io.ktor.routing.Route
import io.ktor.routing.route
import kotlinx.serialization.KSerializer
fun <Key, Value> Route.configureOneToManyKeyValueRepoRoutes(
baseSubpart: String,
originalRepo: OneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>,
) {
route(baseSubpart) {
configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer)
configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSealizer)
}
}

View File

@ -0,0 +1,99 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server.one_to_many
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.one_to_many.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.get
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 = call.uniloadFromQueryOrSendError(
keyParameterName,
keySerializer
) ?: return@get
val reversed = call.uniloadFromQuery(
reversedParameterName,
Boolean.serializer()
) ?: false
call.unianswer(
paginationValueResult,
originalRepo.get(key, pagination, reversed)
)
}
get(keysRoute) {
val pagination = call.request.queryParameters.extractPagination
val reversed = call.uniloadFromQuery(
reversedParameterName,
Boolean.serializer()
) ?: false
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 = call.uniloadFromQueryOrSendError(
keyParameterName,
keySerializer
) ?: return@get
val value = call.uniloadFromQueryOrSendError(
valueParameterName,
valueSealizer
) ?: return@get
call.unianswer(
Boolean.serializer(),
originalRepo.contains(key, value)
)
}
get(countByKeyRoute) {
val key = call.uniloadFromQueryOrSendError(
keyParameterName,
keySerializer
) ?: return@get
call.unianswer(
Long.serializer(),
originalRepo.count(key)
)
}
get(countRoute) {
call.unianswer(
Long.serializer(),
originalRepo.count()
)
}
}

View File

@ -0,0 +1,51 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server.one_to_many
import com.insanusmokrassar.postssystem.ktor.server.unianswer
import com.insanusmokrassar.postssystem.ktor.server.uniload
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.one_to_many.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.post
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: OneToManyWriteKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>,
) {
val keyValueSerializer = PairSerializer(keySerializer, valueSealizer)
post(addRoute) {
val obj = call.uniload(
keyValueSerializer
)
call.unianswer(
Unit.serializer(),
originalRepo.add(obj.first, obj.second)
)
}
post(removeRoute) {
val obj = call.uniload(
keyValueSerializer
)
call.unianswer(
Unit.serializer(),
originalRepo.remove(obj.first, obj.second),
)
}
post(clearRoute) {
val key = call.uniload(keySerializer)
call.unianswer(
Unit.serializer(),
originalRepo.clear(key),
)
}
}