From 8654c627d1d8e5614d998c826526598d4f042759 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:54:30 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/repository/PostRepositoryImpl.kt | 1 - .../repository/ReactionRepositoryImpl.kt | 68 +++++-------------- .../hideout/repository/UserRepository.kt | 8 --- 3 files changed, 17 insertions(+), 60 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index 41242bc4..b9ca5f77 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -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() diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt index 08707c35..628a995e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt @@ -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 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 { - 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 { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt index 919447b8..b9064bf8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt @@ -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()