refactor: ReactionのQueryServiceを追加

This commit is contained in:
usbharu 2023-08-10 20:58:33 +09:00
parent 1d4e916f3a
commit ca541278df
5 changed files with 63 additions and 29 deletions

View File

@ -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<Reaction>
@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)
}

View File

@ -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<Reaction> {
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)) }
}
}

View File

@ -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<Reaction>
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)
}

View File

@ -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)) }

View File

@ -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<ReactionResponse> {