write kerver impl

This commit is contained in:
000Sanya 2020-08-26 13:11:13 +10:00
parent 20dba28f13
commit e9a1276ae1

View File

@ -3,10 +3,13 @@ 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.server.uniload
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.OneToManyKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.application.*
import io.ktor.routing.*
@ -99,3 +102,52 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
)
}
}
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: OneToManyWriteKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>,
) {
post(addRoute) {
val obj = call.uniload(
KeyValuePostObject.serializer(keySerializer, valueSealizer)
)
call.unianswer(
Unit.serializer(),
originalRepo.add(obj.key, obj.value)
)
}
post(removeRoute) {
val obj = call.uniload(
KeyValuePostObject.serializer(keySerializer, valueSealizer)
)
call.unianswer(
Unit.serializer(),
originalRepo.remove(obj.key, obj.value),
)
}
post(clearRoute) {
val key = call.uniload(keySerializer)
call.unianswer(
Unit.serializer(),
originalRepo.clear(key),
)
}
}
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)
}
}