Add sort and order impl

This commit is contained in:
Mikołaj Pich 2023-10-29 19:55:25 +01:00
parent 9b0178f3cb
commit 8dd7c7816f
2 changed files with 26 additions and 1 deletions

View file

@ -22,10 +22,21 @@ class LoginEventDao {
uuid = row[LoginEvents.uuid], uuid = row[LoginEvents.uuid],
) )
suspend fun allLoginEvents(page: Long, pageSize: Int): List<LoginEvent> = dbQuery { suspend fun allLoginEvents(
page: Long,
pageSize: Int,
orderBy: Column<*>?,
order: SortOrder?,
): List<LoginEvent> = dbQuery {
LoginEvents LoginEvents
.selectAll() .selectAll()
.groupBy(LoginEvents.schoolId, LoginEvents.symbol, LoginEvents.scraperBaseUrl)
.limit(pageSize, page * pageSize) .limit(pageSize, page * pageSize)
.let {
if (orderBy != null && order != null) {
it.orderBy(orderBy, order)
} else it
}
.map(::resultRowToLoginEvent) .map(::resultRowToLoginEvent)
} }

View file

@ -4,12 +4,14 @@ import io.github.wulkanowy.schools.dao.LoginEventDao
import io.github.wulkanowy.schools.integrity.* import io.github.wulkanowy.schools.integrity.*
import io.github.wulkanowy.schools.model.ListResponse import io.github.wulkanowy.schools.model.ListResponse
import io.github.wulkanowy.schools.model.LoginEvent import io.github.wulkanowy.schools.model.LoginEvent
import io.github.wulkanowy.schools.model.LoginEvents
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.http.content.* import io.ktor.server.http.content.*
import io.ktor.server.request.* import io.ktor.server.request.*
import io.ktor.server.response.* import io.ktor.server.response.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import org.jetbrains.exposed.sql.SortOrder
import java.util.logging.Level import java.util.logging.Level
import java.util.logging.Logger import java.util.logging.Logger
@ -45,6 +47,18 @@ fun Application.configureRouting() {
rows = loginEventDao.allLoginEvents( rows = loginEventDao.allLoginEvents(
page = params["page"]?.toLongOrNull() ?: 0, page = params["page"]?.toLongOrNull() ?: 0,
pageSize = params["pageSize"]?.toIntOrNull() ?: 10, pageSize = params["pageSize"]?.toIntOrNull() ?: 10,
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
},
), ),
rowsCount = loginEventDao.getLoginEventsCount(), rowsCount = loginEventDao.getLoginEventsCount(),
) )