mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 03:58:45 +00:00
new ReadOneToManyKeyValueRepo#keys
This commit is contained in:
parent
498cd12f94
commit
3ba630684a
@ -2,6 +2,10 @@
|
||||
|
||||
## 0.4.0
|
||||
|
||||
* `Repos`:
|
||||
* `ReadOneToManyKeyValueRepo` got `keys` method with value parameter
|
||||
* All implementations inside of this library has been updated
|
||||
|
||||
## 0.3.3
|
||||
|
||||
* `Coroutines`:
|
||||
|
@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
|
||||
suspend fun get(k: Key, 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(k: Key): Boolean
|
||||
suspend fun contains(k: Key, v: Value): Boolean
|
||||
suspend fun count(k: Key): Long
|
||||
|
@ -42,6 +42,23 @@ open class MapperReadOneToManyKeyValueRepo<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(k: FromKey): Boolean = to.contains(k.toOutKey())
|
||||
override suspend fun contains(k: FromKey, v: FromValue): Boolean = to.contains(k.toOutKey(), v.toOutValue())
|
||||
|
||||
|
@ -169,6 +169,33 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun keys(
|
||||
v: Value,
|
||||
pagination: Pagination,
|
||||
reversed: Boolean
|
||||
): PaginationResult<Key> = count().let { count ->
|
||||
val resultPagination = pagination.let { if (reversed) pagination.reverse(count) else pagination }
|
||||
helper.readableTransaction {
|
||||
select(
|
||||
tableName,
|
||||
selection = "$valueColumnName=?",
|
||||
selectionArgs = arrayOf(v.asValue()),
|
||||
limit = resultPagination.limitClause()
|
||||
).use { c ->
|
||||
mutableListOf<Key>().also {
|
||||
if (c.moveToFirst()) {
|
||||
do {
|
||||
it.add(c.getString(idColumnName).asKey())
|
||||
} while (c.moveToNext())
|
||||
}
|
||||
}
|
||||
}
|
||||
}.createPaginationResult(
|
||||
pagination,
|
||||
count
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||
helper.writableTransaction {
|
||||
toRemove.flatMap { (k, vs) ->
|
||||
|
@ -39,6 +39,21 @@ open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
|
||||
count()
|
||||
)
|
||||
|
||||
override suspend fun keys(
|
||||
v: Value,
|
||||
pagination: Pagination,
|
||||
reversed: Boolean
|
||||
): PaginationResult<Key> = transaction(database) {
|
||||
select { valueColumn.eq(v) }.let {
|
||||
it.count() to it.paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
|
||||
}
|
||||
}.let { (count, list) ->
|
||||
list.createPaginationResult(
|
||||
pagination,
|
||||
count
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun contains(k: Key): Boolean = transaction(database) {
|
||||
select { keyColumn.eq(k) }.limit(1).any()
|
||||
}
|
||||
|
@ -32,6 +32,18 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
|
||||
val keys = map.keys.filter { map[it] ?.contains(v) == true }
|
||||
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(k: Key): Boolean = map.containsKey(k)
|
||||
|
||||
override suspend fun contains(k: Key, v: Value): Boolean = map[k] ?.contains(v) == true
|
||||
|
@ -42,6 +42,18 @@ class KtorReadOneToManyKeyValueRepo<Key, Value> (
|
||||
paginationKeyResultSerializer
|
||||
)
|
||||
|
||||
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
|
||||
),
|
||||
paginationKeyResultSerializer
|
||||
)
|
||||
|
||||
override suspend fun contains(k: Key): Boolean = client.uniget(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
|
@ -42,8 +42,17 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
|
||||
reversedParameterName,
|
||||
Boolean.serializer()
|
||||
) ?: false
|
||||
val value: Value? = call.decodeUrlQueryValue(
|
||||
valueParameterName,
|
||||
valueSealizer
|
||||
)
|
||||
|
||||
call.unianswer(
|
||||
value ?.also {
|
||||
call.unianswer(
|
||||
paginationKeyResult,
|
||||
originalRepo.keys(value, pagination, reversed)
|
||||
)
|
||||
} ?: call.unianswer(
|
||||
paginationKeyResult,
|
||||
originalRepo.keys(pagination, reversed)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user