mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-11-24 16:26:12 +00:00
ids in crud repos
This commit is contained in:
@@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface ReadCRUDRepo<ObjectType, IdType> : Repo {
|
||||
suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType>
|
||||
suspend fun getIdsByPagination(pagination: Pagination): PaginationResult<IdType>
|
||||
suspend fun getById(id: IdType): ObjectType?
|
||||
suspend fun contains(id: IdType): Boolean
|
||||
suspend fun count(): Long
|
||||
|
||||
@@ -20,6 +20,14 @@ open class MapperReadCRUDRepo<FromId, FromRegistered, ToId, ToRegistered>(
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun getIdsByPagination(pagination: Pagination): PaginationResult<FromId> = to.getIdsByPagination(
|
||||
pagination
|
||||
).let {
|
||||
it.changeResultsUnchecked(
|
||||
it.results.map { it.toInnerKey() }
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun count(): Long = to.count()
|
||||
|
||||
override suspend fun contains(id: FromId): Boolean = to.contains(id.toOutKey())
|
||||
|
||||
@@ -16,6 +16,7 @@ abstract class AbstractAndroidCRUDRepo<ObjectType, IdType>(
|
||||
protected abstract val tableName: String
|
||||
protected abstract val idColumnName: String
|
||||
protected abstract suspend fun Cursor.toObject(): ObjectType
|
||||
protected abstract suspend fun Cursor.toId(): IdType
|
||||
protected fun SQLiteDatabase.count(): Long = select(tableName).use {
|
||||
it.count
|
||||
}.toLong()
|
||||
@@ -64,4 +65,23 @@ abstract class AbstractAndroidCRUDRepo<ObjectType, IdType>(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getIdsByPagination(pagination: Pagination): PaginationResult<IdType> {
|
||||
return helper.readableTransaction {
|
||||
select(
|
||||
tableName,
|
||||
limit = pagination.limitClause()
|
||||
).use {
|
||||
if (it.moveToFirst()) {
|
||||
val resultList = mutableListOf(it.toId())
|
||||
while (it.moveToNext()) {
|
||||
resultList.add(it.toId())
|
||||
}
|
||||
resultList.createPaginationResult(pagination, count())
|
||||
} else {
|
||||
emptyList<IdType>().createPaginationResult(pagination, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user