From b165a76e626c985f4083e7800bc313c511a3c625 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 30 Aug 2022 15:20:45 +0600 Subject: [PATCH] fixes in selectPaginated --- .../micro_utils/pagination/QueryExtensions.kt | 6 +--- .../keyvalue/ExposedReadKeyValueRepo.kt | 29 ++++++++++++------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/pagination/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/QueryExtensions.kt b/pagination/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/QueryExtensions.kt index 06a3da5c52a..616c28e0b7b 100644 --- a/pagination/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/QueryExtensions.kt +++ b/pagination/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/QueryExtensions.kt @@ -4,11 +4,7 @@ import org.jetbrains.exposed.sql.* fun Query.paginate(with: Pagination, orderBy: Pair, SortOrder>? = null) = limit( with.size, - (if (orderBy ?.second == SortOrder.DESC) { - with.lastIndex - } else { - with.firstIndex - }).toLong() + with.firstIndex.toLong() ).let { if (orderBy != null) { it.orderBy( diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt index 7fa979d4a26..5ea319f8d02 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt @@ -3,6 +3,7 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.exposed.* +import dev.inmo.micro_utils.repos.exposed.utils.selectPaginated import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction @@ -29,24 +30,32 @@ open class ExposedReadKeyValueRepo( override suspend fun count(): Long = transaction(database) { selectAll().count() } override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = transaction(database) { - selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map { + selectAll().selectPaginated( + pagination, + keyColumn, + reversed + ) { it[keyColumn] } - }.createPaginationResult(pagination, count()) + } override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult = transaction(database) { - select { valueColumn.eq(v) }.let { - it.count() to it.paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map { - it[keyColumn] - } + select { valueColumn.eq(v) }.selectPaginated( + pagination, + keyColumn, + reversed + ) { + it[keyColumn] } - }.let { (count, list) -> - list.createPaginationResult(pagination, count) } override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult = transaction(database) { - selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map { + selectAll().selectPaginated( + pagination, + keyColumn, + reversed + ) { it[valueColumn] } - }.createPaginationResult(pagination, count()) + } }