From f90afedf3abf5fb385813a950b52a150f03ae69e Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Wed, 9 Jul 2025 18:43:37 +0800 Subject: [PATCH] Prevent duplicate reactions --- .../java/com/openisle/service/ReactionService.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/com/openisle/service/ReactionService.java b/src/main/java/com/openisle/service/ReactionService.java index 2b90670af..cae386351 100644 --- a/src/main/java/com/openisle/service/ReactionService.java +++ b/src/main/java/com/openisle/service/ReactionService.java @@ -28,6 +28,11 @@ public class ReactionService { .orElseThrow(() -> new IllegalArgumentException("User not found")); Post post = postRepository.findById(postId) .orElseThrow(() -> new IllegalArgumentException("Post not found")); + java.util.Optional existing = + reactionRepository.findByUserAndPostAndType(user, post, type); + if (existing.isPresent()) { + return null; + } Reaction reaction = new Reaction(); reaction.setUser(user); reaction.setPost(post); @@ -44,6 +49,11 @@ public class ReactionService { .orElseThrow(() -> new IllegalArgumentException("User not found")); Comment comment = commentRepository.findById(commentId) .orElseThrow(() -> new IllegalArgumentException("Comment not found")); + java.util.Optional existing = + reactionRepository.findByUserAndCommentAndType(user, comment, type); + if (existing.isPresent()) { + return null; + } Reaction reaction = new Reaction(); reaction.setUser(user); reaction.setComment(comment);