diff --git a/src/main/java/com/openisle/controller/CommentController.java b/src/main/java/com/openisle/controller/CommentController.java index b6dccfdfd..2de863617 100644 --- a/src/main/java/com/openisle/controller/CommentController.java +++ b/src/main/java/com/openisle/controller/CommentController.java @@ -4,6 +4,7 @@ import com.openisle.model.Comment; import com.openisle.service.CommentService; import com.openisle.service.CaptchaService; import com.openisle.service.LevelService; +import com.openisle.service.ReactionService; import com.openisle.model.CommentSort; import lombok.Data; import lombok.RequiredArgsConstructor; @@ -23,6 +24,7 @@ public class CommentController { private final CommentService commentService; private final LevelService levelService; private final CaptchaService captchaService; + private final ReactionService reactionService; @Value("${app.captcha.enabled:false}") private boolean captchaEnabled; @@ -70,6 +72,10 @@ public class CommentController { .map(this::toDtoWithReplies) .collect(Collectors.toList()); dto.setReplies(replies); + List reactions = reactionService.getReactionsForComment(comment.getId()).stream() + .map(this::toReactionDto) + .collect(Collectors.toList()); + dto.setReactions(reactions); return dto; } @@ -109,6 +115,7 @@ public class CommentController { private LocalDateTime createdAt; private AuthorDto author; private List replies; + private List reactions; private int reward; } @@ -118,4 +125,29 @@ public class CommentController { private String username; private String avatar; } + + private ReactionDto toReactionDto(com.openisle.model.Reaction reaction) { + ReactionDto dto = new ReactionDto(); + dto.setId(reaction.getId()); + dto.setType(reaction.getType()); + dto.setUser(reaction.getUser().getUsername()); + if (reaction.getPost() != null) { + dto.setPostId(reaction.getPost().getId()); + } + if (reaction.getComment() != null) { + dto.setCommentId(reaction.getComment().getId()); + } + dto.setReward(0); + return dto; + } + + @Data + private static class ReactionDto { + private Long id; + private com.openisle.model.ReactionType type; + private String user; + private Long postId; + private Long commentId; + private int reward; + } } diff --git a/src/test/java/com/openisle/controller/CommentControllerTest.java b/src/test/java/com/openisle/controller/CommentControllerTest.java index 8eeabbef9..eb9cfde2a 100644 --- a/src/test/java/com/openisle/controller/CommentControllerTest.java +++ b/src/test/java/com/openisle/controller/CommentControllerTest.java @@ -6,6 +6,7 @@ import com.openisle.model.User; import com.openisle.service.CommentService; import com.openisle.service.CaptchaService; import com.openisle.service.LevelService; +import com.openisle.service.ReactionService; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -37,6 +38,8 @@ class CommentControllerTest { private CaptchaService captchaService; @MockBean private LevelService levelService; + @MockBean + private ReactionService reactionService; private Comment createComment(Long id, String content, String authorName) { User user = new User(); @@ -56,6 +59,7 @@ class CommentControllerTest { Mockito.when(commentService.addComment(eq("bob"), eq(1L), eq("hi"))).thenReturn(comment); Mockito.when(commentService.getCommentsForPost(eq(1L), any())).thenReturn(List.of(comment)); Mockito.when(commentService.getReplies(1L)).thenReturn(List.of()); + Mockito.when(reactionService.getReactionsForComment(1L)).thenReturn(List.of()); mockMvc.perform(post("/api/posts/1/comments") .contentType("application/json")