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.Visibility
import dev.usbharu.hideout.service.core.IdGenerateService
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single
@ -22,12 +20,7 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
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 {
return query {
val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
if (singleOrNull == null) {
Posts.insert {
@ -57,22 +50,16 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
it[apId] = post.apId
}
}
return@query post
}
return post
}
override suspend fun findById(id: Long): Post {
return query {
Posts.select { Posts.id eq id }.single().toPost()
}
}
override suspend fun findById(id: Long): Post = Posts.select { Posts.id eq id }.single().toPost()
override suspend fun delete(id: Long) {
return query {
Posts.deleteWhere { Posts.id eq id }
}
}
}
object Posts : Table() {
val id = long("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.service.core.IdGenerateService
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single
import java.time.Instant
@ -25,12 +23,7 @@ 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 {
return query {
val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull()
if (singleOrNull == null) {
Users.insert {
@ -62,35 +55,34 @@ class UserRepository(private val database: Database, private val idGenerateServi
it[privateKey] = user.privateKey
}
}
return@query user
}
return user
}
override suspend fun findById(id: Long): User? {
return query {
Users.select { Users.id eq id }.map {
return Users.select { Users.id eq id }.map {
it.toUser()
}.singleOrNull()
}
}
override suspend fun deleteFollowRequest(id: Long, follower: Long) {
query {
FollowRequests.deleteWhere { userId.eq(id) and followerId.eq(follower) }
}
}
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
}
}
override suspend fun delete(id: Long) {
query {
Users.deleteWhere { Users.id.eq(id) }
}
}
override suspend fun nextId(): Long = idGenerateService.generateId()