add doForAll and getAll for every type of repo

This commit is contained in:
InsanusMokrassar 2020-10-02 19:13:04 +06:00
parent 083287a77b
commit 04e6c7b574
6 changed files with 109 additions and 18 deletions

View File

@ -8,4 +8,7 @@ Inited :)
* `Repos`
* `Common`
* Extension `ReadStandardCRUDRepo#doWithAll` has been added
* Extensions `doForAll` and `getAll` were added for all current types of repos:
* `ReadStandardCRUDRepo`
* `StandardReadKeyValueRepo`
* `OneToManyReadKeyValueRepo`

View File

@ -6,8 +6,8 @@ import kotlinx.coroutines.flow.Flow
interface StandardReadKeyValueRepo<Key, Value> : Repo {
suspend fun get(k: Key): Value?
suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value>
suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key>
suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
suspend fun contains(key: Key): Boolean
suspend fun count(): Long
}

View File

@ -0,0 +1,31 @@
package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
suspend inline fun <T, ID, REPO : ReadStandardCRUDRepo<T, ID>> REPO.doForAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<T>,
block: (List<T>) -> Unit
) {
doWithPagination {
methodCaller(it).also {
block(it.results)
}.nextPageIfNotEmpty()
}
}
suspend inline fun <T, ID, REPO : ReadStandardCRUDRepo<T, ID>> REPO.doForAll(
block: (List<T>) -> Unit
) = doForAll({ getByPagination(it) }, block)
suspend inline fun <T, ID, REPO : ReadStandardCRUDRepo<T, ID>> REPO.getAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<T>
): List<T> {
val resultList = mutableListOf<T>()
doForAll(methodCaller) {
resultList.addAll(it)
}
return resultList
}

View File

@ -0,0 +1,31 @@
package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
suspend inline fun <Key, Value, REPO : StandardReadKeyValueRepo<Key, Value>> REPO.doForAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>,
block: (List<Pair<Key, Value>>) -> Unit
) {
doWithPagination {
methodCaller(it).also { keys ->
block(keys.results.mapNotNull { key -> get(key) ?.let { value -> key to value } })
}.nextPageIfNotEmpty()
}
}
suspend inline fun <Key, Value, REPO : StandardReadKeyValueRepo<Key, Value>> REPO.doForAll(
block: (List<Pair<Key, Value>>) -> Unit
) = doForAll({ keys(it, false) }, block)
suspend inline fun <Key, Value, REPO : StandardReadKeyValueRepo<Key, Value>> REPO.getAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>
): List<Pair<Key, Value>> {
val resultList = mutableListOf<Pair<Key, Value>>()
doForAll(methodCaller) {
resultList.addAll(it)
}
return resultList
}

View File

@ -0,0 +1,41 @@
package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
suspend inline fun <Key, Value, REPO : OneToManyReadKeyValueRepo<Key, Value>> REPO.doForAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>,
block: (List<Pair<Key, List<Value>>>) -> Unit
) {
doWithPagination {
methodCaller(it).also { keys ->
block(
keys.results.mapNotNull { key ->
val values = mutableListOf<Value>()
doWithPagination {
get(key, it).also {
values.addAll(it.results)
}.nextPageIfNotEmpty()
}
key to values
}
)
}.nextPageIfNotEmpty()
}
}
suspend inline fun <Key, Value, REPO : OneToManyReadKeyValueRepo<Key, Value>> REPO.doForAll(
block: (List<Pair<Key, List<Value>>>) -> Unit
) = doForAll({ keys(it, false) }, block)
suspend inline fun <Key, Value, REPO : OneToManyReadKeyValueRepo<Key, Value>> REPO.getAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>
): List<Pair<Key, List<Value>>> {
val resultList = mutableListOf<Pair<Key, List<Value>>>()
doForAll(methodCaller) {
resultList.addAll(it)
}
return resultList
}

View File

@ -1,15 +0,0 @@
package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.doWithPagination
import dev.inmo.micro_utils.pagination.nextPageIfNotEmpty
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
suspend inline fun <T, ID, REPO : ReadStandardCRUDRepo<T, ID>> REPO.doWithAll(
block: (List<T>) -> Unit
) {
doWithPagination {
getByPagination(it).also {
block(it.results)
}.nextPageIfNotEmpty()
}
}