diff --git a/src/main/kotlin/dev/usbharu/hideout/query/ReactionQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/query/ReactionQueryService.kt new file mode 100644 index 00000000..db5a844f --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/query/ReactionQueryService.kt @@ -0,0 +1,14 @@ +package dev.usbharu.hideout.query + +import dev.usbharu.hideout.domain.model.hideout.entity.Reaction + +interface ReactionQueryService { + suspend fun findByPostId(postId: Long): List + + @Suppress("FunctionMaxLength") + suspend fun findByPostIdAndUserIdAndEmojiId(postId: Long, userId: Long, emojiId: Long): Reaction + + suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean + + suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long) +} diff --git a/src/main/kotlin/dev/usbharu/hideout/query/ReactionQueryServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/query/ReactionQueryServiceImpl.kt new file mode 100644 index 00000000..8b6bf9ec --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/query/ReactionQueryServiceImpl.kt @@ -0,0 +1,42 @@ +package dev.usbharu.hideout.query + +import dev.usbharu.hideout.domain.model.hideout.entity.Reaction +import dev.usbharu.hideout.repository.Reactions +import dev.usbharu.hideout.repository.toReaction +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.deleteWhere +import org.jetbrains.exposed.sql.select +import org.koin.core.annotation.Single + +@Single +class ReactionQueryServiceImpl : ReactionQueryService { + override suspend fun findByPostId(postId: Long): List { + return Reactions.select { + Reactions.postId.eq(postId) + }.map { it.toReaction() } + } + + override suspend fun findByPostIdAndUserIdAndEmojiId(postId: Long, userId: Long, emojiId: Long): Reaction { + return Reactions + .select { + Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)).and( + Reactions.emojiId.eq(emojiId) + ) + } + .single() + .toReaction() + } + + override suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean { + return Reactions.select { + Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)).and( + Reactions.emojiId.eq(emojiId) + ) + }.empty().not() + } + + override suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long) { + Reactions.deleteWhere { Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)) } + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepository.kt index d51d00c2..f2aad560 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepository.kt @@ -5,11 +5,5 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Reaction interface ReactionRepository { suspend fun generateId(): Long suspend fun save(reaction: Reaction): Reaction - suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean - suspend fun findByPostId(postId: Long): List suspend fun delete(reaction: Reaction): Reaction - suspend fun deleteById(id: Long) - suspend fun deleteByPostId(postId: Long) - suspend fun deleteByUserId(userId: Long) - suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long) } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt index 3717fbab..08707c35 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt @@ -79,24 +79,6 @@ class ReactionRepositoryImpl( return reaction } - override suspend fun deleteById(id: Long) { - query { - Reactions.deleteWhere { Reactions.id.eq(id) } - } - } - - override suspend fun deleteByPostId(postId: Long) { - query { - Reactions.deleteWhere { Reactions.postId.eq(postId) } - } - } - - override suspend fun deleteByUserId(userId: Long) { - query { - Reactions.deleteWhere { Reactions.userId.eq(userId) } - } - } - override suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long) { query { Reactions.deleteWhere { Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt index 489a85ea..6b604527 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt @@ -3,6 +3,7 @@ package dev.usbharu.hideout.service.reaction import dev.usbharu.hideout.domain.model.hideout.dto.Account import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse import dev.usbharu.hideout.domain.model.hideout.entity.Reaction +import dev.usbharu.hideout.query.ReactionQueryService import dev.usbharu.hideout.repository.ReactionRepository import dev.usbharu.hideout.repository.Reactions import dev.usbharu.hideout.repository.Users @@ -16,10 +17,11 @@ import org.koin.core.annotation.Single @Single class ReactionServiceImpl( private val reactionRepository: ReactionRepository, - private val activityPubReactionService: ActivityPubReactionService + private val activityPubReactionService: ActivityPubReactionService, + private val reactionQueryService: ReactionQueryService ) : IReactionService { override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) { - if (reactionRepository.reactionAlreadyExist(postId, userId, 0).not()) { + if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) { reactionRepository.save( Reaction(reactionRepository.generateId(), 0, postId, userId) ) @@ -27,9 +29,9 @@ class ReactionServiceImpl( } override suspend fun sendReaction(name: String, userId: Long, postId: Long) { - if (reactionRepository.reactionAlreadyExist(postId, userId, 0)) { + if (reactionQueryService.reactionAlreadyExist(postId, userId, 0)) { // delete - reactionRepository.deleteByPostIdAndUserId(postId, userId) + reactionQueryService.deleteByPostIdAndUserId(postId, userId) } else { val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId) reactionRepository.save(reaction) @@ -38,7 +40,7 @@ class ReactionServiceImpl( } override suspend fun removeReaction(userId: Long, postId: Long) { - reactionRepository.deleteByPostIdAndUserId(postId, userId) + reactionQueryService.deleteByPostIdAndUserId(postId, userId) } override suspend fun findByPostIdForUser(postId: Long, userId: Long): List {