Fix count all when using filtering vol. 2

This commit is contained in:
Mikołaj Pich 2023-10-29 22:28:41 +01:00
parent 386f8cae72
commit cbece4ee1a
2 changed files with 49 additions and 22 deletions

View file

@ -11,6 +11,12 @@ import java.time.Instant
class LoginEventDao {
private val uniqueColumns = listOf(
LoginEvents.schoolId,
LoginEvents.symbol,
LoginEvents.scraperBaseUrl,
)
private fun resultRowToLoginEvent(row: ResultRow) = LoginEvent(
schoolName = row[LoginEvents.schoolName],
schoolShort = row[LoginEvents.schoolShort],
@ -29,23 +35,38 @@ class LoginEventDao {
orderBy: Column<*>?,
order: SortOrder?,
): List<LoginEvent> = dbQuery {
getQuery(text)
.let {
if (orderBy != null && order != null) {
it.orderBy(orderBy, order)
} else it
}
getQuery(text, orderBy, order)
.limit(pageSize, page * pageSize)
.map(::resultRowToLoginEvent)
}
suspend fun getCount(text: String?) = dbQuery {
getQuery(text).count()
suspend fun getCount(
text: String?,
orderBy: Column<*>?,
order: SortOrder?,
) = dbQuery {
val query = getQuery(text, orderBy, order).alias("getAll")
query.selectAll().count()
}
private suspend fun getQuery(text: String?) = dbQuery {
private suspend fun getQuery(
text: String?,
orderBy: Column<*>?,
order: SortOrder?,
) = dbQuery {
val currentOrderBy = listOfNotNull(orderBy).toSet()
val distinctColumns = currentOrderBy + (uniqueColumns - currentOrderBy)
LoginEvents
.slice(
customDistinctOn(*distinctColumns.toTypedArray()),
*(LoginEvents.columns).toTypedArray()
)
.selectAll()
.let { query ->
if (order != null) {
query.orderBy(*distinctColumns.map { it to order }.toTypedArray())
} else query
}
.let {
if (text.isNullOrBlank()) it else {
it.orWhere { LoginEvents.schoolName like "%${text}%" }

View file

@ -42,26 +42,32 @@ fun Application.configureRouting() {
}
get("/log/list") {
val params = call.request.queryParameters
val orderBy = when (params["sortBy"]) {
"id" -> LoginEvents.id
"schoolName" -> LoginEvents.schoolName
"schoolShort" -> LoginEvents.schoolShort
"schoolAddress" -> LoginEvents.schoolAddress
else -> null
}
val order = when (params["order"]) {
"asc" -> SortOrder.ASC
"desc" -> SortOrder.DESC
else -> null
}
call.respond(
ListResponse(
rows = loginEventDao.allLoginEvents(
page = params["page"]?.toLongOrNull() ?: 0,
pageSize = params["pageSize"]?.toIntOrNull() ?: 10,
text = params["text"],
orderBy = when (params["sortBy"]) {
"id" -> LoginEvents.id
"schoolName" -> LoginEvents.schoolName
"schoolShort" -> LoginEvents.schoolShort
"schoolAddress" -> LoginEvents.schoolAddress
else -> null
},
order = when (params["order"]) {
"asc" -> SortOrder.ASC
"desc" -> SortOrder.DESC
else -> null
},
orderBy = orderBy,
order = order,
),
rowsCount = loginEventDao.getCount(
text = params["text"],
orderBy = orderBy,
order = order,
),
rowsCount = loginEventDao.getCount(text = params["text"]),
)
)
}