mirror of https://github.com/usbharu/Hideout.git
refactor: トランザクションを削除
This commit is contained in:
parent
23711f430f
commit
1d4e916f3a
|
@ -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,12 +20,7 @@ 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 {
|
||||||
|
@ -57,21 +50,15 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
|
||||||
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 }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
object Posts : Table() {
|
object Posts : Table() {
|
||||||
|
|
|
@ -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,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 {
|
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 {
|
||||||
|
@ -62,35 +55,34 @@ class UserRepository(private val database: Database, private val idGenerateServi
|
||||||
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()
|
||||||
|
|
Loading…
Reference in New Issue