mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-26 12:38:35 +08:00
Merge pull request #304 from nagisa77/codex/update-commentdto-for-multiple-reactions
Add reactions to comment listing
This commit is contained in:
@@ -4,6 +4,7 @@ import com.openisle.model.Comment;
|
|||||||
import com.openisle.service.CommentService;
|
import com.openisle.service.CommentService;
|
||||||
import com.openisle.service.CaptchaService;
|
import com.openisle.service.CaptchaService;
|
||||||
import com.openisle.service.LevelService;
|
import com.openisle.service.LevelService;
|
||||||
|
import com.openisle.service.ReactionService;
|
||||||
import com.openisle.model.CommentSort;
|
import com.openisle.model.CommentSort;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -23,6 +24,7 @@ public class CommentController {
|
|||||||
private final CommentService commentService;
|
private final CommentService commentService;
|
||||||
private final LevelService levelService;
|
private final LevelService levelService;
|
||||||
private final CaptchaService captchaService;
|
private final CaptchaService captchaService;
|
||||||
|
private final ReactionService reactionService;
|
||||||
|
|
||||||
@Value("${app.captcha.enabled:false}")
|
@Value("${app.captcha.enabled:false}")
|
||||||
private boolean captchaEnabled;
|
private boolean captchaEnabled;
|
||||||
@@ -70,6 +72,10 @@ public class CommentController {
|
|||||||
.map(this::toDtoWithReplies)
|
.map(this::toDtoWithReplies)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
dto.setReplies(replies);
|
dto.setReplies(replies);
|
||||||
|
List<ReactionDto> reactions = reactionService.getReactionsForComment(comment.getId()).stream()
|
||||||
|
.map(this::toReactionDto)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
dto.setReactions(reactions);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +115,7 @@ public class CommentController {
|
|||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
private AuthorDto author;
|
private AuthorDto author;
|
||||||
private List<CommentDto> replies;
|
private List<CommentDto> replies;
|
||||||
|
private List<ReactionDto> reactions;
|
||||||
private int reward;
|
private int reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,4 +125,29 @@ public class CommentController {
|
|||||||
private String username;
|
private String username;
|
||||||
private String avatar;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.openisle.model.User;
|
|||||||
import com.openisle.service.CommentService;
|
import com.openisle.service.CommentService;
|
||||||
import com.openisle.service.CaptchaService;
|
import com.openisle.service.CaptchaService;
|
||||||
import com.openisle.service.LevelService;
|
import com.openisle.service.LevelService;
|
||||||
|
import com.openisle.service.ReactionService;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -37,6 +38,8 @@ class CommentControllerTest {
|
|||||||
private CaptchaService captchaService;
|
private CaptchaService captchaService;
|
||||||
@MockBean
|
@MockBean
|
||||||
private LevelService levelService;
|
private LevelService levelService;
|
||||||
|
@MockBean
|
||||||
|
private ReactionService reactionService;
|
||||||
|
|
||||||
private Comment createComment(Long id, String content, String authorName) {
|
private Comment createComment(Long id, String content, String authorName) {
|
||||||
User user = new User();
|
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.addComment(eq("bob"), eq(1L), eq("hi"))).thenReturn(comment);
|
||||||
Mockito.when(commentService.getCommentsForPost(eq(1L), any())).thenReturn(List.of(comment));
|
Mockito.when(commentService.getCommentsForPost(eq(1L), any())).thenReturn(List.of(comment));
|
||||||
Mockito.when(commentService.getReplies(1L)).thenReturn(List.of());
|
Mockito.when(commentService.getReplies(1L)).thenReturn(List.of());
|
||||||
|
Mockito.when(reactionService.getReactionsForComment(1L)).thenReturn(List.of());
|
||||||
|
|
||||||
mockMvc.perform(post("/api/posts/1/comments")
|
mockMvc.perform(post("/api/posts/1/comments")
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
|
|||||||
Reference in New Issue
Block a user