Include reactions in comment API

This commit is contained in:
Tim
2025-08-01 12:30:15 +08:00
parent f73f0cd45a
commit d056bc9120
2 changed files with 36 additions and 0 deletions

View File

@@ -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<ReactionDto> 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<CommentDto> replies;
private List<ReactionDto> 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;
}
}

View File

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