refactor: トランザクションを削除

This commit is contained in:
usbharu 2023-08-10 20:33:23 +09:00
parent 23711f430f
commit 1d4e916f3a
2 changed files with 76 additions and 97 deletions

View File

@ -3,10 +3,8 @@ package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Post import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.domain.model.hideout.entity.Visibility import dev.usbharu.hideout.domain.model.hideout.entity.Visibility
import dev.usbharu.hideout.service.core.IdGenerateService import dev.usbharu.hideout.service.core.IdGenerateService
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single import org.koin.core.annotation.Single
@ -22,55 +20,44 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
override suspend fun generateId(): Long = idGenerateService.generateId() override suspend fun generateId(): Long = idGenerateService.generateId()
@Suppress("InjectDispatcher")
suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun save(post: Post): Post { override suspend fun save(post: Post): Post {
return query { val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull() if (singleOrNull == null) {
if (singleOrNull == null) { Posts.insert {
Posts.insert { it[id] = post.id
it[id] = post.id it[userId] = post.userId
it[userId] = post.userId it[overview] = post.overview
it[overview] = post.overview it[text] = post.text
it[text] = post.text it[createdAt] = post.createdAt
it[createdAt] = post.createdAt it[visibility] = post.visibility.ordinal
it[visibility] = post.visibility.ordinal it[url] = post.url
it[url] = post.url it[repostId] = post.repostId
it[repostId] = post.repostId it[replyId] = post.replyId
it[replyId] = post.replyId it[sensitive] = post.sensitive
it[sensitive] = post.sensitive it[apId] = post.apId
it[apId] = post.apId }
} } else {
} else { Posts.update({ Posts.id eq post.id }) {
Posts.update({ Posts.id eq post.id }) { it[userId] = post.userId
it[userId] = post.userId it[overview] = post.overview
it[overview] = post.overview it[text] = post.text
it[text] = post.text it[createdAt] = post.createdAt
it[createdAt] = post.createdAt it[visibility] = post.visibility.ordinal
it[visibility] = post.visibility.ordinal it[url] = post.url
it[url] = post.url it[repostId] = post.repostId
it[repostId] = post.repostId it[replyId] = post.replyId
it[replyId] = post.replyId it[sensitive] = post.sensitive
it[sensitive] = post.sensitive it[apId] = post.apId
it[apId] = post.apId
}
} }
return@query post
} }
return post
} }
override suspend fun findById(id: Long): Post { override suspend fun findById(id: Long): Post = Posts.select { Posts.id eq id }.single().toPost()
return query {
Posts.select { Posts.id eq id }.single().toPost()
}
}
override suspend fun delete(id: Long) { override suspend fun delete(id: Long) {
return query { Posts.deleteWhere { Posts.id eq id }
Posts.deleteWhere { Posts.id eq id }
}
} }
} }

View File

@ -2,11 +2,9 @@ package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.service.core.IdGenerateService import dev.usbharu.hideout.service.core.IdGenerateService
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.dao.id.LongIdTable import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single import org.koin.core.annotation.Single
import java.time.Instant import java.time.Instant
@ -25,72 +23,66 @@ class UserRepository(private val database: Database, private val idGenerateServi
} }
} }
@Suppress("InjectDispatcher")
suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun save(user: User): User { override suspend fun save(user: User): User {
return query { val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull()
val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull() if (singleOrNull == null) {
if (singleOrNull == null) { Users.insert {
Users.insert { it[id] = user.id
it[id] = user.id it[name] = user.name
it[name] = user.name it[domain] = user.domain
it[domain] = user.domain it[screenName] = user.screenName
it[screenName] = user.screenName it[description] = user.description
it[description] = user.description it[password] = user.password
it[password] = user.password it[inbox] = user.inbox
it[inbox] = user.inbox it[outbox] = user.outbox
it[outbox] = user.outbox it[url] = user.url
it[url] = user.url it[createdAt] = user.createdAt.toEpochMilli()
it[createdAt] = user.createdAt.toEpochMilli() it[publicKey] = user.publicKey
it[publicKey] = user.publicKey it[privateKey] = user.privateKey
it[privateKey] = user.privateKey }
} } else {
} else { Users.update({ Users.id eq user.id }) {
Users.update({ Users.id eq user.id }) { it[name] = user.name
it[name] = user.name it[domain] = user.domain
it[domain] = user.domain it[screenName] = user.screenName
it[screenName] = user.screenName it[description] = user.description
it[description] = user.description it[password] = user.password
it[password] = user.password it[inbox] = user.inbox
it[inbox] = user.inbox it[outbox] = user.outbox
it[outbox] = user.outbox it[url] = user.url
it[url] = user.url it[createdAt] = user.createdAt.toEpochMilli()
it[createdAt] = user.createdAt.toEpochMilli() it[publicKey] = user.publicKey
it[publicKey] = user.publicKey it[privateKey] = user.privateKey
it[privateKey] = user.privateKey
}
} }
return@query user
} }
return user
} }
override suspend fun findById(id: Long): User? { override suspend fun findById(id: Long): User? {
return query { return Users.select { Users.id eq id }.map {
Users.select { Users.id eq id }.map { it.toUser()
it.toUser() }.singleOrNull()
}.singleOrNull()
}
} }
override suspend fun deleteFollowRequest(id: Long, follower: Long) { override suspend fun deleteFollowRequest(id: Long, follower: Long) {
query {
FollowRequests.deleteWhere { userId.eq(id) and followerId.eq(follower) } FollowRequests.deleteWhere { userId.eq(id) and followerId.eq(follower) }
}
} }
override suspend fun findFollowRequestsById(id: Long, follower: Long): Boolean { override suspend fun findFollowRequestsById(id: Long, follower: Long): Boolean {
return query {
FollowRequests.select { (FollowRequests.userId eq id) and (FollowRequests.followerId eq follower) } return FollowRequests.select { (FollowRequests.userId eq id) and (FollowRequests.followerId eq follower) }
.singleOrNull() != null .singleOrNull() != null
}
} }
override suspend fun delete(id: Long) { override suspend fun delete(id: Long) {
query {
Users.deleteWhere { Users.id.eq(id) } Users.deleteWhere { Users.id.eq(id) }
}
} }
override suspend fun nextId(): Long = idGenerateService.generateId() override suspend fun nextId(): Long = idGenerateService.generateId()