-
Notifications
You must be signed in to change notification settings - Fork 0
UserRepositoryを試しにEntity APIで実装してみたがダメだった件 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
keyskey
wants to merge
1
commit into
main
Choose a base branch
from
if-use-dao-in-repository-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 8 additions & 7 deletions
15
src/main/kotlin/com/keyskey/ktknowledge/infrastructure/database/Users.kt
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
| 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 } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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を取得する手段がない | ||
| 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) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addメソッドの返り値はinsert文のaffected rowの数。そんなん要らんのよ 😇 |
||
|
|
||
| return user | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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を使うのであればアリかも。