mirror of https://github.com/usbharu/Hideout.git
feat: saveをinsertからupsertに
This commit is contained in:
parent
48f455d4bc
commit
561509f23b
|
@ -19,27 +19,41 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("", ReplaceWith("toUser()"))
|
|
||||||
private fun ResultRow.toUserEntity(): User = toUser()
|
|
||||||
|
|
||||||
suspend fun <T> query(block: suspend () -> T): T =
|
suspend fun <T> query(block: suspend () -> T): T =
|
||||||
newSuspendedTransaction(Dispatchers.IO) { block() }
|
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||||
|
|
||||||
override suspend fun save(user: User): User {
|
override suspend fun save(user: User): User {
|
||||||
return query {
|
return query {
|
||||||
Users.insert {
|
val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull()
|
||||||
it[id] = user.id
|
if (singleOrNull == null) {
|
||||||
it[name] = user.name
|
Users.insert {
|
||||||
it[domain] = user.domain
|
it[id] = user.id
|
||||||
it[screenName] = user.screenName
|
it[name] = user.name
|
||||||
it[description] = user.description
|
it[domain] = user.domain
|
||||||
it[password] = user.password
|
it[screenName] = user.screenName
|
||||||
it[inbox] = user.inbox
|
it[description] = user.description
|
||||||
it[outbox] = user.outbox
|
it[password] = user.password
|
||||||
it[url] = user.url
|
it[inbox] = user.inbox
|
||||||
it[createdAt] = user.createdAt.toEpochMilli()
|
it[outbox] = user.outbox
|
||||||
it[publicKey] = user.publicKey
|
it[url] = user.url
|
||||||
it[privateKey] = user.privateKey
|
it[createdAt] = user.createdAt.toEpochMilli()
|
||||||
|
it[publicKey] = user.publicKey
|
||||||
|
it[privateKey] = user.privateKey
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Users.update({ Users.id eq user.id }) {
|
||||||
|
it[name] = user.name
|
||||||
|
it[domain] = user.domain
|
||||||
|
it[screenName] = user.screenName
|
||||||
|
it[description] = user.description
|
||||||
|
it[password] = user.password
|
||||||
|
it[inbox] = user.inbox
|
||||||
|
it[outbox] = user.outbox
|
||||||
|
it[url] = user.url
|
||||||
|
it[createdAt] = user.createdAt.toEpochMilli()
|
||||||
|
it[publicKey] = user.publicKey
|
||||||
|
it[privateKey] = user.privateKey
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return@query user
|
return@query user
|
||||||
}
|
}
|
||||||
|
@ -57,7 +71,7 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
override suspend fun findById(id: Long): User? {
|
override suspend fun findById(id: Long): User? {
|
||||||
return query {
|
return query {
|
||||||
Users.select { Users.id eq id }.map {
|
Users.select { Users.id eq id }.map {
|
||||||
it.toUserEntity()
|
it.toUser()
|
||||||
}.singleOrNull()
|
}.singleOrNull()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,7 +79,7 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
override suspend fun findByIds(ids: List<Long>): List<User> {
|
override suspend fun findByIds(ids: List<Long>): List<User> {
|
||||||
return query {
|
return query {
|
||||||
Users.select { Users.id inList ids }.map {
|
Users.select { Users.id inList ids }.map {
|
||||||
it.toUserEntity()
|
it.toUser()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,7 +87,7 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
override suspend fun findByName(name: String): User? {
|
override suspend fun findByName(name: String): User? {
|
||||||
return query {
|
return query {
|
||||||
Users.select { Users.name eq name }.map {
|
Users.select { Users.name eq name }.map {
|
||||||
it.toUserEntity()
|
it.toUser()
|
||||||
}.singleOrNull()
|
}.singleOrNull()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,19 +98,19 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
names.forEach { (name, domain) ->
|
names.forEach { (name, domain) ->
|
||||||
selectAll.orWhere { Users.name eq name and (Users.domain eq domain) }
|
selectAll.orWhere { Users.name eq name and (Users.domain eq domain) }
|
||||||
}
|
}
|
||||||
selectAll.map { it.toUserEntity() }
|
selectAll.map { it.toUser() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByUrl(url: String): User? {
|
override suspend fun findByUrl(url: String): User? {
|
||||||
return query {
|
return query {
|
||||||
Users.select { Users.url eq url }.singleOrNull()?.toUserEntity()
|
Users.select { Users.url eq url }.singleOrNull()?.toUser()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByUrls(urls: List<String>): List<User> {
|
override suspend fun findByUrls(urls: List<String>): List<User> {
|
||||||
return query {
|
return query {
|
||||||
Users.select { Users.url inList urls }.map { it.toUserEntity() }
|
Users.select { Users.url inList urls }.map { it.toUser() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,20 +162,6 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override suspend fun update(userEntity: User) {
|
|
||||||
return query {
|
|
||||||
Users.update({ Users.id eq userEntity.id }) {
|
|
||||||
it[name] = userEntity.name
|
|
||||||
it[domain] = userEntity.domain
|
|
||||||
it[screenName] = userEntity.screenName
|
|
||||||
it[description] = userEntity.description
|
|
||||||
it[inbox] = userEntity.inbox
|
|
||||||
it[outbox] = userEntity.outbox
|
|
||||||
it[url] = userEntity.url
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun delete(id: Long) {
|
override suspend fun delete(id: Long) {
|
||||||
query {
|
query {
|
||||||
Users.deleteWhere { Users.id.eq(id) }
|
Users.deleteWhere { Users.id.eq(id) }
|
||||||
|
@ -182,7 +182,7 @@ class UserRepository(private val database: Database) : IUserRepository {
|
||||||
|
|
||||||
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<User> {
|
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<User> {
|
||||||
return query {
|
return query {
|
||||||
Users.selectAll().limit(limit, offset).map { it.toUserEntity() }
|
Users.selectAll().limit(limit, offset).map { it.toUser() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -202,6 +202,7 @@ object Users : Table("users") {
|
||||||
val createdAt = long("created_at")
|
val createdAt = long("created_at")
|
||||||
|
|
||||||
override val primaryKey: PrimaryKey = PrimaryKey(id)
|
override val primaryKey: PrimaryKey = PrimaryKey(id)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
uniqueIndex(name, domain)
|
uniqueIndex(name, domain)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue