问答社区-显示、添加评论
1.显示评论
经典开发步骤,没有什么复杂的功能,就是查询出来的数据显示在前台页面上
数据层:
- 根据实体查询一页评论数据
- 根据实体查询评论的数量
业务层: - 处理查询评论的业务
- 处理查询评论数量的业务
表现层: - 显示帖子详情数据时,同时显示该帖子的所有评论
前台页面: - 根据返回的对象,循环输出帖子评论、回复
这里只展示controller层的代码
@RequestMapping(value = "/detail/{discussPostId}", method = RequestMethod.GET) public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page) { //帖子 DiscussPost post = discussPostService.findDiscussPostById(discussPostId); model.addAttribute("post", post); //作者 User user = userService.findUserById(post.getUserId()); model.addAttribute("user", user); //点赞 long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, discussPostId); model.addAttribute("likeCount", likeCount); //点赞状态 int likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_POST, discussPostId); model.addAttribute("likeStatus", likeStatus); //评论分页信息 page.setLimit(5); page.setPath("/discuss/detail/" + discussPostId); page.setRows(post.getCommentCount()); //评论:帖子的评论 //回复:评论的评论 //评论列表 List<Comment> commentList = commentService.findCommentsByEntity(ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit()); //评论显示列表 List<Map<String, Object>> commentVoList = new ArrayList<>(); if (commentList != null) { for (Comment comment : commentList) { //评论Vo Map<String, Object> commentVo = new HashMap<>(); //评论 commentVo.put("comment", comment); //作者 commentVo.put("user", userService.findUserById(comment.getUserId())); //目前还没开发点赞功能,下集更新 //点赞 likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, comment.getId()); commentVo.put("likeCount", likeCount); //点赞状态 likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, comment.getId()); commentVo.put("likeStatus",likeStatus); //回复列表 List<Comment> replyList = commentService.findCommentsByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE); //回复Vo列表 List<Map<String, Object>> replyVoList = new ArrayList<>(); if (replyList != null) { for (Comment reply : replyList) { Map<String, Object> replayVo = new HashMap<>(); //回复 replayVo.put("reply", reply); //回复的作者 replayVo.put("user", userService.findUserById(comment.getUserId())); //回复目标 User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId()); replayVo.put("target", target); //点赞 likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, reply.getId()); replayVo.put("likeCount",likeCount); //点赞状态 likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, reply.getId()); replayVo.put("likeStatus",likeStatus); replyVoList.add(replayVo); } } commentVo.put("replys", replyVoList); //回复数量 int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId()); commentVo.put("replyCount", replyCount); commentVoList.add(commentVo); } } model.addAttribute("comments", commentVoList); return "/site/discuss-detail"; }
2.添加评论
也是经典开发步骤了,没难点
不过这里要注意的一点,因为要增加评论,提交的时候可能会出现些许问题,我们这里用声明式事务处理掉
数据层:
- 增加评论数据
- 修改帖子评论数量
业务层: - 处理添加评论的业务-先增加评论、再更新帖子的评论数量
表现层: - 处理添加评论数据的请求
- 设置添加评论的表单
这里展示业务层和表现层
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) public int addComment(Comment comment) { transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); if (comment == null) { throw new IllegalArgumentException("参数不能为空!"); } comment.setContent(HtmlUtils.htmlEscape(comment.getContent())); comment.setContent(sensitiveFilter.filter(comment.getContent())); int rows = commentMapper.insertComment(comment); //更新帖子评论数量 if (comment.getEntityType() == ENTITY_TYPE_POST) { int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId()); discussPostService.updateCommentCount(comment.getEntityId(), count); } return rows; }
@RequestMapping(value = "/add/{discussPostId}", method = RequestMethod.POST) public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) { comment.setUserId(hostHolder.getUser().getId()); comment.setStatus(0); comment.setCreateTime(new Date()); commentService.addComment(comment); //触发评论事件 Event event = new Event().setTopic(TOPIC_COMMENT). setUserId(hostHolder.getUser().getId()). setEntityType(comment.getEntityType()). setEntityId(comment.getEntityId()). setData("postId", discussPostId); if (comment.getEntityType() == ENTITY_TYPE_POST) { int userId = discussPostService.findDiscussPostById(comment.getEntityId()).getUserId(); event.setEntityUserId(userId); }else if(comment.getEntityType() == ENTITY_TYPE_COMMENT){ Comment target = commentService.findCommentById(comment.getEntityId()); event.setEntityUserId(target.getUserId()); } eventProducer.fireEvent(event); if(comment.getEntityType() == ENTITY_TYPE_POST){ event = new Event() .setTopic(TOPIC_PUBLISH) .setUserId(hostHolder.getUser().getId()) .setEntityId(discussPostId) .setEntityType(ENTITY_TYPE_POST); eventProducer.fireEvent(event); //帖子分数 String redisKey = RedisKeyUtil.getPostScoreKey(); redisTemplate.opsForSet().add(redisKey,discussPostId); } return "redirect:/discuss/detail/" + discussPostId; }