Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.keyskey.ktknowledge.infrastructure.database

import com.keyskey.ktknowledge.infrastructure.repositories.UserDao
import org.ktorm.schema.Table
import org.ktorm.schema.datetime
import org.ktorm.schema.int
import org.ktorm.schema.varchar

object Users: Table<Nothing>("users") {
val id = int("id").primaryKey()
val name = varchar("name")
val email = varchar("email")
val password = varchar("password")
val createdAt = datetime("created_at")
val updatedAt = datetime("updated_at")
object Users: Table<UserDao>("users") {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
val email = varchar("email").bindTo { it.email }
val password = varchar("password").bindTo { it.password }
val createdAt = datetime("created_at").bindTo { it.createdAt }
val updatedAt = datetime("updated_at").bindTo { it.updatedAt }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,49 @@ import com.keyskey.ktknowledge.entities.UsersRepository
import com.keyskey.ktknowledge.infrastructure.database.Users
import org.ktorm.database.Database
import org.ktorm.dsl.*
import org.ktorm.entity.*
import org.springframework.stereotype.Repository
import java.time.LocalDateTime

interface UserDao : Entity<UserDao> {
companion object : Entity.Factory<UserDao>()

val id: Int
var name: String
var email: String
var password: String
var createdAt: LocalDateTime
var updatedAt: LocalDateTime
}

@Repository
class UsersRepositoryImpl(val database: Database): UsersRepository {
// insert時に生成されたidを取得する手段がない
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これが致命的だった。やはりAuto incrementなIDを振る場合Entity APIは使えない。UUIDを使うのであればアリかも。

override fun create(user: User): User {
val id = database.insertAndGenerateKey(Users) {
set(it.name, user.name)
set(it.email, user.email)
set(it.password, user.password)
set(it.createdAt, user.createdAt)
set(it.updatedAt, user.updatedAt)
}.toString().toInt()

return user.copy(id = id)
val userDao = buildDao(user)
users().add(userDao)
Copy link
Owner Author

@keyskey keyskey Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addメソッドの返り値はinsert文のaffected rowの数。そんなん要らんのよ 😇


return user
Copy link
Owner Author

@keyskey keyskey Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ID取ってこれない以上他にユニークなキーがないので検索して引っ張ってくることもできない。とりあえず元のエンティティ返してるけどID = 0が入ってるのでこの返り値は使えない。

}

override fun update(user: User) {
database.update(Users) {
set(it.name, user.name)
set(it.email, user.email)
set(it.password, user.password)
set(it.createdAt, user.createdAt)
set(it.updatedAt, user.updatedAt)
where { it.id eq user.id }
}
val userDao = buildDao(user)
users().update(userDao)
}

override fun delete(id: Int) {
database.delete(Users) {
it.id eq id
users().removeIf { it.id eq id }
}

private fun buildDao(user: User): UserDao {
return UserDao {
name = user.name
email = user.email
password = user.password
createdAt = user.createdAt
updatedAt = user.updatedAt
}
}

private fun users() = database.sequenceOf(Users)
}