fixes of todos
This commit is contained in:
parent
01ab884e68
commit
83fd9f264a
@ -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)
|
@ -2,12 +2,12 @@ package com.insanusmokrassar.postssystem.ktor.server
|
|||||||
|
|
||||||
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
|
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
|
||||||
import io.ktor.application.ApplicationCall
|
import io.ktor.application.ApplicationCall
|
||||||
|
import io.ktor.application.call
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
import io.ktor.response.respondBytes
|
import io.ktor.response.respondBytes
|
||||||
import io.ktor.util.toByteArray
|
import io.ktor.util.toByteArray
|
||||||
import kotlinx.serialization.DeserializationStrategy
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.SerializationStrategy
|
|
||||||
|
|
||||||
suspend fun <T> ApplicationCall.unianswer(
|
suspend fun <T> ApplicationCall.unianswer(
|
||||||
answerSerializer: SerializationStrategy<T>,
|
answerSerializer: SerializationStrategy<T>,
|
||||||
@ -26,6 +26,23 @@ suspend fun <T> ApplicationCall.uniload(
|
|||||||
request.receiveChannel().toByteArray()
|
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(
|
suspend fun ApplicationCall.getParameterOrSendError(
|
||||||
field: String
|
field: String
|
||||||
) = parameters[field].also {
|
) = parameters[field].also {
|
||||||
@ -34,9 +51,13 @@ suspend fun ApplicationCall.getParameterOrSendError(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ApplicationCall.getQueryParameter(
|
||||||
|
field: String
|
||||||
|
) = request.queryParameters[field]
|
||||||
|
|
||||||
suspend fun ApplicationCall.getQueryParameterOrSendError(
|
suspend fun ApplicationCall.getQueryParameterOrSendError(
|
||||||
field: String
|
field: String
|
||||||
) = request.queryParameters[field].also {
|
) = getQueryParameter(field).also {
|
||||||
if (it == null) {
|
if (it == null) {
|
||||||
respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field")
|
respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field")
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package com.insanusmokrassar.postssystem.utils.repos.ktor.client
|
package com.insanusmokrassar.postssystem.utils.repos.ktor.client
|
||||||
|
|
||||||
import com.insanusmokrassar.postssystem.ktor.asUrlQueryParts
|
import com.insanusmokrassar.postssystem.ktor.*
|
||||||
import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
|
|
||||||
import com.insanusmokrassar.postssystem.ktor.client.BodyPair
|
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.ktor.client.unipost
|
||||||
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
|
|
||||||
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.OneToManyKeyValueRepo
|
||||||
@ -31,8 +29,8 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
|
|||||||
baseUrl,
|
baseUrl,
|
||||||
getRoute,
|
getRoute,
|
||||||
mapOf(
|
mapOf(
|
||||||
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k),
|
keyParameterName to k.toHex(keySerializer),
|
||||||
reversedParameterName to standardKtorSerialFormat.encodeToHexString(reversed)
|
reversedParameterName to reversed.toHex(Boolean.serializer())
|
||||||
) + pagination.asUrlQueryParts
|
) + pagination.asUrlQueryParts
|
||||||
),
|
),
|
||||||
paginationValueResultSerializer
|
paginationValueResultSerializer
|
||||||
@ -43,7 +41,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
|
|||||||
baseUrl,
|
baseUrl,
|
||||||
keysRoute,
|
keysRoute,
|
||||||
mapOf(
|
mapOf(
|
||||||
reversedParameterName to standardKtorSerialFormat.encodeToHexString(reversed)
|
reversedParameterName to reversed.toHex(Boolean.serializer())
|
||||||
) + pagination.asUrlQueryParts
|
) + pagination.asUrlQueryParts
|
||||||
),
|
),
|
||||||
paginationKeyResultSerializer
|
paginationKeyResultSerializer
|
||||||
@ -53,7 +51,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
|
|||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
containsByKeyRoute,
|
containsByKeyRoute,
|
||||||
mapOf(keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k))
|
mapOf(keyParameterName to k.toHex(keySerializer))
|
||||||
),
|
),
|
||||||
Boolean.serializer()
|
Boolean.serializer()
|
||||||
)
|
)
|
||||||
@ -63,8 +61,8 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
|
|||||||
baseUrl,
|
baseUrl,
|
||||||
containsByKeyValueRoute,
|
containsByKeyValueRoute,
|
||||||
mapOf(
|
mapOf(
|
||||||
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k),
|
keyParameterName to k.toHex(keySerializer),
|
||||||
valueParameterName to standardKtorSerialFormat.encodeToHexString(valueSerializer, v),
|
valueParameterName to v.toHex(valueSerializer),
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
Boolean.serializer()
|
Boolean.serializer()
|
||||||
@ -75,7 +73,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
|
|||||||
baseUrl,
|
baseUrl,
|
||||||
countByKeyRoute,
|
countByKeyRoute,
|
||||||
mapOf(
|
mapOf(
|
||||||
keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k) // TODO: Леша, придумай короче запись, типа keySerializer.toHexString(k)
|
keyParameterName to k.toHex(keySerializer)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
Long.serializer()
|
Long.serializer()
|
||||||
|
@ -30,7 +30,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
|
|||||||
baseUrl,
|
baseUrl,
|
||||||
getByIdRouting,
|
getByIdRouting,
|
||||||
mapOf(
|
mapOf(
|
||||||
"id" to standardKtorSerialFormat.encodeToHexString(idsSerializer, id)
|
"id" to id.toHex(idsSerializer)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
objectsSerializerNullable
|
objectsSerializerNullable
|
||||||
@ -41,7 +41,7 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
|
|||||||
baseUrl,
|
baseUrl,
|
||||||
containsRouting,
|
containsRouting,
|
||||||
mapOf(
|
mapOf(
|
||||||
"id" to standardKtorSerialFormat.encodeToHexString(idsSerializer, id)
|
"id" to id.toHex(idsSerializer)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
Boolean.serializer()
|
Boolean.serializer()
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
package com.insanusmokrassar.postssystem.utils.repos.ktor.server
|
package com.insanusmokrassar.postssystem.utils.repos.ktor.server
|
||||||
|
|
||||||
import com.insanusmokrassar.postssystem.ktor.server.extractPagination
|
import com.insanusmokrassar.postssystem.ktor.server.*
|
||||||
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.ktor.standardKtorSerialFormat
|
||||||
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.OneToManyKeyValueRepo
|
||||||
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
|
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
|
||||||
@ -27,14 +23,14 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
|||||||
|
|
||||||
get(getRoute) {
|
get(getRoute) {
|
||||||
val pagination = call.request.queryParameters.extractPagination
|
val pagination = call.request.queryParameters.extractPagination
|
||||||
val key = standardKtorSerialFormat.decodeFromHexString(
|
val key = call.uniloadFromQueryOrSendError(
|
||||||
keySerializer,
|
keyParameterName,
|
||||||
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
|
keySerializer
|
||||||
) // TODO: Леша, а это можно сократить как-то до call.decodeFromQueryHexParameter(keySerializer, keyParameterName)?
|
) ?: return@get
|
||||||
val reversed = standardKtorSerialFormat.decodeFromHexString(
|
val reversed = call.uniloadFromQuery(
|
||||||
Boolean.serializer(),
|
reversedParameterName,
|
||||||
call.getQueryParameterOrSendError(reversedParameterName) ?: return@get
|
Boolean.serializer()
|
||||||
)
|
) ?: false
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
paginationValueResult,
|
paginationValueResult,
|
||||||
@ -44,10 +40,10 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
|||||||
|
|
||||||
get(keysRoute) {
|
get(keysRoute) {
|
||||||
val pagination = call.request.queryParameters.extractPagination
|
val pagination = call.request.queryParameters.extractPagination
|
||||||
val reversed = standardKtorSerialFormat.decodeFromHexString(
|
val reversed = call.uniloadFromQuery(
|
||||||
Boolean.serializer(),
|
reversedParameterName,
|
||||||
call.getQueryParameterOrSendError(reversedParameterName) ?: return@get
|
Boolean.serializer()
|
||||||
)
|
) ?: false
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
paginationKeyResult,
|
paginationKeyResult,
|
||||||
@ -68,14 +64,14 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(containsByKeyValueRoute) {
|
get(containsByKeyValueRoute) {
|
||||||
val key = standardKtorSerialFormat.decodeFromHexString(
|
val key = call.uniloadFromQueryOrSendError(
|
||||||
keySerializer,
|
keyParameterName,
|
||||||
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
|
keySerializer
|
||||||
)
|
) ?: return@get
|
||||||
val value = standardKtorSerialFormat.decodeFromHexString(
|
val value = call.uniloadFromQueryOrSendError(
|
||||||
valueSealizer,
|
valueParameterName,
|
||||||
call.getQueryParameterOrSendError(valueParameterName) ?: return@get
|
valueSealizer
|
||||||
)
|
) ?: return@get
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
Boolean.serializer(),
|
Boolean.serializer(),
|
||||||
@ -84,10 +80,10 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(countByKeyRoute) {
|
get(countByKeyRoute) {
|
||||||
val key = standardKtorSerialFormat.decodeFromHexString(
|
val key = call.uniloadFromQueryOrSendError(
|
||||||
keySerializer,
|
keyParameterName,
|
||||||
call.getQueryParameterOrSendError(keyParameterName) ?: return@get
|
keySerializer
|
||||||
)
|
) ?: return@get
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
Long.serializer(),
|
Long.serializer(),
|
||||||
|
@ -30,10 +30,10 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(getByIdRouting) {
|
get(getByIdRouting) {
|
||||||
val id = standardKtorSerialFormat.decodeFromHexString(
|
val id = call.uniloadFromQueryOrSendError(
|
||||||
idsSerializer,
|
"id",
|
||||||
call.getQueryParameterOrSendError("id") ?: return@get
|
idsSerializer
|
||||||
)
|
) ?: return@get
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
objectsNullableSerializer,
|
objectsNullableSerializer,
|
||||||
@ -42,10 +42,10 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(containsRouting) {
|
get(containsRouting) {
|
||||||
val id = standardKtorSerialFormat.decodeFromHexString(
|
val id = call.uniloadFromQueryOrSendError(
|
||||||
idsSerializer,
|
"id",
|
||||||
call.getQueryParameterOrSendError("id") ?: return@get
|
idsSerializer
|
||||||
)
|
) ?: return@get
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
Boolean.serializer(),
|
Boolean.serializer(),
|
||||||
|
Loading…
Reference in New Issue
Block a user