From 56e742e39f5218e22acc9edcb8d220c76dce0067 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Mon, 17 Aug 2020 16:01:15 +0200 Subject: [PATCH] Improve sql --- .../src/main/kotlin/users/UserRepositoryImpl.kt | 11 ++++++++--- persistance/src/main/kotlin/users/Users.kt | 8 -------- .../src/test/kotlin/users/UserRepositoryImplTest.kt | 6 +++++- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/persistance/src/main/kotlin/users/UserRepositoryImpl.kt b/persistance/src/main/kotlin/users/UserRepositoryImpl.kt index f9d128b..b68397c 100644 --- a/persistance/src/main/kotlin/users/UserRepositoryImpl.kt +++ b/persistance/src/main/kotlin/users/UserRepositoryImpl.kt @@ -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 } } diff --git a/persistance/src/main/kotlin/users/Users.kt b/persistance/src/main/kotlin/users/Users.kt index 20bd1af..c788bab 100644 --- a/persistance/src/main/kotlin/users/Users.kt +++ b/persistance/src/main/kotlin/users/Users.kt @@ -26,14 +26,6 @@ internal interface UserEntity : Entity { internal fun UserEntity.toPersistedUser() = PersistedUser(username, password, id) -internal fun PersistedUser.toEntity(): UserEntity { - val user = this - return UserEntity { - this.username = user.username - this.password = user.password - }.apply { this["id"] = user.id } -} - internal fun User.toEntity(): UserEntity { val user = this return UserEntity { diff --git a/persistance/src/test/kotlin/users/UserRepositoryImplTest.kt b/persistance/src/test/kotlin/users/UserRepositoryImplTest.kt index 3eff7f0..e892820 100644 --- a/persistance/src/test/kotlin/users/UserRepositoryImplTest.kt +++ b/persistance/src/test/kotlin/users/UserRepositoryImplTest.kt @@ -63,7 +63,11 @@ internal class UserRepositoryImplTest { @Test fun `insert user`() { val user = User("username", "test") - assertThat(userRepo.create(user)).isNotNull + assertThat(userRepo.create(user)) + .isNotNull + .hasFieldOrPropertyWithValue("username", user.username) + .hasFieldOrPropertyWithValue("password", user.password) + assertThat(db.users.find { it.username eq user.username }).isNotNull assertThat(db.users.toList()).hasSize(1) assertThat(userRepo.create(user)).isNull()