Created
May 4, 2020 10:36
-
-
Save simonmorley/009cdfe312e0f8de077c4feff95ae091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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