删除评论对应的数量问题

main
forely 4 weeks ago
parent b626045de2
commit 27f29826bc

@ -0,0 +1,22 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Edge",
"request": "launch",
"type": "msedge",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
},
{
"type": "msedge",
"request": "launch",
"name": "针对 localhost 启动 Edge",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}

@ -185,22 +185,42 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> impl
@Transactional(rollbackFor = Exception.class)
public void deleteComment(Long id) {
validatePostUtil.validateCommentOwnership(id);
Comment comment = commentMapper.selectById(id);
if(comment.getId().equals(comment.getTopId())) {
LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery(Comment.class)
.eq(Comment::getTopId, id);
int delete = commentMapper.delete(queryWrapper);
if(delete <= 0) {
throw new PostException("删除评论失败");
throw new PostException("删除评论失败");
}
Comment comment = commentMapper.selectById(id);
if(comment.getId().equals(comment.getTopId())) {
Post post = postMapper.selectById(comment.getPostId());
post.setCommentCount(post.getCommentCount() - delete);
postMapper.updateById(post);
redisUtil.zRemove("post:comment_by_time:" + comment.getPostId(), comment.getId());
redisUtil.zRemove("post:comment_by_hot:" + comment.getPostId(), comment.getId());
redisUtil.delete("comment:reply_by_time:" + comment.getTopId());
redisUtil.delete("comment:reply_by_hot:" + comment.getTopId());
}else{
int delete = commentMapper.deleteById(id);
if(delete <= 0){
throw new PostException("删除评论失败");
}
Post post = postMapper.selectById(comment.getPostId());
post.setCommentCount(post.getCommentCount() - delete);
postMapper.updateById(post);
Comment parentComment = commentMapper.selectById(comment.getParentCommentId());
Comment topComment = commentMapper.selectById(comment.getTopId());
parentComment.setReplyCount(parentComment.getReplyCount() - delete);
topComment.setReplyCount(topComment.getReplyCount() - delete);
updateById(parentComment);
updateById(topComment);
redisUtil.zRemove("comment:reply_by_time:" + comment.getTopId(), comment.getId());
redisUtil.zRemove("comment:reply_by_hot:" + comment.getTopId(), comment.getId());
}
// TODO 如果根评论删除,那么其他评论怎么办,目前做法是删除其下所有的子评论
}

Loading…
Cancel
Save