fix: リアクション受信時の重複検査を修正

This commit is contained in:
usbharu 2023-12-28 11:35:49 +09:00
parent bed0b84d16
commit 458a1d7717
3 changed files with 36 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.domain.model.reaction
import dev.usbharu.hideout.core.domain.model.emoji.Emoji
import org.springframework.stereotype.Repository
@Repository
@ -13,5 +14,7 @@ interface ReactionRepository {
suspend fun findByPostId(postId: Long): List<Reaction>
suspend fun findByPostIdAndActorIdAndEmojiId(postId: Long, actorId: Long, emojiId: Long): Reaction?
suspend fun existByPostIdAndActorIdAndEmojiId(postId: Long, actorId: Long, emojiId: Long): Boolean
suspend fun existByPostIdAndActorIdAndUnicodeEmoji(postId: Long, actorId: Long, unicodeEmoji: String): Boolean
suspend fun existByPostIdAndActorIdAndEmoji(postId: Long, actorId: Long, emoji: Emoji): Boolean
suspend fun findByPostIdAndActorId(postId: Long, actorId: Long): List<Reaction>
}

View File

@ -2,6 +2,7 @@ package dev.usbharu.hideout.core.infrastructure.exposedrepository
import dev.usbharu.hideout.application.service.id.IdGenerateService
import dev.usbharu.hideout.core.domain.model.emoji.CustomEmoji
import dev.usbharu.hideout.core.domain.model.emoji.Emoji
import dev.usbharu.hideout.core.domain.model.emoji.UnicodeEmoji
import dev.usbharu.hideout.core.domain.model.reaction.Reaction
import dev.usbharu.hideout.core.domain.model.reaction.ReactionRepository
@ -103,6 +104,37 @@ class ReactionRepositoryImpl(
}.empty().not()
}
override suspend fun existByPostIdAndActorIdAndUnicodeEmoji(
postId: Long,
actorId: Long,
unicodeEmoji: String
): Boolean = query {
return@query Reactions.select {
Reactions.postId
.eq(postId)
.and(Reactions.actorId.eq(actorId))
.and(Reactions.unicodeEmoji.eq(unicodeEmoji))
}.empty().not()
}
override suspend fun existByPostIdAndActorIdAndEmoji(postId: Long, actorId: Long, emoji: Emoji): Boolean = query {
val query = Reactions.select {
Reactions.postId
.eq(postId)
.and(Reactions.actorId.eq(actorId))
}
if (emoji is UnicodeEmoji) {
query.andWhere { Reactions.unicodeEmoji eq emoji.name }
} else {
emoji as CustomEmoji
query.andWhere { Reactions.customEmojiId eq emoji.id }
}
return@query query.empty().not()
}
override suspend fun findByPostIdAndActorId(postId: Long, actorId: Long): List<Reaction> = query {
return@query Reactions.leftJoin(CustomEmojis)
.select { Reactions.postId eq postId and (Reactions.actorId eq actorId) }

View File

@ -19,7 +19,7 @@ class ReactionServiceImpl(
actorId: Long,
postId: Long
) {
if (reactionRepository.existByPostIdAndActorIdAndEmojiId(postId, actorId, 0).not()) {
if (reactionRepository.existByPostIdAndActorIdAndEmoji(postId, actorId, emoji).not()) {
try {
reactionRepository.save(
Reaction(reactionRepository.generateId(), emoji, postId, actorId)