refactor: 不要なコードを削除

This commit is contained in:
usbharu 2023-08-11 10:54:30 +09:00
parent ca541278df
commit 8654c627d1
3 changed files with 17 additions and 60 deletions

View File

@ -51,7 +51,6 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
}
}
return post
}
override suspend fun findById(id: Long): Post = Posts.select { Posts.id eq id }.single().toPost()

View File

@ -2,11 +2,9 @@ package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
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
@ -23,67 +21,35 @@ class ReactionRepositoryImpl(
}
}
@Suppress("InjectDispatcher")
suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(reaction: Reaction): Reaction {
query {
if (Reactions.select { Reactions.id eq reaction.id }.empty()) {
Reactions.insert {
it[id] = reaction.id
it[emojiId] = reaction.emojiId
it[postId] = reaction.postId
it[userId] = reaction.userId
}
} else {
Reactions.update({ Reactions.id eq reaction.id }) {
it[emojiId] = reaction.emojiId
it[postId] = reaction.postId
it[userId] = reaction.userId
}
if (Reactions.select { Reactions.id eq reaction.id }.empty()) {
Reactions.insert {
it[id] = reaction.id
it[emojiId] = reaction.emojiId
it[postId] = reaction.postId
it[userId] = reaction.userId
}
} else {
Reactions.update({ Reactions.id eq reaction.id }) {
it[emojiId] = reaction.emojiId
it[postId] = reaction.postId
it[userId] = reaction.userId
}
}
return reaction
}
override suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean {
return query {
Reactions.select {
Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)).and(
Reactions.emojiId.eq(emojiId)
)
}.empty().not()
}
}
override suspend fun findByPostId(postId: Long): List<Reaction> {
return query {
Reactions.select {
Reactions.postId.eq(postId)
}.map { it.toReaction() }
}
}
override suspend fun delete(reaction: Reaction): Reaction {
query {
Reactions.deleteWhere {
id.eq(reaction.id)
.and(postId.eq(reaction.postId))
.and(userId.eq(reaction.postId))
.and(emojiId.eq(reaction.emojiId))
}
Reactions.deleteWhere {
id.eq(reaction.id)
.and(postId.eq(reaction.postId))
.and(userId.eq(reaction.postId))
.and(emojiId.eq(reaction.emojiId))
}
return reaction
}
override suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long) {
query {
Reactions.deleteWhere { Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)) }
}
}
}
fun ResultRow.toReaction(): Reaction {

View File

@ -56,33 +56,25 @@ class UserRepository(private val database: Database, private val idGenerateServi
}
}
return user
}
override suspend fun findById(id: Long): User? {
return Users.select { Users.id eq id }.map {
it.toUser()
}.singleOrNull()
}
override suspend fun deleteFollowRequest(id: Long, follower: Long) {
FollowRequests.deleteWhere { userId.eq(id) and followerId.eq(follower) }
}
override suspend fun findFollowRequestsById(id: Long, follower: Long): Boolean {
return FollowRequests.select { (FollowRequests.userId eq id) and (FollowRequests.followerId eq follower) }
.singleOrNull() != null
}
override suspend fun delete(id: Long) {
Users.deleteWhere { Users.id.eq(id) }
}
override suspend fun nextId(): Long = idGenerateService.generateId()