Building a Douyin-like short video app from scratch - developing a comment business module (1)

The project is continuously updated:

Imitation vibrato short video APP column

Table of contents

User comments

Display of the total number of comments

 Elegant display of front-end number totals

 sql script to query the comment list


User comments

There is a comment icon in the middle of our page, we can click some

 There will be a popup

one click to send

 

 

The first thing we have to do is create a comment, post a comment. First create our basic controller:

Here we don't write it first, write to view the front-end routing

 

We prepared a CommentBO object in advance

The fatherCommentId here means that you have posted a comment, other users can reply to you, and your own record will become the parent record

commentUserId refers to the user id of the current login user leaving a message

 Let's create a service first. We need to note that when we create a comment, it is also based on a record of a single table. We do not need to customize the mapper, but use a general mapper:

Then implement our service,

public class CommentServiceImpl extends BaseInfoProperties implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private Sid sid;

    @Override
    public void createComment(CommentBO commentBO) {

        String commentId = sid.nextShort();

        Comment comment = new Comment();
        comment.setId(commentId);

        comment.setVlogId(commentBO.getVlogId());
        comment.setVlogerId(commentBO.getVlogerId());

        comment.setCommentUserId(commentBO.getCommentUserId());
        comment.setFatherCommentId(commentBO.getFatherCommentId());
        comment.setContent(commentBO.getContent());

        comment.setLikeCounts(0);
        comment.setCreateTime(new Date());

        commentMapper.insert(comment);
    }
}

Note: The number of comments and likes here is 0 by default.

Next, a very important operation needs to be added, the number of likes of comments needs to be stored in redis 

Open the front end and view the route:

 

Note: This should be changed to POST 

After the front end is executed, a new record will be obtained.

After we publish a comment, the user may not necessarily refresh the comment. We do not go to the database to query the latest comment, but put the latest comment posted by the user in the first position, and wait until it enters next time to do a refresh. paging

 At the back end, it is necessary to return a latest record to the front end for processing. At this time, we only need to build a VO again

 

 Here we have to modify the return type

 @Override
    public CommentVO createComment(CommentBO commentBO) {

        String commentId = sid.nextShort();

        Comment comment = new Comment();
        comment.setId(commentId);

        comment.setVlogId(commentBO.getVlogId());
        comment.setVlogerId(commentBO.getVlogerId());

        comment.setCommentUserId(commentBO.getCommentUserId());
        comment.setFatherCommentId(commentBO.getFatherCommentId());
        comment.setContent(commentBO.getContent());

        comment.setLikeCounts(0);
        comment.setCreateTime(new Date());

        commentMapper.insert(comment);
        //redis操作放在service中,评论总数的累加
        redis.increment(REDIS_VLOG_COMMENT_COUNTS+":"+commentBO.getVlogId(),1);

        //留言后的最新评论需要返回给前端进行展示
        CommentVO commentVO = new CommentVO();
        BeanUtils.copyProperties(comment,commentVO);

        return commentVO;
    }

 Then it can be implemented in the Controller

 Install it in Meavn, restart, and test:

The front end will make a judgment here whether it is the author, if it is the author, it will display the mark here

 

 I reply to myself:

Commenting here is a success.

Because we have not done any query here, it will not be displayed once you close the entry again.

 

 

Display of the total number of comments

On the home page, after the comments, there should be a cumulative and display of the number of comments

Open our frontend:

 Then write our controller:

  @GetMapping("counts")
    public Object counts(@RequestParam  String vlogId){

        String countsStr = redis.get(REDIS_VLOG_COMMENT_COUNTS+":"+ vlogId);
        if(StringUtils.isBlank(countsStr)){
            countsStr = "0";
        }
        return GraceJSONResult.ok(Integer.valueOf(countsStr));

    }


}

Restart the test:

After publishing, the number of comments here shows 4

 Elegant display of front-end number totals

We find the records in redis 

The number of comments displayed here is now 4

 We change it to 900, save it, there is no problem here

 

 But if we change it to 90w, it is a bit unsightly here. We can optimize it to a certain extent, more than one thousand is written as 1k

More than 10,000, write 1w

 Here we handle on the front end

 

 Then re-run the source code of the front end and return to our back end:

 We changed in redis to

 

 sql script to query the comment list

Realized the publication of comments and the display of the number of comments. When we enter the comment page, the comment record should be displayed here:

 This will involve queries, and we need to use the association of our two tables: 

But here we will involve the current user and the replying user, the current comment and the replying comment, so it is actually a query relationship related to the four tables

First write a custom Mapper:

 

Then complete our sql statement

 `` This symbol stands for removing keywords

<mapper namespace="com.imooc.mapper.CommentMapperCustom" >

  <select id="getCommentList" parameterType="map" resultType="com.imooc.vo.CommentVO">
    SELECT
      c.id as commentId,
      c.vlog_id   as vlogId,
      u.id as vlogerId,
      u.nickname as commentUserNickname,
      u.face as commentUserFace,
      c.father_comment_id as fatherCommentId,
      c.comment_user_id as commentUserId,
      c.content as content,
      c.like_counts as likeCounts,
      fu.nickname as replyedUserNickname,
      c.create_time as createTime
    FROM
      `comment` as c
        LEFT JOIN
      users as u
      ON
        c.comment_user_id = u.id
        LEFT JOIN
      `comment` as fc
      ON
        c.father_comment_id = fc.id
        LEFT JOIN
      users as fu
      ON
        fc.comment_user_id = fu.id
    WHERE
      c.vlog_id = #{paramMap.vlogId}
    ORDER BY
      c.like_counts DESC,
      c.create_time DESC


  </select>
</mapper>

 The logical relationship here should be cleared

 Thinking: Ali's specification clearly stipulates that no more than three tables can be associated. Here we have reached four tables. How should we optimize it so that the SQL script query becomes a three-table or two-table query, or can we not use the database and use What about other means (middleware) to achieve it?

Guess you like

Origin blog.csdn.net/m0_64005381/article/details/127640918