评论
# 评论列表
VO:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommentVo {
private Long id;
//文章id
private Long articleId;
//根评论id
private Long rootId;
//评论内容
private String content;
//所回复的目标评论的userid
private Long toCommentUserId;
private String toCommentUserName;
//回复目标评论id
private Long toCommentId;
private Long createBy;
private Date createTime;
private String username;
private List<CommentVo> children;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
接口:
@GetMapping("/commentList")
@ApiOperation(value = "评论列表")
public ResponseResult commentList(Long articleId, Integer pageNum, Integer pageSize){
return commentService.commentList(SystemConstants.ARTICLE_COMMENT,articleId,pageNum,pageSize);
}
1
2
3
4
5
2
3
4
5
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
@Autowired
private UserService userService;
@Override
public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId)
.eq(Comment::getRootId,-1)// 根评论
.eq(Comment::getType,commentType);
Page<Comment> page = new Page(pageNum, pageSize);
page(page,queryWrapper);
List<Comment> comments = page.getRecords();
List<CommentVo> commentVoList = toCommentVoList(comments);
for (CommentVo commentVo : commentVoList) {
List<CommentVo> children = getChildren(commentVo.getId());
commentVo.setChildren(children);
}
return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
}
private List<CommentVo> toCommentVoList(List<Comment> list){
List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
for (CommentVo commentVo : commentVos) {
// comment没有userName字段
String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
commentVo.setUsername(nickName);
// comment没有toCommentUserName字段
if(commentVo.getToCommentUserId()!=-1){
String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
commentVo.setToCommentUserName(toCommentUserName);
}
}
return commentVos;
}
private List<CommentVo> getChildren(Long id){
LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
// 子评论:评论的根评论id 为 id
queryWrapper.eq(Comment::getRootId, id)
.orderByAsc(Comment::getCreateTime);
List<Comment> comments = list(queryWrapper);
List<CommentVo> commentVos = toCommentVoList(comments);
return commentVos;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 发表评论
时间字段填充:
entity中字段上创建和更新分别添加注解:
@TableField(fill = FieldFill.INSERT) @TableField(fill = FieldFill.INSERT_UPDATE)
1
2MyMetaObjectHandler:
@Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { Long userId = null; try { userId = SecurityUtils.getUserId(); } catch (Exception e) { e.printStackTrace(); userId = -1L;// 表示是自己创建 } this.setFieldValByName("createTime", new Date(), metaObject); this.setFieldValByName("createBy",userId , metaObject); this.setFieldValByName("updateTime", new Date(), metaObject); this.setFieldValByName("updateBy", userId, metaObject); } @Override public void updateFill(MetaObject metaObject) { this.setFieldValByName("updateTime", new Date(), metaObject); this.setFieldValByName(" ", SecurityUtils.getUserId(), metaObject); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
接口:
@PostMapping
@ApiOperation(value = "发表评论")
public ResponseResult addComment(@RequestBody Comment comment){
return commentService.addComment(comment);
}
1
2
3
4
5
2
3
4
5
@Override
public ResponseResult addComment(Comment comment) {
if(!StringUtils.hasText(comment.getContent())){
throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
}
save(comment);
return ResponseResult.okResult();
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8