Skip to content

Instantly share code, notes, and snippets.

@simonmorley
Created May 4, 2020 10:36
Show Gist options
  • Save simonmorley/009cdfe312e0f8de077c4feff95ae091 to your computer and use it in GitHub Desktop.
Save simonmorley/009cdfe312e0f8de077c4feff95ae091 to your computer and use it in GitHub Desktop.
package org.simon.webapp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
internal class Data() {
suspend fun findUser(id: Int): User? = dbQuery {
Users.slice(Users.email, Users.name).select { Users.id eq id }.mapNotNull { toUser(it) }.singleOrNull()
}
}
suspend fun <T> dbQuery(block: () -> T): T {
var withContext = withContext(Dispatchers.IO) {
transaction {
addLogger(StdOutSqlLogger)
block()
}
}
return withContext
}
private fun toUser(row: ResultRow): User {
val tagId = row.getOrNull(Users.age)
val age = tagId?.let { tagId }
println("oh my gosh: ${age}")
val user = User(
id = row.getOrNull(Users.id),
name = row[Users.name],
email = row[Users.email],
age = age
)
return user
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment