This commit is contained in:
InsanusMokrassar 2020-08-26 11:36:41 +06:00
parent 83fd9f264a
commit 6961a866df
12 changed files with 149 additions and 138 deletions

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.ktor.* import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.uniget import com.insanusmokrassar.postssystem.ktor.client.uniget
@ -9,7 +9,6 @@ import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encodeToHexString
class KtorReadStandardCrudRepo<ObjectType, IdType> ( class KtorReadStandardCrudRepo<ObjectType, IdType> (
private val baseUrl: String, private val baseUrl: String,

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 com.insanusmokrassar.postssystem.utils.repos.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient

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.ktor.* import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.* import com.insanusmokrassar.postssystem.ktor.client.*

View File

@ -0,0 +1,27 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client.one_to_many
import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import io.ktor.client.*
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

@ -1,19 +1,14 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client package com.insanusmokrassar.postssystem.utils.repos.ktor.client.one_to_many
import com.insanusmokrassar.postssystem.ktor.* import com.insanusmokrassar.postssystem.ktor.*
import com.insanusmokrassar.postssystem.ktor.client.BodyPair
import com.insanusmokrassar.postssystem.ktor.client.uniget import com.insanusmokrassar.postssystem.ktor.client.uniget
import com.insanusmokrassar.postssystem.ktor.client.unipost
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult 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.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.* import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.client.* import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encodeToHexString
class KtorOneToManyReadKeyValueRepo<Key, Value> ( class KtorOneToManyReadKeyValueRepo<Key, Value> (
private val baseUrl: String, private val baseUrl: String,
@ -88,57 +83,3 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
) )
} }
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> {
override suspend fun add(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
addRoute,
),
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
Unit.serializer(),
)
override suspend fun remove(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
removeRoute,
),
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
Unit.serializer(),
)
override suspend fun clear(k: Key) = client.unipost(
buildStandardUrl(
baseUrl,
clearRoute,
),
BodyPair(keySerializer, k),
Unit.serializer(),
)
}
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,44 @@
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.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
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> {
override suspend fun add(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
addRoute,
),
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
Unit.serializer(),
)
override suspend fun remove(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
removeRoute,
),
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
Unit.serializer(),
)
override suspend fun clear(k: Key) = client.unipost(
buildStandardUrl(
baseUrl,
clearRoute,
),
BodyPair(keySerializer, k),
Unit.serializer(),
)
}

View File

@ -1,7 +1,6 @@
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.server.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.* import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
@ -10,7 +9,6 @@ import io.ktor.routing.Route
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.decodeFromHexString
fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes( fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>, originalRepo: ReadStandardCRUDRepo<ObjectType, IdType>,

View File

@ -1,16 +1,8 @@
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.*
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.application.call
import io.ktor.routing.* import io.ktor.routing.*
import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.*
import kotlinx.serialization.decodeFromHexString
fun <ObjectType, IdType, InputValue> Route.configureStandardCrudRepoRoutes( fun <ObjectType, IdType, InputValue> Route.configureStandardCrudRepoRoutes(
baseSubpart: String, baseSubpart: String,

View File

@ -1,17 +1,12 @@
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.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.WriteStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.* import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.application.call import io.ktor.application.call
import io.ktor.routing.* import io.ktor.routing.*
import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.* import kotlinx.serialization.builtins.*
import kotlinx.serialization.decodeFromHexString
fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes( fun <ObjectType, IdType, InputValue> Route.configureWriteStandardCrudRepoRoutes(
originalRepo: WriteStandardCRUDRepo<ObjectType, IdType, InputValue>, originalRepo: WriteStandardCRUDRepo<ObjectType, IdType, InputValue>,

View File

@ -0,0 +1,17 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server.one_to_many
import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
import io.ktor.routing.*
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

@ -1,14 +1,13 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.server package com.insanusmokrassar.postssystem.utils.repos.ktor.server.one_to_many
import com.insanusmokrassar.postssystem.ktor.server.* import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult 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.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.ktor.common.* import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
import io.ktor.application.* import io.ktor.application.call
import io.ktor.routing.* import io.ktor.routing.Route
import io.ktor.routing.get
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.decodeFromHexString import kotlinx.serialization.decodeFromHexString
@ -98,52 +97,3 @@ 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)
}
}

View File

@ -0,0 +1,48 @@
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.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.post
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
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),
)
}
}