mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
A LOT OF KTOR METHODS RELATED TO UnifierRouter/UnifiedRequester HAVE BEEN REMOVED
This commit is contained in:
parent
8055003b47
commit
bd33b09052
@ -3,6 +3,7 @@
|
||||
## 0.13.0
|
||||
|
||||
**ALL DEPRECATIONS HAVE BEEN REMOVED**
|
||||
**A LOT OF KTOR METHODS RELATED TO UnifierRouter/UnifiedRequester HAVE BEEN REMOVED**
|
||||
|
||||
* `Repos`:
|
||||
* `Exposed`:
|
||||
|
@ -9,11 +9,3 @@ expect suspend fun HttpClient.tempUpload(
|
||||
file: MPPFile,
|
||||
onUpload: (uploaded: Long, count: Long) -> Unit = { _, _ -> }
|
||||
): TemporalFileId
|
||||
|
||||
suspend fun UnifiedRequester.tempUpload(
|
||||
fullTempUploadDraftPath: String,
|
||||
file: MPPFile,
|
||||
onUpload: (uploaded: Long, count: Long) -> Unit = { _, _ -> }
|
||||
): TemporalFileId = client.tempUpload(
|
||||
fullTempUploadDraftPath, file, onUpload
|
||||
)
|
||||
|
@ -26,7 +26,6 @@ import java.nio.file.attribute.FileTime
|
||||
|
||||
class TemporalFilesRoutingConfigurator(
|
||||
private val subpath: String = DefaultTemporalFilesSubPath,
|
||||
private val unifiedRouter: UnifiedRouter = UnifiedRouter.default,
|
||||
private val temporalFilesUtilizer: TemporalFilesUtilizer = TemporalFilesUtilizer
|
||||
) : ApplicationRoutingConfigurator.Element {
|
||||
interface TemporalFilesUtilizer {
|
||||
@ -80,42 +79,40 @@ class TemporalFilesRoutingConfigurator(
|
||||
|
||||
override fun Route.invoke() {
|
||||
post(subpath) {
|
||||
unifiedRouter.apply {
|
||||
val multipart = call.receiveMultipart()
|
||||
val multipart = call.receiveMultipart()
|
||||
|
||||
var fileInfo: Pair<TemporalFileId, MPPFile>? = null
|
||||
var part = multipart.readPart()
|
||||
var fileInfo: Pair<TemporalFileId, MPPFile>? = null
|
||||
var part = multipart.readPart()
|
||||
|
||||
while (part != null) {
|
||||
if (part is PartData.FileItem) {
|
||||
break
|
||||
}
|
||||
part = multipart.readPart()
|
||||
while (part != null) {
|
||||
if (part is PartData.FileItem) {
|
||||
break
|
||||
}
|
||||
|
||||
part ?.let {
|
||||
if (it is PartData.FileItem) {
|
||||
val fileId = TemporalFileId(uuid4().toString())
|
||||
val fileName = it.originalFileName ?.let { FileName(it) } ?: return@let
|
||||
fileInfo = fileId to File.createTempFile(fileId.string, ".${fileName.extension}").apply {
|
||||
outputStream().use { outputStream ->
|
||||
it.streamProvider().use {
|
||||
it.copyTo(outputStream)
|
||||
}
|
||||
}
|
||||
deleteOnExit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileInfo ?.also { (fileId, file) ->
|
||||
temporalFilesMutex.withLock {
|
||||
temporalFilesMap[fileId] = file
|
||||
}
|
||||
call.respondText(fileId.string)
|
||||
launchSafelyWithoutExceptions { filesFlow.emit(fileId) }
|
||||
} ?: call.respond(HttpStatusCode.BadRequest)
|
||||
part = multipart.readPart()
|
||||
}
|
||||
|
||||
part ?.let {
|
||||
if (it is PartData.FileItem) {
|
||||
val fileId = TemporalFileId(uuid4().toString())
|
||||
val fileName = it.originalFileName ?.let { FileName(it) } ?: return@let
|
||||
fileInfo = fileId to File.createTempFile(fileId.string, ".${fileName.extension}").apply {
|
||||
outputStream().use { outputStream ->
|
||||
it.streamProvider().use {
|
||||
it.copyTo(outputStream)
|
||||
}
|
||||
}
|
||||
deleteOnExit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileInfo ?.also { (fileId, file) ->
|
||||
temporalFilesMutex.withLock {
|
||||
temporalFilesMap[fileId] = file
|
||||
}
|
||||
call.respondText(fileId.string)
|
||||
launchSafelyWithoutExceptions { filesFlow.emit(fileId) }
|
||||
} ?: call.respond(HttpStatusCode.BadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,83 +0,0 @@
|
||||
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.pagination.PaginationResult
|
||||
import dev.inmo.micro_utils.pagination.extractPagination
|
||||
import dev.inmo.micro_utils.repos.ReadCRUDRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.countRouting
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
|
||||
fun <ObjectType, IdType> Route.configureReadCRUDRepoRoutes(
|
||||
originalRepo: ReadCRUDRepo<ObjectType, IdType>,
|
||||
objectsSerializer: KSerializer<ObjectType>,
|
||||
objectsNullableSerializer: KSerializer<ObjectType?>,
|
||||
idsSerializer: KSerializer<IdType>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
val paginationResultSerializer = PaginationResult.serializer(objectsSerializer)
|
||||
|
||||
get(getByPaginationRouting) {
|
||||
unifiedRouter.apply {
|
||||
val pagination = call.request.queryParameters.extractPagination
|
||||
|
||||
unianswer(
|
||||
paginationResultSerializer,
|
||||
originalRepo.getByPagination(pagination)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(getByIdRouting) {
|
||||
unifiedRouter.apply {
|
||||
val id = decodeUrlQueryValueOrSendError(
|
||||
"id",
|
||||
idsSerializer
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
objectsNullableSerializer,
|
||||
originalRepo.getById(id)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(containsRouting) {
|
||||
unifiedRouter.apply {
|
||||
val id = decodeUrlQueryValueOrSendError(
|
||||
"id",
|
||||
idsSerializer
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
Boolean.serializer(),
|
||||
originalRepo.contains(id)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(countRouting) {
|
||||
unifiedRouter.apply {
|
||||
unianswer(
|
||||
Long.serializer(),
|
||||
originalRepo.count()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <ObjectType, IdType> Route.configureReadCRUDRepoRoutes(
|
||||
originalRepo: ReadCRUDRepo<ObjectType, IdType>,
|
||||
objectsSerializer: KSerializer<ObjectType>,
|
||||
objectsNullableSerializer: KSerializer<ObjectType?>,
|
||||
idsSerializer: KSerializer<IdType>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureReadCRUDRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType))
|
@ -1,39 +0,0 @@
|
||||
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.CRUDRepo
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.route
|
||||
import kotlinx.serialization.KSerializer
|
||||
|
||||
fun <ObjectType, IdType, InputValue> Route.configureCRUDRepoRoutes(
|
||||
baseSubpart: String,
|
||||
originalRepo: CRUDRepo<ObjectType, IdType, InputValue>,
|
||||
objectsSerializer: KSerializer<ObjectType>,
|
||||
objectsNullableSerializer: KSerializer<ObjectType?>,
|
||||
inputsSerializer: KSerializer<InputValue>,
|
||||
idsSerializer: KSerializer<IdType>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
route(baseSubpart) {
|
||||
configureReadCRUDRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, unifiedRouter)
|
||||
configureWriteCRUDRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, unifiedRouter)
|
||||
}
|
||||
}
|
||||
|
||||
fun <ObjectType, IdType, InputValue> Route.configureCRUDRepoRoutes(
|
||||
baseSubpart: String,
|
||||
originalRepo: CRUDRepo<ObjectType, IdType, InputValue>,
|
||||
objectsSerializer: KSerializer<ObjectType>,
|
||||
objectsNullableSerializer: KSerializer<ObjectType?>,
|
||||
inputsSerializer: KSerializer<InputValue>,
|
||||
idsSerializer: KSerializer<IdType>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureCRUDRepoRoutes(
|
||||
baseSubpart, originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
|
||||
)
|
@ -1,107 +0,0 @@
|
||||
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.repos.WriteCRUDRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.post
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.*
|
||||
|
||||
fun <ObjectType, IdType, InputValue> Route.configureWriteCRUDRepoRoutes(
|
||||
originalRepo: WriteCRUDRepo<ObjectType, IdType, InputValue>,
|
||||
objectsSerializer: KSerializer<ObjectType>,
|
||||
objectsNullableSerializer: KSerializer<ObjectType?>,
|
||||
inputsSerializer: KSerializer<InputValue>,
|
||||
idsSerializer: KSerializer<IdType>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
val listObjectsSerializer = ListSerializer(objectsSerializer)
|
||||
val listInputSerializer = ListSerializer(inputsSerializer)
|
||||
val listIdsSerializer = ListSerializer(idsSerializer)
|
||||
val inputUpdateSerializer = PairSerializer(
|
||||
idsSerializer,
|
||||
inputsSerializer
|
||||
)
|
||||
val listInputUpdateSerializer = ListSerializer(inputUpdateSerializer)
|
||||
|
||||
unifiedRouter.apply {
|
||||
includeWebsocketHandling(
|
||||
newObjectsFlowRouting,
|
||||
originalRepo.newObjectsFlow,
|
||||
objectsSerializer
|
||||
)
|
||||
includeWebsocketHandling(
|
||||
updatedObjectsFlowRouting,
|
||||
originalRepo.updatedObjectsFlow,
|
||||
objectsSerializer
|
||||
)
|
||||
includeWebsocketHandling(
|
||||
deletedObjectsIdsFlowRouting,
|
||||
originalRepo.deletedObjectsIdsFlow,
|
||||
idsSerializer
|
||||
)
|
||||
}
|
||||
|
||||
post(createRouting) {
|
||||
unifiedRouter.apply {
|
||||
unianswer(
|
||||
listObjectsSerializer,
|
||||
originalRepo.create(
|
||||
uniload(listInputSerializer)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
post(updateRouting) {
|
||||
unifiedRouter.apply {
|
||||
val (id, input) = uniload(inputUpdateSerializer)
|
||||
unianswer(
|
||||
objectsNullableSerializer,
|
||||
originalRepo.update(
|
||||
id, input
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
post(updateManyRouting) {
|
||||
unifiedRouter.apply {
|
||||
val updates = uniload(listInputUpdateSerializer)
|
||||
unianswer(
|
||||
listObjectsSerializer,
|
||||
originalRepo.update(
|
||||
updates
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
post(deleteByIdRouting) {
|
||||
unifiedRouter.apply {
|
||||
val ids = uniload(listIdsSerializer)
|
||||
unianswer(
|
||||
Unit.serializer(),
|
||||
originalRepo.deleteById(
|
||||
ids
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <ObjectType, IdType, InputValue> Route.configureWriteCRUDRepoRoutes(
|
||||
originalRepo: WriteCRUDRepo<ObjectType, IdType, InputValue>,
|
||||
objectsSerializer: KSerializer<ObjectType>,
|
||||
objectsNullableSerializer: KSerializer<ObjectType?>,
|
||||
inputsSerializer: KSerializer<InputValue>,
|
||||
idsSerializer: KSerializer<IdType>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureWriteCRUDRepoRoutes(
|
||||
originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
|
||||
)
|
@ -1,46 +0,0 @@
|
||||
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.KeyValueRepo
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.route
|
||||
import kotlinx.serialization.KSerializer
|
||||
|
||||
fun <K, V> Route.configureKeyValueRepoRoutes(
|
||||
baseSubpart: String,
|
||||
originalRepo: KeyValueRepo<K, V>,
|
||||
keySerializer: KSerializer<K>,
|
||||
valueSerializer: KSerializer<V>,
|
||||
valueNullableSerializer: KSerializer<V?>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
route(baseSubpart) {
|
||||
configureReadStandartKeyValueRepoRoutes(
|
||||
originalRepo,
|
||||
keySerializer,
|
||||
valueSerializer,
|
||||
valueNullableSerializer,
|
||||
unifiedRouter
|
||||
)
|
||||
configureWriteKeyValueRepoRoutes(
|
||||
originalRepo,
|
||||
keySerializer,
|
||||
valueSerializer,
|
||||
unifiedRouter
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun <K, V> Route.configureStandartKeyValueRepoRoutes(
|
||||
baseSubpart: String,
|
||||
originalRepo: KeyValueRepo<K, V>,
|
||||
keySerializer: KSerializer<K>,
|
||||
valueSerializer: KSerializer<V>,
|
||||
valueNullableSerializer: KSerializer<V?>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureKeyValueRepoRoutes(baseSubpart, originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType))
|
@ -1,107 +0,0 @@
|
||||
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.pagination.PaginationResult
|
||||
import dev.inmo.micro_utils.pagination.extractPagination
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.containsRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.countRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.keyParameterName
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.reversedParameterName
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
|
||||
fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
|
||||
originalRepo: ReadKeyValueRepo<K, V>,
|
||||
keySerializer: KSerializer<K>,
|
||||
valueSerializer: KSerializer<V>,
|
||||
valueNullableSerializer: KSerializer<V?>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
get(getRoute) {
|
||||
unifiedRouter.apply {
|
||||
val key = decodeUrlQueryValueOrSendError(
|
||||
keyParameterName,
|
||||
keySerializer
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
valueNullableSerializer,
|
||||
originalRepo.get(key)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(valuesRoute) {
|
||||
unifiedRouter.apply {
|
||||
val parination = call.request.queryParameters.extractPagination;
|
||||
val reversed = decodeUrlQueryValueOrSendError(
|
||||
reversedParameterName,
|
||||
Boolean.serializer()
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
PaginationResult.serializer(valueSerializer),
|
||||
originalRepo.values(parination, reversed)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(keysRoute) {
|
||||
unifiedRouter.apply {
|
||||
val parination = call.request.queryParameters.extractPagination;
|
||||
val reversed = decodeUrlQueryValueOrSendError(
|
||||
reversedParameterName,
|
||||
Boolean.serializer()
|
||||
) ?: return@get
|
||||
val value = decodeUrlQueryValue(valueParameterName, valueSerializer)
|
||||
|
||||
unianswer(
|
||||
PaginationResult.serializer(keySerializer),
|
||||
value?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(containsRoute) {
|
||||
unifiedRouter.apply {
|
||||
val key = decodeUrlQueryValueOrSendError(
|
||||
keyParameterName,
|
||||
keySerializer
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
Boolean.serializer(),
|
||||
originalRepo.contains(key)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(countRoute) {
|
||||
unifiedRouter.apply {
|
||||
unianswer(
|
||||
Long.serializer(),
|
||||
originalRepo.count()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
|
||||
originalRepo: ReadKeyValueRepo<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)
|
||||
)
|
@ -1,70 +0,0 @@
|
||||
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.repos.WriteKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.post
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.*
|
||||
|
||||
fun <K, V> Route.configureWriteKeyValueRepoRoutes (
|
||||
originalRepo: WriteKeyValueRepo<K, V>,
|
||||
keySerializer: KSerializer<K>,
|
||||
valueSerializer: KSerializer<V>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
||||
val keysListSerializer = ListSerializer(keySerializer)
|
||||
val valuesListSerializer = ListSerializer(valueSerializer)
|
||||
unifiedRouter.apply {
|
||||
includeWebsocketHandling(
|
||||
onNewValueRoute,
|
||||
originalRepo.onNewValue,
|
||||
PairSerializer(keySerializer, valueSerializer)
|
||||
)
|
||||
|
||||
includeWebsocketHandling(
|
||||
onValueRemovedRoute,
|
||||
originalRepo.onValueRemoved,
|
||||
keySerializer
|
||||
)
|
||||
}
|
||||
|
||||
post(setRoute) {
|
||||
unifiedRouter.apply {
|
||||
val toSet = uniload(
|
||||
keyValueMapSerializer
|
||||
)
|
||||
|
||||
unianswer(Unit.serializer(), originalRepo.set(toSet))
|
||||
}
|
||||
}
|
||||
|
||||
post(unsetRoute) {
|
||||
unifiedRouter.apply {
|
||||
val toUnset = uniload(keysListSerializer)
|
||||
|
||||
unianswer(Unit.serializer(), originalRepo.unset(toUnset))
|
||||
}
|
||||
}
|
||||
|
||||
post(unsetWithValuesRoute) {
|
||||
unifiedRouter.apply {
|
||||
val toUnset = uniload(valuesListSerializer)
|
||||
|
||||
unianswer(Unit.serializer(), originalRepo.unsetWithValues(toUnset))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
|
||||
originalRepo: WriteKeyValueRepo<K, V>,
|
||||
keySerializer: KSerializer<K>,
|
||||
valueSerializer: KSerializer<V>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))
|
@ -1,35 +0,0 @@
|
||||
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.KeyValuesRepo
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.route
|
||||
import kotlinx.serialization.KSerializer
|
||||
|
||||
fun <Key, Value> Route.configureOneToManyKeyValueRepoRoutes(
|
||||
baseSubpart: String,
|
||||
originalRepo: KeyValuesRepo<Key, Value>,
|
||||
keySerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
route(baseSubpart) {
|
||||
configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter)
|
||||
configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, unifiedRouter)
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> Route.configureOneToManyKeyValueRepoRoutes(
|
||||
baseSubpart: String,
|
||||
originalRepo: KeyValuesRepo<Key, Value>,
|
||||
keySerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureOneToManyKeyValueRepoRoutes(
|
||||
baseSubpart, originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)
|
||||
)
|
@ -1,129 +0,0 @@
|
||||
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.pagination.PaginationResult
|
||||
import dev.inmo.micro_utils.pagination.extractPagination
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.keyParameterName
|
||||
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.reversedParameterName
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
|
||||
fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
||||
originalRepo: ReadKeyValuesRepo<Key, Value>,
|
||||
keySerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
val paginationKeyResult = PaginationResult.serializer(keySerializer)
|
||||
val paginationValueResult = PaginationResult.serializer(valueSerializer)
|
||||
|
||||
get(getRoute) {
|
||||
unifiedRouter.apply {
|
||||
val pagination = call.request.queryParameters.extractPagination
|
||||
val key = decodeUrlQueryValueOrSendError(
|
||||
keyParameterName,
|
||||
keySerializer
|
||||
) ?: return@get
|
||||
val reversed = decodeUrlQueryValue(
|
||||
reversedParameterName,
|
||||
Boolean.serializer()
|
||||
) ?: false
|
||||
|
||||
unianswer(
|
||||
paginationValueResult,
|
||||
originalRepo.get(key, pagination, reversed)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(keysRoute) {
|
||||
unifiedRouter.apply {
|
||||
val pagination = call.request.queryParameters.extractPagination
|
||||
val reversed = decodeUrlQueryValue(
|
||||
reversedParameterName,
|
||||
Boolean.serializer()
|
||||
) ?: false
|
||||
val value: Value? = decodeUrlQueryValue(
|
||||
valueParameterName,
|
||||
valueSerializer
|
||||
)
|
||||
|
||||
unianswer(
|
||||
paginationKeyResult,
|
||||
value?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(containsByKeyRoute) {
|
||||
unifiedRouter.apply {
|
||||
val key = decodeUrlQueryValueOrSendError(
|
||||
keyParameterName,
|
||||
keySerializer
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
Boolean.serializer(),
|
||||
originalRepo.contains(key)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(containsByKeyValueRoute) {
|
||||
unifiedRouter.apply {
|
||||
val key = decodeUrlQueryValueOrSendError(
|
||||
keyParameterName,
|
||||
keySerializer
|
||||
) ?: return@get
|
||||
val value = decodeUrlQueryValueOrSendError(
|
||||
valueParameterName,
|
||||
valueSerializer
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
Boolean.serializer(),
|
||||
originalRepo.contains(key, value)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(countByKeyRoute) {
|
||||
unifiedRouter.apply {
|
||||
val key = decodeUrlQueryValueOrSendError(
|
||||
keyParameterName,
|
||||
keySerializer
|
||||
) ?: return@get
|
||||
|
||||
unianswer(
|
||||
Long.serializer(),
|
||||
originalRepo.count(key)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
get(countRoute) {
|
||||
unifiedRouter.apply {
|
||||
unianswer(
|
||||
Long.serializer(),
|
||||
originalRepo.count()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
||||
originalRepo: ReadKeyValuesRepo<Key, Value>,
|
||||
keySerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureOneToManyReadKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))
|
@ -1,103 +0,0 @@
|
||||
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.repos.WriteKeyValuesRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.post
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.*
|
||||
|
||||
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
|
||||
originalRepo: WriteKeyValuesRepo<Key, Value>,
|
||||
keySerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
unifiedRouter: UnifiedRouter
|
||||
) {
|
||||
val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
|
||||
val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
|
||||
|
||||
unifiedRouter.apply {
|
||||
includeWebsocketHandling(
|
||||
onNewValueRoute,
|
||||
originalRepo.onNewValue,
|
||||
keyValueSerializer
|
||||
)
|
||||
includeWebsocketHandling(
|
||||
onValueRemovedRoute,
|
||||
originalRepo.onValueRemoved,
|
||||
keyValueSerializer
|
||||
)
|
||||
includeWebsocketHandling(
|
||||
onDataClearedRoute,
|
||||
originalRepo.onDataCleared,
|
||||
keySerializer
|
||||
)
|
||||
}
|
||||
|
||||
post(addRoute) {
|
||||
unifiedRouter.apply {
|
||||
val obj = uniload(keyValueMapSerializer)
|
||||
|
||||
unianswer(
|
||||
Unit.serializer(),
|
||||
originalRepo.add(obj)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
post(removeRoute) {
|
||||
unifiedRouter.apply {
|
||||
val obj = uniload(keyValueMapSerializer)
|
||||
|
||||
unianswer(
|
||||
Unit.serializer(),
|
||||
originalRepo.remove(obj),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
post(clearRoute) {
|
||||
unifiedRouter.apply {
|
||||
val key = uniload(keySerializer)
|
||||
|
||||
unianswer(
|
||||
Unit.serializer(),
|
||||
originalRepo.clear(key),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
post(clearWithValueRoute) {
|
||||
unifiedRouter.apply {
|
||||
val v = uniload(valueSerializer)
|
||||
|
||||
unianswer(
|
||||
Unit.serializer(),
|
||||
originalRepo.clearWithValue(v),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
post(setRoute) {
|
||||
unifiedRouter.apply {
|
||||
val obj = uniload(keyValueMapSerializer)
|
||||
|
||||
unianswer(
|
||||
Unit.serializer(),
|
||||
originalRepo.set(obj)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
|
||||
originalRepo: WriteKeyValuesRepo<Key, Value>,
|
||||
keySerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat,
|
||||
serialFormatContentType: ContentType = standardKtorSerialFormatContentType
|
||||
) = configureOneToManyWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType))
|
Loading…
Reference in New Issue
Block a user