Skip to content

Commit

Permalink
Fix count all when using filtering vol. 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed Oct 29, 2023
1 parent 386f8ca commit cbece4e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
39 changes: 30 additions & 9 deletions src/main/kotlin/io/github/wulkanowy/schools/dao/LoginEventDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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}%" }
Expand Down
32 changes: 19 additions & 13 deletions src/main/kotlin/io/github/wulkanowy/schools/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
)
)
}
Expand Down

0 comments on commit cbece4e

Please sign in to comment.