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 return post
} }
override suspend fun findById(id: Long): Post = Posts.select { Posts.id eq id }.single().toPost() 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.domain.model.hideout.entity.Reaction
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
@ -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 generateId(): Long = idGenerateService.generateId()
override suspend fun save(reaction: Reaction): Reaction { override suspend fun save(reaction: Reaction): Reaction {
query { if (Reactions.select { Reactions.id eq reaction.id }.empty()) {
if (Reactions.select { Reactions.id eq reaction.id }.empty()) { Reactions.insert {
Reactions.insert { it[id] = reaction.id
it[id] = reaction.id it[emojiId] = reaction.emojiId
it[emojiId] = reaction.emojiId it[postId] = reaction.postId
it[postId] = reaction.postId it[userId] = reaction.userId
it[userId] = reaction.userId }
} } else {
} else { Reactions.update({ Reactions.id eq reaction.id }) {
Reactions.update({ Reactions.id eq reaction.id }) { it[emojiId] = reaction.emojiId
it[emojiId] = reaction.emojiId it[postId] = reaction.postId
it[postId] = reaction.postId it[userId] = reaction.userId
it[userId] = reaction.userId
}
} }
} }
return reaction 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 { override suspend fun delete(reaction: Reaction): Reaction {
query { Reactions.deleteWhere {
Reactions.deleteWhere { id.eq(reaction.id)
id.eq(reaction.id) .and(postId.eq(reaction.postId))
.and(postId.eq(reaction.postId)) .and(userId.eq(reaction.postId))
.and(userId.eq(reaction.postId)) .and(emojiId.eq(reaction.emojiId))
.and(emojiId.eq(reaction.emojiId))
}
} }
return reaction 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 { fun ResultRow.toReaction(): Reaction {

View File

@ -56,33 +56,25 @@ class UserRepository(private val database: Database, private val idGenerateServi
} }
} }
return user return user
} }
override suspend fun findById(id: Long): User? { override suspend fun findById(id: Long): User? {
return Users.select { Users.id eq id }.map { return 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) {
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 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) {
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()