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.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,55 +20,44 @@ 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 {
|
||||
it[id] = post.id
|
||||
it[userId] = post.userId
|
||||
it[overview] = post.overview
|
||||
it[text] = post.text
|
||||
it[createdAt] = post.createdAt
|
||||
it[visibility] = post.visibility.ordinal
|
||||
it[url] = post.url
|
||||
it[repostId] = post.repostId
|
||||
it[replyId] = post.replyId
|
||||
it[sensitive] = post.sensitive
|
||||
it[apId] = post.apId
|
||||
}
|
||||
} else {
|
||||
Posts.update({ Posts.id eq post.id }) {
|
||||
it[userId] = post.userId
|
||||
it[overview] = post.overview
|
||||
it[text] = post.text
|
||||
it[createdAt] = post.createdAt
|
||||
it[visibility] = post.visibility.ordinal
|
||||
it[url] = post.url
|
||||
it[repostId] = post.repostId
|
||||
it[replyId] = post.replyId
|
||||
it[sensitive] = post.sensitive
|
||||
it[apId] = post.apId
|
||||
}
|
||||
val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
|
||||
if (singleOrNull == null) {
|
||||
Posts.insert {
|
||||
it[id] = post.id
|
||||
it[userId] = post.userId
|
||||
it[overview] = post.overview
|
||||
it[text] = post.text
|
||||
it[createdAt] = post.createdAt
|
||||
it[visibility] = post.visibility.ordinal
|
||||
it[url] = post.url
|
||||
it[repostId] = post.repostId
|
||||
it[replyId] = post.replyId
|
||||
it[sensitive] = post.sensitive
|
||||
it[apId] = post.apId
|
||||
}
|
||||
} else {
|
||||
Posts.update({ Posts.id eq post.id }) {
|
||||
it[userId] = post.userId
|
||||
it[overview] = post.overview
|
||||
it[text] = post.text
|
||||
it[createdAt] = post.createdAt
|
||||
it[visibility] = post.visibility.ordinal
|
||||
it[url] = post.url
|
||||
it[repostId] = post.repostId
|
||||
it[replyId] = post.replyId
|
||||
it[sensitive] = post.sensitive
|
||||
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 }
|
||||
}
|
||||
Posts.deleteWhere { Posts.id eq id }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,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 {
|
||||
return query {
|
||||
val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull()
|
||||
if (singleOrNull == null) {
|
||||
Users.insert {
|
||||
it[id] = 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
|
||||
}
|
||||
} 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
|
||||
}
|
||||
val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull()
|
||||
if (singleOrNull == null) {
|
||||
Users.insert {
|
||||
it[id] = 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
|
||||
}
|
||||
} 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 user
|
||||
|
||||
}
|
||||
|
||||
override suspend fun findById(id: Long): User? {
|
||||
return query {
|
||||
Users.select { Users.id eq id }.map {
|
||||
it.toUser()
|
||||
}.singleOrNull()
|
||||
}
|
||||
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) }
|
||||
}
|
||||
|
||||
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) }
|
||||
.singleOrNull() != null
|
||||
}
|
||||
|
||||
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) }
|
||||
}
|
||||
|
||||
Users.deleteWhere { Users.id.eq(id) }
|
||||
|
||||
}
|
||||
|
||||
override suspend fun nextId(): Long = idGenerateService.generateId()
|
||||
|
|
Loading…
Reference in New Issue