Improve sql
This commit is contained in:
@@ -11,8 +11,13 @@ import java.sql.SQLIntegrityConstraintViolationException
|
||||
internal class UserRepositoryImpl(private val db: Database) : UserRepository {
|
||||
override fun create(user: User): PersistedUser? {
|
||||
return try {
|
||||
db.useTransaction { db.users.add(user.toEntity()) }
|
||||
find(user.username)
|
||||
db.useTransaction {
|
||||
val id = db.insertAndGenerateKey(Users) {
|
||||
it.username to user.username
|
||||
it.password to user.password
|
||||
} as Int
|
||||
PersistedUser(user.username, user.password, id)
|
||||
}
|
||||
} catch (e: SQLIntegrityConstraintViolationException) {
|
||||
null
|
||||
}
|
||||
@@ -22,5 +27,5 @@ internal class UserRepositoryImpl(private val db: Database) : UserRepository {
|
||||
override fun find(id: Int) = db.users.find { it.id eq id }?.toPersistedUser()
|
||||
override fun exists(username: String) = db.users.any { it.username eq username }
|
||||
override fun exists(id: Int) = db.users.any { it.id eq id }
|
||||
override fun delete(id: Int) = db.useTransaction { db.users.find { it.id eq id }?.delete() == 1 }
|
||||
override fun delete(id: Int) = db.useTransaction { db.delete(Users) { it.id eq id } == 1 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user