Skip to content

Commit

Permalink
Fix count all when using filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed Oct 29, 2023
1 parent df7c57d commit 386f8ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/src/routes/RemoteTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let rows = [];
let page = 0; //first page
let pageIndex = 0; //first row
let pageSize = 10; //optional, 10 by default
let pageSize = 15; //optional, 10 by default
let loading = true;
let rowsCount = 0;
Expand Down
25 changes: 16 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 @@ -25,26 +25,33 @@ class LoginEventDao {
suspend fun allLoginEvents(
page: Long,
pageSize: Int,
text: String?,
orderBy: Column<*>?,
order: SortOrder?,
): List<LoginEvent> = dbQuery {
LoginEvents
.slice(
customDistinctOn(LoginEvents.schoolId, LoginEvents.symbol, LoginEvents.scraperBaseUrl),
*(LoginEvents.columns).toTypedArray()
)
.selectAll()
.limit(pageSize, page * pageSize)
getQuery(text)
.let {
if (orderBy != null && order != null) {
it.orderBy(orderBy, order)
} else it
}
.limit(pageSize, page * pageSize)
.map(::resultRowToLoginEvent)
}

suspend fun getLoginEventsCount(): Long = dbQuery {
LoginEvents.selectAll().count()
suspend fun getCount(text: String?) = dbQuery {
getQuery(text).count()
}

private suspend fun getQuery(text: String?) = dbQuery {
LoginEvents
.selectAll()
.let {
if (text.isNullOrBlank()) it else {
it.orWhere { LoginEvents.schoolName like "%${text}%" }
.orWhere { LoginEvents.schoolAddress like "%${text}%" }
}
}
}

suspend fun addLoginEvent(event: LoginEvent) = withContext(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fun Application.configureRouting() {
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
Expand All @@ -60,7 +61,7 @@ fun Application.configureRouting() {
else -> null
},
),
rowsCount = loginEventDao.getLoginEventsCount(),
rowsCount = loginEventDao.getCount(text = params["text"]),
)
)
}
Expand Down

0 comments on commit 386f8ca

Please sign in to comment.