评论置顶
这里是后台,没发表评论,可以操作评论。
这个是置顶,评论内容由门户那这发表。
接口
@PreAuthorize("@permission.admin()")
@PutMapping("/top/{commentId}")
public ResponseResult topComment(@PathVariable("commentId") String commentId) {
return commentService.topComment(commentId);
}
评论置顶的话,查询的时候,我们就需要以这个作为排序了
实现
@Override
public ResponseResult topComment(String commentId) {
Comment comment = commentDao.findOneById(commentId);
if (comment == null) {
return ResponseResult.FAILED("评论不存在.");
}
String state = comment.getState();
if (Constants.Comment.STATE_PUBLISH.equals(state)) {
comment.setState(Constants.Comment.STATE_TOP);
return ResponseResult.SUCCESS("置顶成功.");
} else if (Constants.Comment.STATE_TOP.equals(state)) {
comment.setState(Constants.Comment.STATE_PUBLISH);
return ResponseResult.SUCCESS("取消置顶.");
} else {
return ResponseResult.FAILED("评论状态非法.");
}
}
获取评论列表
接口
@PreAuthorize("@permission.admin()")
@GetMapping("/list")
public ResponseResult listComments(@RequestParam("page") int page, @RequestParam("size") int size) {
return commentService.listComments(page, size);
}
这个获取只需要根据时间排序即可,如果同学们要做得全一点,可以跟前面的文章一样,条件查询即可。
根据条件获取评论列表,比如说按时间,比如说按文章,比如说按状态.
@Override
public ResponseResult listComments(int page, int size) {
page = checkPage(page);
size = checkSize(size);
Sort sort = new Sort(Sort.Direction.DESC, "createTime");
Pageable pageable = PageRequest.of(page - 1, size, sort);
Page<Comment> all = commentDao.findAll(pageable);
return ResponseResult.SUCCESS("获取评论列表成功.").setData(all);
}
删除评论
删除评论的话,直接物理删除即可
@PreAuthorize("@permission.admin()")
@DeleteMapping("/{commentId}")
public ResponseResult deleteComment(@PathVariable("commentId") String commentId) {
return commentService.deleteCommentById(commentId);
}
门户可以删除,管理中心也可以删除。
管理员可以删除任意的,但是,门户的用户,只能删除自己的。
@Override
public ResponseResult deleteCommentById(String commentId) {
//检查用户角色
SobUser sobUser = userService.checkSobUser();
if (sobUser == null) {
return ResponseResult.ACCOUNT_NOT_LOGIN();
}
//把评论找出来,比对用户权限
Comment comment = commentDao.findOneById(commentId);
if (comment == null) {
return ResponseResult.FAILED("评论不存在.");
}
if (sobUser.getId().equals(comment.getUserId()) ||
//登录要判断角色
Constants.User.ROLE_ADMIN.equals(sobUser.getRoles())) {
commentDao.deleteById(commentId);
return ResponseResult.SUCCESS("评论删除成功.");
} else {
return ResponseResult.PERMISSION_DENIED();
}
}