fixes of todos

This commit is contained in:
InsanusMokrassar 2020-08-26 11:32:11 +06:00
parent 01ab884e68
commit 83fd9f264a
6 changed files with 74 additions and 53 deletions

View File

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

View File

@ -2,12 +2,12 @@ package com.insanusmokrassar.postssystem.ktor.server
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
import io.ktor.response.respondBytes
import io.ktor.util.toByteArray
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.*
suspend fun <T> ApplicationCall.unianswer(
answerSerializer: SerializationStrategy<T>,
@ -26,6 +26,23 @@ suspend fun <T> ApplicationCall.uniload(
request.receiveChannel().toByteArray()
)
fun <T> ApplicationCall.uniloadFromQuery(
parameterName: String,
deserializer: DeserializationStrategy<T>
): T? {
return standardKtorSerialFormat.decodeFromHexString(
deserializer,
getQueryParameter(parameterName) ?: return null
)
}
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 +51,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

@ -1,11 +1,9 @@
package com.insanusmokrassar.postssystem.utils.repos.ktor.client
import com.insanusmokrassar.postssystem.ktor.asUrlQueryParts
import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
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.unipost
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
@ -31,8 +29,8 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
baseUrl,
getRoute,
mapOf(
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k),
reversedParameterName to standardKtorSerialFormat.encodeToHexString(reversed)
keyParameterName to k.toHex(keySerializer),
reversedParameterName to reversed.toHex(Boolean.serializer())
) + pagination.asUrlQueryParts
),
paginationValueResultSerializer
@ -43,7 +41,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
baseUrl,
keysRoute,
mapOf(
reversedParameterName to standardKtorSerialFormat.encodeToHexString(reversed)
reversedParameterName to reversed.toHex(Boolean.serializer())
) + pagination.asUrlQueryParts
),
paginationKeyResultSerializer
@ -53,7 +51,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
buildStandardUrl(
baseUrl,
containsByKeyRoute,
mapOf(keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k))
mapOf(keyParameterName to k.toHex(keySerializer))
),
Boolean.serializer()
)
@ -63,8 +61,8 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
baseUrl,
containsByKeyValueRoute,
mapOf(
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k),
valueParameterName to standardKtorSerialFormat.encodeToHexString(valueSerializer, v),
keyParameterName to k.toHex(keySerializer),
valueParameterName to v.toHex(valueSerializer),
)
),
Boolean.serializer()
@ -75,7 +73,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
baseUrl,
countByKeyRoute,
mapOf(
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k) // TODO: Леша, придумай короче запись, типа keySerializer.toHexString(k)
keyParameterName to k.toHex(keySerializer)
)
),
Long.serializer()

View File

@ -30,7 +30,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
baseUrl,
getByIdRouting,
mapOf(
"id" to standardKtorSerialFormat.encodeToHexString(idsSerializer, id)
"id" to id.toHex(idsSerializer)
)
),
objectsSerializerNullable
@ -41,7 +41,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
baseUrl,
containsRouting,
mapOf(
"id" to standardKtorSerialFormat.encodeToHexString(idsSerializer, id)
"id" to id.toHex(idsSerializer)
)
),
Boolean.serializer()

View File

@ -1,11 +1,7 @@
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.server.*
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
@ -27,14 +23,14 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
get(getRoute) {
val pagination = call.request.queryParameters.extractPagination
val key = standardKtorSerialFormat.decodeFromHexString(
keySerializer,
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
) // TODO: Леша, а это можно сократить как-то до call.decodeFromQueryHexParameter(keySerializer, keyParameterName)?
val reversed = standardKtorSerialFormat.decodeFromHexString(
Boolean.serializer(),
call.getQueryParameterOrSendError(reversedParameterName) ?: return@get
)
val key = call.uniloadFromQueryOrSendError(
keyParameterName,
keySerializer
) ?: return@get
val reversed = call.uniloadFromQuery(
reversedParameterName,
Boolean.serializer()
) ?: false
call.unianswer(
paginationValueResult,
@ -44,10 +40,10 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
get(keysRoute) {
val pagination = call.request.queryParameters.extractPagination
val reversed = standardKtorSerialFormat.decodeFromHexString(
Boolean.serializer(),
call.getQueryParameterOrSendError(reversedParameterName) ?: return@get
)
val reversed = call.uniloadFromQuery(
reversedParameterName,
Boolean.serializer()
) ?: false
call.unianswer(
paginationKeyResult,
@ -68,14 +64,14 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
}
get(containsByKeyValueRoute) {
val key = standardKtorSerialFormat.decodeFromHexString(
keySerializer,
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
val value = standardKtorSerialFormat.decodeFromHexString(
valueSealizer,
call.getQueryParameterOrSendError(valueParameterName) ?: return@get
)
val key = call.uniloadFromQueryOrSendError(
keyParameterName,
keySerializer
) ?: return@get
val value = call.uniloadFromQueryOrSendError(
valueParameterName,
valueSealizer
) ?: return@get
call.unianswer(
Boolean.serializer(),
@ -84,10 +80,10 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
}
get(countByKeyRoute) {
val key = standardKtorSerialFormat.decodeFromHexString(
keySerializer,
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
)
val key = call.uniloadFromQueryOrSendError(
keyParameterName,
keySerializer
) ?: return@get
call.unianswer(
Long.serializer(),

View File

@ -30,10 +30,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 +42,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(),