文章目录

  • 1.用户为视频留言/回复功能
  • 1.需求分析
  • 2.代码实现
  • 2.查询用户留言列表功能的实现
  • 1.需求分析
  • 2.代码实现


1.用户为视频留言/回复功能

1.需求分析

我们需要前端传入Comments实体类中的相关参数,通过service的保存方法,将留言保存到数据库即可,根据Comments的参数数量来判断用户是留言还是为回复留言。

2.代码实现

dao层代码

public interface CommentsDao  extends JpaRepository<Comments,String> ,JpaSpecificationExecutor<Comments> {
}

service层代码

/**
     * 保存用户评论
     * @param comments
     */
    @Override
    @Transactional
    public void saveComments(Comments comments) {
        comments.setId(IdUtils.getId());
        comments.setCreateTime(new Date());
        commentsDao.save(comments);
    }

controller层代码

@ApiOperation(value = "用户评论的接口/回复用户的留言", notes = "用户评论的接口/回复用户的留言")
    @PostMapping(value = "/saveComments")
    public LexJSONResult saveComments(@RequestBody Comments comments){
        videosService.saveComments(comments);
        return LexJSONResult.ok();
    }

2.查询用户留言列表功能的实现

1.需求分析

该接口需要对该视频的所有评论进行查询,查询结果需要进行分页,需要前端将评论的页数以及视频的id作为参数传入,我们早controller层定义每页显示的页数为4,将查询到的结果已vo包装类的形式返回,因为在前端需要显示评论者的昵称,评论者的头像,以及多长时间前评论或回复的,这里我们需要封装一个时间的工具类对时间进行一下统一的处理,在vo类中加上一个新的显示时间的字段,用来存放我们处理后的时间数据,将用户的头像信息用户的昵称都作为新的字段加入vo类中,以pageResult的形式返回给前端。

2.代码实现

service层代码

/**
     * 分页查询全部的评论
     * @param videoId
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult findComments(final String videoId, Integer page, Integer size) {
        //创建分页的条件
        Pageable pageable =new PageRequest(page-1,size);
        //创建返回给前端的分页结果类
        PageResult pageResult =new PageResult();
        //创建一个用来装返回结果的集合
        List<CommentsVo> rows =new ArrayList<CommentsVo>();
        //根据视频的id分页查询数据,并按照创建时间降序排列
        Page<Comments> commentsPage = commentsDao.findAll(new Specification<Comments>() {
            @Override
            public Predicate toPredicate(Root<Comments> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                //将实体类中的videoId作为参数传入
                Predicate p1 = cb.equal(root.get("videoId").as(String.class), videoId);
                //通过这个条件进行查询
                cq.where(p1);
                //通过创建实现对查询结果进行降序排列
                cq.orderBy(cb.desc(root.get("createTime").as(Date.class)));
                return cq.getRestriction();
            }
        }, pageable);
        //设置当前的页数
        pageResult.setPage(page);
        //设置总页数
        pageResult.setTotalPage(commentsPage.getTotalPages());
        //设置当前元素数
        pageResult.setTotalElements(commentsPage.getTotalElements());
        //进行数据收集,将数据拼装成需要返回给前端的vo类
        for (Comments comments:commentsPage){
            CommentsVo commentsVo =new CommentsVo();
            //使用我们封装好的显示时间的工具类,对创建时间进行处理,使得在前端页面以xx前的形式显示
            commentsVo.setTimeAgo(TimeAgoUtils.format(comments.getCreateTime()));
            //查询评论者的相关用户信息
            Users one = usersDao.findOne(comments.getFromUserId());
            //如果getToUserId不为空,说明有用户对该条评论做出了回复
            if(!StringUtils.isEmpty(comments.getToUserId())){
                //查询到被回复对象的信息
                Users one1 = usersDao.findOne(comments.getToUserId());
                //将用户的昵称封装到vo中
                commentsVo.setToNickname(one1.getNickname());
            }
            //将用户的头像信息存放到vo中
            commentsVo.setFaceImage(one.getFaceImage());
            //将用户的昵称存放到vo中
            commentsVo.setNickname(one.getNickname());
            //将查询到的comments类拷贝到vo对象中
            BeanUtils.copyProperties(comments,commentsVo);
            //将这个对象存放近列表
            rows.add(commentsVo);
        }
        //将列表结果存放到分页结果对象中
        pageResult.setRows(rows);
        //返回分页结果对象
        return pageResult;
    }

controller层代码

//默认每页显示数据数
 private final Integer SIZE=4;
@ApiOperation(value = "查询用户留言", notes = "查询用户留言")
    @PostMapping(value = "/findComments")
    public LexJSONResult findComments(String videoId,Integer page){
        PageResult pageResult = videosService.findComments(videoId, page, SIZE);
        return LexJSONResult.ok(pageResult);
    }