-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserRepo.kt
41 lines (35 loc) · 1.12 KB
/
UserRepo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.ishroid.example.app.repo
import com.ishroid.example.app.database.UserTable
import com.ishroid.example.app.model.User
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.*
class UserRepo {
suspend fun create(user: User) {
transaction {
UserTable.insert {
it[name] = user.name
it[email] = user.email
it[city] = user.city
}
}
}
suspend fun get(id: String): User? {
return transaction {
UserTable.select { UserTable.id eq UUID.fromString(id) }.map {
it.toUser()
}.firstOrNull()
}
}
suspend fun getAll(): List<User> {
return transaction {
UserTable.selectAll().map { it.toUser() }
}
}
private fun ResultRow.toUser(): User {
return User(this[UserTable.id].toString(), this[UserTable.name], this[UserTable.email], this[UserTable.city])
}
}