mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
add ReadStandardKeyValueRepo#keys with value parameter
This commit is contained in:
parent
3ba630684a
commit
5a9cabc4bd
@ -5,6 +5,8 @@
|
||||
* `Repos`:
|
||||
* `ReadOneToManyKeyValueRepo` got `keys` method with value parameter
|
||||
* All implementations inside of this library has been updated
|
||||
* `ReadStandardKeyValueRepo` got `keys` method with value parameter
|
||||
* All implementations inside of this library has been updated
|
||||
|
||||
## 0.3.3
|
||||
|
||||
|
@ -8,6 +8,7 @@ interface ReadStandardKeyValueRepo<Key, Value> : Repo {
|
||||
suspend fun get(k: Key): Value?
|
||||
suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
||||
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
||||
suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
||||
suspend fun contains(key: Key): Boolean
|
||||
suspend fun count(): Long
|
||||
}
|
||||
|
@ -44,6 +44,23 @@ open class MapperReadStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun keys(
|
||||
v: FromValue,
|
||||
pagination: Pagination,
|
||||
reversed: Boolean
|
||||
): PaginationResult<FromKey> = to.keys(
|
||||
v.toOutValue(),
|
||||
pagination,
|
||||
reversed
|
||||
).let {
|
||||
PaginationResult(
|
||||
it.page,
|
||||
it.pagesNumber,
|
||||
it.results.map { it.toInnerKey() },
|
||||
it.size
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun contains(key: FromKey): Boolean = to.contains(
|
||||
key.toOutKey()
|
||||
)
|
||||
|
@ -96,6 +96,20 @@ class KeyValueStore<T : Any> internal constructor (
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun keys(v: T, pagination: Pagination, reversed: Boolean): PaginationResult<String> {
|
||||
val resultPagination = if (reversed) pagination.reverse(count()) else pagination
|
||||
return sharedPreferences.all.mapNotNull { (k, value) -> if (value == v) k else null }.paginate(
|
||||
resultPagination
|
||||
).let {
|
||||
PaginationResult(
|
||||
it.page,
|
||||
it.pagesNumber,
|
||||
it.results.let { if (reversed) it.reversed() else it },
|
||||
it.size
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun contains(key: String): Boolean = sharedPreferences.contains(key)
|
||||
|
||||
override suspend fun count(): Long = sharedPreferences.all.size.toLong()
|
||||
|
@ -33,6 +33,16 @@ open class ExposedReadKeyValueRepo<Key, Value>(
|
||||
}
|
||||
}.createPaginationResult(pagination, count())
|
||||
|
||||
override suspend fun keys(value: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(database) {
|
||||
select { valueColumn.eq(value) }.let {
|
||||
it.count() to it.paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||
it[keyColumn]
|
||||
}
|
||||
}
|
||||
}.let { (count, list) ->
|
||||
list.createPaginationResult(pagination, count)
|
||||
}
|
||||
|
||||
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = transaction(database) {
|
||||
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||
it[valueColumn]
|
||||
|
@ -39,6 +39,18 @@ class ReadMapKeyValueRepo<Key, Value>(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
|
||||
val keys: List<Key> = map.mapNotNull { (k, value) -> if (v == value) k else null }
|
||||
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
|
||||
return keys.paginate(actualPagination).let {
|
||||
if (reversed) {
|
||||
it.copy(results = it.results.reversed())
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun contains(key: Key): Boolean = map.containsKey(key)
|
||||
|
||||
override suspend fun count(): Long = map.size.toLong()
|
||||
|
@ -6,6 +6,7 @@ import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
|
||||
import io.ktor.client.HttpClient
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
@ -50,6 +51,18 @@ class KtorReadStandardKeyValueRepo<Key, Value> (
|
||||
PaginationResult.serializer(keySerializer)
|
||||
)
|
||||
|
||||
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
keysRoute,
|
||||
mapOf(
|
||||
valueParameterName to valueSerializer.encodeUrlQueryValue(v),
|
||||
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed)
|
||||
) + pagination.asUrlQueryParts
|
||||
),
|
||||
PaginationResult.serializer(keySerializer)
|
||||
)
|
||||
|
||||
override suspend fun contains(key: Key): Boolean = client.uniget(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
|
@ -5,7 +5,10 @@ import dev.inmo.micro_utils.ktor.client.uniget
|
||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
||||
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.reversedParameterName
|
||||
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
|
||||
import io.ktor.client.HttpClient
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
|
@ -0,0 +1,5 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.common
|
||||
|
||||
const val keyParameterName = "key"
|
||||
const val valueParameterName = "value"
|
||||
const val reversedParameterName = "reversed"
|
@ -1,5 +1,8 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.common.one_to_many
|
||||
|
||||
const val keyParameterName = "key"
|
||||
const val valueParameterName = "value"
|
||||
const val reversedParameterName = "reversed"
|
||||
@Deprecated("Replaced", ReplaceWith("keyParameterName", "dev.inmo.micro_utils.repos.ktor.common.keyParameterName"))
|
||||
const val keyParameterName = dev.inmo.micro_utils.repos.ktor.common.keyParameterName
|
||||
@Deprecated("Replaced", ReplaceWith("valueParameterName", "dev.inmo.micro_utils.repos.ktor.common.valueParameterName"))
|
||||
const val valueParameterName = dev.inmo.micro_utils.repos.ktor.common.valueParameterName
|
||||
@Deprecated("Replaced", ReplaceWith("reversedParameterName", "dev.inmo.micro_utils.repos.ktor.common.reversedParameterName"))
|
||||
const val reversedParameterName = dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
|
@ -1,11 +1,11 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.server.key_value
|
||||
|
||||
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError
|
||||
import dev.inmo.micro_utils.ktor.server.unianswer
|
||||
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.ReadStandardKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
|
||||
import io.ktor.application.call
|
||||
import io.ktor.routing.Route
|
||||
import io.ktor.routing.get
|
||||
@ -49,10 +49,11 @@ fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
|
||||
reversedParameterName,
|
||||
Boolean.serializer()
|
||||
) ?: return@get
|
||||
val value = call.decodeUrlQueryValue(valueParameterName, valueSerializer)
|
||||
|
||||
call.unianswer(
|
||||
PaginationResult.serializer(keySerializer),
|
||||
originalRepo.keys(parination, reversed)
|
||||
value ?.let { originalRepo.keys(value, parination, reversed) } ?: originalRepo.keys(parination, reversed)
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,11 @@ 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.ReadOneToManyKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ktor.common.*
|
||||
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.application.call
|
||||
import io.ktor.routing.Route
|
||||
import io.ktor.routing.get
|
||||
@ -47,14 +51,9 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
||||
valueSealizer
|
||||
)
|
||||
|
||||
value ?.also {
|
||||
call.unianswer(
|
||||
paginationKeyResult,
|
||||
originalRepo.keys(value, pagination, reversed)
|
||||
)
|
||||
} ?: call.unianswer(
|
||||
paginationKeyResult,
|
||||
originalRepo.keys(pagination, reversed)
|
||||
value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed)
|
||||
)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user