add all client realizations for core repos
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package com.insanusmokrassar.postssystem.utils.repos
|
||||
|
||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination
|
||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult
|
||||
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
|
||||
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
|
||||
|
||||
interface OneToManyReadKeyValueRepo<Key, Value> : Repo {
|
||||
suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package com.insanusmokrassar.postssystem.utils.repos
|
||||
|
||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination
|
||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult
|
||||
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
|
||||
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface ReadStandardCRUDRepo<ObjectType, IdType> : Repo {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package com.insanusmokrassar.postssystem.utils.repos
|
||||
|
||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination
|
||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult
|
||||
import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
|
||||
import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface StandardReadKeyValueRepo<Key, Value> : Repo {
|
||||
|
@@ -1,61 +0,0 @@
|
||||
package com.insanusmokrassar.postssystem.utils.repos.pagination
|
||||
|
||||
import kotlin.math.ceil
|
||||
|
||||
/**
|
||||
* Base interface of pagination
|
||||
*
|
||||
* If you want to request something, you should use [SimplePagination]. If you need to return some result including
|
||||
* pagination - [PaginationResult]
|
||||
*/
|
||||
interface Pagination {
|
||||
/**
|
||||
* Started with 0.
|
||||
* Number of page inside of pagination. Offset can be calculated as [page] * [size]
|
||||
*/
|
||||
val page: Int
|
||||
/**
|
||||
* Can be 0, but can't be < 0
|
||||
* Size of current page. Offset can be calculated as [page] * [size]
|
||||
*/
|
||||
val size: Int
|
||||
}
|
||||
|
||||
/**
|
||||
* First number in index of objects. It can be used as offset for databases or other data sources
|
||||
*/
|
||||
val Pagination.firstIndex: Int
|
||||
get() = page * size
|
||||
|
||||
/**
|
||||
* Last exclusive number in index of objects
|
||||
*
|
||||
* For [Pagination] with [Pagination.size] == 10 and [Pagination.page] == 1
|
||||
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastExclusiveIndex] == 20.
|
||||
*/
|
||||
val Pagination.lastExclusiveIndex: Int
|
||||
get() = firstIndex + size
|
||||
|
||||
/**
|
||||
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
|
||||
*
|
||||
* [[firstIndex], [lastIndex]]; That means, that for [Pagination] with [Pagination.size] == 10 and [Pagination.page] == 1
|
||||
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19.
|
||||
*/
|
||||
val Pagination.lastIndex: Int
|
||||
get() = lastExclusiveIndex - 1
|
||||
|
||||
/**
|
||||
* Calculates pages count for given [datasetSize]
|
||||
*/
|
||||
fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int {
|
||||
return ceil(datasetSize.toDouble() / pageSize).toInt()
|
||||
}
|
||||
/**
|
||||
* Calculates pages count for given [datasetSize]
|
||||
*/
|
||||
fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int =
|
||||
calculatePagesNumber(
|
||||
datasetSize.toLong(),
|
||||
pageSize
|
||||
)
|
@@ -1,21 +0,0 @@
|
||||
package com.insanusmokrassar.postssystem.utils.repos.pagination
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class PaginationResult<T>(
|
||||
override val page: Int,
|
||||
val pagesNumber: Int,
|
||||
val results: List<T>,
|
||||
override val size: Int
|
||||
) : Pagination
|
||||
|
||||
fun <T> List<T>.createPaginationResult(
|
||||
pagination: Pagination,
|
||||
commonObjectsNumber: Long
|
||||
) = PaginationResult(
|
||||
pagination.page,
|
||||
calculatePagesNumber(commonObjectsNumber, pagination.size),
|
||||
this,
|
||||
pagination.size
|
||||
)
|
@@ -1,28 +0,0 @@
|
||||
package com.insanusmokrassar.postssystem.utils.repos.pagination
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
const val defaultSmallPageSize = 2
|
||||
const val defaultMediumPageSize = 5
|
||||
const val defaultLargePageSize = 10
|
||||
const val defaultExtraLargePageSize = 15
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE", "FunctionName")
|
||||
inline fun FirstPagePagination(size: Int = defaultMediumPageSize) =
|
||||
SimplePagination(
|
||||
page = 0,
|
||||
size = size
|
||||
)
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun Pagination.nextPage() =
|
||||
SimplePagination(
|
||||
page + 1,
|
||||
size
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class SimplePagination(
|
||||
override val page: Int,
|
||||
override val size: Int
|
||||
) : Pagination
|
Reference in New Issue
Block a user