feat: 修正した実装を追加

This commit is contained in:
usbharu 2024-01-30 22:26:27 +09:00
parent 2efae6f6da
commit e028da5925
1 changed files with 17 additions and 4 deletions

View File

@ -1,9 +1,6 @@
package dev.usbharu.hideout.application.infrastructure.exposed
import org.jetbrains.exposed.sql.ExpressionWithColumnType
import org.jetbrains.exposed.sql.Query
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.*
fun <S> Query.pagination(page: Page, exp: ExpressionWithColumnType<S>): Query {
if (page.minId != null) {
@ -17,3 +14,19 @@ fun <S> Query.pagination(page: Page, exp: ExpressionWithColumnType<S>): Query {
page.limit?.let { limit(it) }
return this
}
fun <S> Query.withPagination(page: Page, exp: ExpressionWithColumnType<S>): PaginationList<ResultRow, S> {
page.limit?.let { limit(it) }
val resultRows = if (page.minId != null) {
page.maxId?.let { andWhere { exp.less(it) } }
page.minId?.let { andWhere { exp.greater(it) } }
reversed()
} else {
page.maxId?.let { andWhere { exp.less(it) } }
page.sinceId?.let { andWhere { exp.greater(it) } }
orderBy(exp, SortOrder.DESC)
toList()
}
return PaginationList(resultRows, resultRows.firstOrNull()?.getOrNull(exp), resultRows.lastOrNull()?.getOrNull(exp))
}