Build a vibrato-like short video APP from scratch--back-end development fan business module (3)

The project continues to be created:

Imitation vibrato short video APP column

Table of contents

Enable users to like videos

User unlikes

Judging whether the user likes the video


Enable users to like videos

After users like our videos, we need to implement some related businesses,

Here we need to correspond to a table in the database, which is my_liked_vlog, which is the list of videos I have liked.

After likes, the number of likes of this video will increase by one, and our redis technology will be used.

First in our service:

 accomplish:

  @Transactional
    @Override
    public void userLikeVlog(String userId, String vlogId) {
        String rid = sid.nextShort();
        MyLikedVlog likedVlog = new MyLikedVlog();
        likedVlog.setId(rid);
        likedVlog.setVlogId(vlogId);
        likedVlog.setUserId(userId);
        myLikedVlogMapper.insert(likedVlog);
    }

Pay attention to adding an injection MyLikeVlogMapper in front of us

Look at the front end before writing the controller:

We are in the backend:

 @PostMapping("like")
    public GraceJSONResult myPrivateList(@RequestParam String userId,
                                         @RequestParam String vlogerId,
                                         @RequestParam String vlogId) {

        //我点赞的视频,关联保存到数据库
        vlogService.userLikeVlog(userId,vlogId);

        //点赞后,视频和视频发布者的或者都会+1;
        redis.increment(REDIS_VLOGER_BE_LIKED_COUNTS+":"+vlogerId,1);
        redis.increment(REDIS_VLOG_BE_LIKED_COUNTS+":"+vlogId,1);
        //我点赞的视频,需要在redis中保存关联关系
        redis.set(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId,"1");

        return GraceJSONResult.ok();
    }

 It would be better if the redis operation here can be placed in our service.

It is also best to come down here and do a check to determine whether the two ids exist, as we wrote in our previous article.

Start, test:

After clicking, the red heart here lights up

 Let's open the database and have a look:

Here is the successful insertion

 Open redis to view:

 Here is exactly the same, the description is successful

 The user's like operation is realized.

User unlikes

The request initiated by our front end here is called unlike 

Let's write it in the backend, the parameters carried are the same as above, and the operation and like are opposite.

   @PostMapping("unlike")
    public GraceJSONResult unlike(@RequestParam String userId,
                                         @RequestParam String vlogerId,
                                         @RequestParam String vlogId) {

        //我取消点赞的视频,关联关系删除
        vlogService.userUnLikeVlog(userId,vlogId);

        //点赞后,视频和视频发布者的或者都会+1;
        redis.decrement(REDIS_VLOGER_BE_LIKED_COUNTS+":"+vlogerId,1);
        redis.decrement(REDIS_VLOG_BE_LIKED_COUNTS+":"+vlogId,1);

        redis.del(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId);

        return GraceJSONResult.ok();
    }

It is still recommended that you write redis in service for processing.

Then service:

accomplish: 

Here, the matching data will be matched and deleted according to the vlogeId and userId.

  @Transactional
    @Override
    public void userUnLikeVlog(String userId, String vlogId) {
        MyLikedVlog likedVlog = new MyLikedVlog();
        likedVlog.setVlogId(vlogId);
        likedVlog.setUserId(userId);
        myLikedVlogMapper.delete(likedVlog);
    }

Reboot, test:

 Here we cancel likes.

Open the database:

Here is the original data that has not been refreshed

 After refreshing, this record will disappear.

 Then go to redis to view:

 refresh:

Cumulative decrement becomes 0

 Also deleted here.

 Let's look at the effect on the page, first give a thumbs up to the video

 The number of likes has become 1

 Cancel the like, the number of likes becomes 0:

 We will find some small problems here, in the previous development:

There is actually something wrong here

 The total number of likes for a user should be the sum of vloggers (likes/likes)

The first record never exists 

 This fixes such a piece of code.

Judging whether the user likes the video

We actually have a little problem here, because when we pull down to refresh, although our data is stored in the database, it is not actually processed on the front end, and the heart is still not bright after it is refreshed.

 For our backend, when entering this interface, we should judge whether we have liked this video before. If we liked it, it should be on when we see this video again.

We're going to make an extension to the method here:

After we get the list, we need to make a loop judgment to judge whether the user has liked the video.

first:

 We need to add a parameter userId here, which is actually visible on our front end.

Here we need to add an incoming parameter at the interface method.

In the service implementation, we need to loop through the returned list,

 for(IndexVlogVO v : list) calls the method to query whether I have liked this video in reids.

  @Override
    public PagedGridResult getIndexVlogList(String userId,
                                            String search,
                                            Integer page,
                                            Integer pageSize){
        PageHelper.startPage(page,pageSize);

        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isNotBlank(search)){
            map.put("search",search);
        }
       List<IndexVlogVO> list= vlogMapperCustom.getIndexVlogList(map);
//        return list;
        for(IndexVlogVO v : list){
            String vlogerId = v.getVlogerId();
            String vlogId = v.getVlogId();
            if(StringUtils.isNotBlank(userId)){
                v.setDoILikeThisVlog(doILikeVlog(userId,vlogId));
            }

        }
        return setterPagedGrid(list,page);
    }
    private boolean doILikeVlog(String myId,String vlogId){
        String doILike = redis.get(REDIS_USER_LIKE_VLOG+":"+myId+":"+vlogId);
        boolean isLike = false;
        if(StringUtils.isNotBlank(doILike) && doILike.equalsIgnoreCase("1")){
            isLike  = true;
        }
        return isLike;
    }

Reboot, test:

We do a pull-to-refresh

 Here we have completed one of our judgments whether the user has liked the video.

Guess you like

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