个人网站优化------博客文章评论模块
摘要:
本篇将博客主要是介绍如何实现文章的评论功能,已经对评论功能的优化处理。
1 评论功能分析
对于一条评论:
- 有可能是第一条对于文章的评论;
- 有可能是子评论(也就是回复);
- 评论中是否存在敏感词汇;
- 评论下方有评论,数据库表如何设计;
- 评论不能一直评论,需要进行条数控制;
6. 评论需要管理员在后台进行管理;
2 评论功能实现思路
(1)数据库存储实现思路
对于评论的存储我们可以使用Mysql数据库进行存储,因为评论内容较少.如果访问量较高的情况加我们可以使用MongoDB进行评论的存储.这里使用Mysql进行存储.创建数据库表t_comment用于存储评论数据库.创建单表t_comments用于存储comment的ID和comment的内容,并且使用t_comments用于在后台显示对comment可以对评论进行删除和查看操作。最重要的一点,t_comment表需要进行自关联。因为一条评论可能被回复。
(2)评论存储业务逻辑实现思路
在访客在前端进行评论的时候,在t_comment中进行存储,存储成功之后,在t_comments表中在进行一次存储,此次需要将t_comment中的评论ID以及评论内容进行存储(t_comments字段只有id name commentId)。
(3)评论查看和删除业务实现
查看很简单就是使用就是使用SpringDataJpa的查询所有就可以进行展示,删除评论需要先根据t_comments的id查询出t_comment的id将评论进行删除,然后将t_comments中的评论进行删除即可。
3 后台实现逻辑代码
评论存储逻辑代码
@PostMapping("/comments")
public String post(Comment comment, HttpSession session) {
Long blogId = comment.getBlog().getId();
/**
* 根据blog查询一下评论条数,超过30条,停止写入数据库。
*/
int number = commentService.getCommentsNumByblogid(blogId);
if (number >= 30) {
return "error/NumFull";
}
/**
* 取出评论内容过滤脏字
*/
try {
//将文本中的敏感词读取出来存入List集合
ClassPathResource classPathResource = new ClassPathResource("/static/txttemplate/maren.txt");
List<String> list = FileUtil.readFile2List(classPathResource.getInputStream());
//初始化词库
Map sensitiveWordMap = sensitiveWordInit.initSensitiveWord(list);
//初始化传入SensitiveUtil的敏感词库
SensitiveUtil.sensitiveWordMap = sensitiveWordMap;
String txtString = comment.getContent();
boolean flag = SensitiveUtil.isContainsSensitiveWord(txtString, 2);
if (flag) {
return "error/zang";
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//没有脏字进行下面逻辑代码
comment.setBlog(blogService.getBlog(blogId));
User user = (User) session.getAttribute("user");
if (user != null) {
comment.setAvatar(user.getAvatar());
comment.setAdminComment(true);
} else {
comment.setAvatar(avatar);
}
Comment comment1 = commentService.saveComment(comment);
/**
* 普通用户保存评论成功需要将评论内容在t_comments表中保存一份
*/
Long commentId = comment1.getId();
String name = comment.getContent();
Comments comments = new Comments();
comments.setCommentId(commentId);
comments.setName(name);
Comments commnetsNew = commentsService.saveComments(comments);
return "redirect:/comments/" + blogId;
}
@Transactional
@Override
public Comment saveComment(Comment comment) {
Long parentCommentId = comment.getParentComment().getId();
if (parentCommentId != -1) {
comment.setParentComment(commentRepository.findOne(parentCommentId));
} else {
comment.setParentComment(null);
}
comment.setCreateTime(new Date());
return commentRepository.save(comment);
}
查看所有评论逻辑代码
/**
* 查询所有用户的的评论
*/
@GetMapping("/comments")
public String comments(@PageableDefault(size = 10, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model) {
model.addAttribute("page", commentsService.listComments(pageable));
return "admin/comments";
}
删除指定评论逻辑代码
/**
* 删除该评论:从t_comment中删除 、从t_comments表中也删除
* 事务注解,其中一个数据库表删除失败就回滚
*/
@Transactional
@GetMapping("/comments/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes) {
/*先删除该条用户评论*/
Comments comments = commentsService.findCommentsById(id);
Long commentId = comments.getCommentId();
commentService.delCommentById(commentId);
/*再删除t_comments中的记录*/
commentsService.delCommentsById(id);
/*返回操作提示*/
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/comments";
}
4 总结
此次评论管理模块还有很多不足之处需要继续改进;