springboot 实现帖子详情及评论

1.帖子详情及评论接口

@GetMapping("/detail/{discussPostId}")
public String getDiscussPost(@PathVariable("discussPostId") int postId, Model model, Page page) {
    //帖子
    DiscussPost post = discussPostService.findDiscussPostById(postId);
    model.addAttribute("post", post);
    //作者
    User user = userService.findUserById(post.getUserId());
    model.addAttribute("user", user);
    //评论的分页信息
    page.setLimit(5);
    page.setPath("/discuss/detail/" + postId);
    page.setRows(post.getCommentCount());
    //评论列表
    List<Comment> commentList = commentService.findCommentEntity(ENTITY_TYPE_POST, postId, page.getOffset(), page.getLimit());
    //评论Vo列表
    ArrayList<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()));
            //回复列表
            List<Comment> replyList = commentService.findCommentEntity(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> replyVo = new HashMap<>();
                    //回复
                    replyVo.put("reply", reply);
                    //作者
                    replyVo.put("user", userService.findUserById(reply.getUserId()));
                    //目标
                    User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId());
                    replyVo.put("target", target);
                    replyVoList.add(replyVo);
                }
            }
            commentVo.put("replys", replyVoList);
            //回复数量
            commentVo.put("replyCount", commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId()));
            commentVoList.add(commentVo);
        }
    }
    model.addAttribute("comments", commentVoList);
    return "site/discuss-detail";
}

在这里插入图片描述

  • commentVoList:评论集合
  • comment:评论实体
  • user:评论的作者
  • replyVoList:回复集合(回复:评论的评论)
    – reply:回复实体
    – user:回复的作者
    – target:回复的对象
  • replyCount:回复的数量

2.comment服务层

@Override
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public int addComment(Comment comment) {
    if (comment == null) throw new IllegalArgumentException("参数不能为空!");
    //添加评论
    //处理评论
    comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
    comment.setContent(sensitiveWordsFilter.filter(comment.getContent()));
    int rows = commentMapper.insertComment(comment);
    //更新评论数量

    if (comment.getEntityType() == ENTITY_TYPE_POST) {
        discussPostService.updatePostCommentCount(comment.getEntityId(), commentMapper.selectCountByEntity(ENTITY_TYPE_POST, comment.getEntityId()));
    }
    return rows;
}

@Transactional:spring自带事务管理

3.comment控制层

@PostMapping("/add/{discussPostId}")
private String addComment(@PathVariable("discussPostId") int postId, Comment comment) {
    comment.setUserId(hostHolder.getUser().getId());
    comment.setStatus(0);
    comment.setCreateTime(new Date());
    commentService.addComment(comment);
    return "redirect:/discuss/detail/" + postId;
}

4.Thymeleaf模板处理

5.最终结果

在这里插入图片描述
点击看gitee源码!

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值