mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 22:41:05 +08:00
Implement reaction panel with backend support
This commit is contained in:
@@ -28,6 +28,9 @@ public class ReactionController {
|
||||
@RequestBody ReactionRequest req,
|
||||
Authentication auth) {
|
||||
Reaction reaction = reactionService.reactToPost(auth.getName(), postId, req.getType());
|
||||
if (reaction == null) {
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
return ResponseEntity.ok(toDto(reaction));
|
||||
}
|
||||
|
||||
@@ -36,6 +39,9 @@ public class ReactionController {
|
||||
@RequestBody ReactionRequest req,
|
||||
Authentication auth) {
|
||||
Reaction reaction = reactionService.reactToComment(auth.getName(), commentId, req.getType());
|
||||
if (reaction == null) {
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
return ResponseEntity.ok(toDto(reaction));
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ import lombok.Setter;
|
||||
@NoArgsConstructor
|
||||
@Table(name = "reactions",
|
||||
uniqueConstraints = {
|
||||
@UniqueConstraint(columnNames = {"user_id", "post_id"}),
|
||||
@UniqueConstraint(columnNames = {"user_id", "comment_id"})
|
||||
@UniqueConstraint(columnNames = {"user_id", "post_id", "type"}),
|
||||
@UniqueConstraint(columnNames = {"user_id", "comment_id", "type"})
|
||||
})
|
||||
public class Reaction {
|
||||
@Id
|
||||
|
||||
@@ -13,8 +13,8 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ReactionRepository extends JpaRepository<Reaction, Long> {
|
||||
Optional<Reaction> findByUserAndPost(User user, Post post);
|
||||
Optional<Reaction> findByUserAndComment(User user, Comment comment);
|
||||
Optional<Reaction> findByUserAndPostAndType(User user, Post post, com.openisle.model.ReactionType type);
|
||||
Optional<Reaction> findByUserAndCommentAndType(User user, Comment comment, com.openisle.model.ReactionType type);
|
||||
List<Reaction> findByPost(Post post);
|
||||
List<Reaction> findByComment(Comment comment);
|
||||
|
||||
|
||||
@@ -28,8 +28,12 @@ public class ReactionService {
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
Post post = postRepository.findById(postId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
|
||||
Reaction reaction = reactionRepository.findByUserAndPost(user, post)
|
||||
.orElseGet(Reaction::new);
|
||||
java.util.Optional<Reaction> existing = reactionRepository.findByUserAndPostAndType(user, post, type);
|
||||
if (existing.isPresent()) {
|
||||
reactionRepository.delete(existing.get());
|
||||
return null;
|
||||
}
|
||||
Reaction reaction = new Reaction();
|
||||
reaction.setUser(user);
|
||||
reaction.setPost(post);
|
||||
reaction.setType(type);
|
||||
@@ -45,8 +49,12 @@ public class ReactionService {
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
Comment comment = commentRepository.findById(commentId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Comment not found"));
|
||||
Reaction reaction = reactionRepository.findByUserAndComment(user, comment)
|
||||
.orElseGet(Reaction::new);
|
||||
java.util.Optional<Reaction> existing = reactionRepository.findByUserAndCommentAndType(user, comment, type);
|
||||
if (existing.isPresent()) {
|
||||
reactionRepository.delete(existing.get());
|
||||
return null;
|
||||
}
|
||||
Reaction reaction = new Reaction();
|
||||
reaction.setUser(user);
|
||||
reaction.setComment(comment);
|
||||
reaction.setPost(null);
|
||||
|
||||
Reference in New Issue
Block a user