Building a Douyin-like short video app from scratch - back-end development fan business module (2)

The project is continuously updated:

Anti-vibration short video APP project column

Table of contents

Determine whether the user follows the blogger

my watchlist

 my fans list

Realize mutual powder mark display


Determine whether the user follows the blogger

After we follow this person here, we log out of the interface and enter again, but it does not become followed, so we need to make a query here

 We write the interface in the service layer:

 Interface implementation:

 @Override
    public boolean queryDoIFollowVloger(String myId, String vlogerId) {
        Fans vloger = queryFansRelationship(myId,vlogerId);
        return vloger !=null;
    }

Here we first look at the front-end routing:

 Then we are in the controller layer:

    @GetMapping("queryDoIFollowVloger")
    public GraceJSONResult queryDoIFollowVloger(@RequestParam String myId,
                                  @RequestParam String vlogerId){


        return GraceJSONResult.ok(fansService.queryDoIFollowVloger(myId,vlogerId));
    }

Then we restart it, and after refreshing to return to our homepage, it becomes the status of being followed.

Note: We do this by querying the database at the service layer, but we have already processed it through redis before. So we can also query from redis here, and return to judge a true or false.

my watchlist

When we are on the pages of other bloggers, follow and fans cannot be clicked to view. On your homepage, you can view your followers and fans.

 

 Here we are all doing some corresponding queries.

We need to have a VO:

This is what we need to display to the front end, where we encapsulate the data.

We complete the writing in the custom.xml file,

   <select id="queryMyFollows" resultType="com.imooc.vo.VlogerVO" parameterType="map">

        SELECT
            u.id as vlogerId,
            u.nickname as nickname,
            u.face as face
        FROM
            fans f
                LEFT JOIN
            users u
            ON
                f.vloger_id = u.id
        WHERE
            f.fan_id = #{paramMap.myId}
        ORDER BY
            u.nickname
                ASC

    </select>

followed by a mapping

After the mapper layer is completed here, it is time to go to our business service layer:

We need to use our pagination here 

Then we need to complete our implementation. First, we need to give new the Map we need to call:

    @Override
    public PagedGridResult queryMyFollow(String myId, Integer page, Integer pageSize) {
        Map<String,Object> map=new HashMap<>();
        map.put("myId",myId);
        List<VlogerVO> list = fansMapperCustom.queryMyFollows(map);
        return setterPagedGrid(list,page);
    }

Before completing the controller layer, let's look at the front end:

In the backend:

 Then restart and do a test:

We are here to carry out a clearance operation, which is successful.

Entering again, it is the state of no attention.

 my fans list

My fan list and follow list are pretty much the same this week.

First prepare a FansVO:

isFriend indicates whether we are friends or not, and we can use it here as a marker for mutual fans.

 

Write our relevant code in custom.xml:

<select id="queryMyFans" resultType="com.imooc.vo.FansVO" parameterType="map">

        SELECT
            u.id as fanId,
            u.nickname as nickname,
            u.face as face
        FROM
            fans f
                LEFT JOIN
            users u
            ON
                f.fan_id = u.id
        WHERE
            f.vloger_id = #{paramMap.myId}
        ORDER BY
            u.nickname
                ASC

    </select>

Then into our interface:

Then write our service:

 Then implement this method:

 @Override
    public PagedGridResult queryMyFans(String myId, Integer page, Integer pageSize) {
        Map<String,Object> map=new HashMap<>();
        map.put("myId",myId);
        List<FansVO> list = fansMapperCustom.queryMyFans(map);
        return setterPagedGrid(list,page);
    }

Finally, it is quickly implemented in the controller:

Check it out first:

 Here is pretty much the same as my watchlist.

 @GetMapping("queryMyFans")
    public GraceJSONResult queryMyFans(@RequestParam String myId,
                                          @RequestParam Integer page,
                                          @RequestParam Integer pageSize){


        return GraceJSONResult.ok(fansService.queryMyFans(myId,page,pageSize));
    }

Reboot, and test it out:

Here my number of fans is 0, but actually clicked in, there are fans 

We click to enter, there are still fans.

In fact, the data has been saved in the database, but it has not been implemented in our redis, so it is not displayed.

 The back follower here is another marked state, which we will implement later.

Realize mutual powder mark display

In our vo, the default isFriend is false, regardless of whether it is a friend or not, so the display mark is back to fans

So we have to do a query to check whether this isFriend is true or false.

We modify the previous code in the service, and perform circular query on reids in it.

Note:
        /**
         * Determine whether a fan is my friend (mutual fan, mutual relationship)
         *Common method:
         * Multi-table association + nested association query, this will violate the specification of multi-table association, it is not advisable, and high concurrency will appear Performance issues
         *
         * Conventional practice:
         * 1. Avoid too many table association queries, first query my fans list, get fansList
         * 2. Avoid fans following me, and I also follow fans, --" cycle fansList, get each Fans, go to the database to check whether I follow him
         * 3. If I also follow him (fans), it means that we are friends with each other, then mark the flag as true
         *
         * High-end practice:
         * 1. When following/unfollowing, link The relationship is stored in redis, do not rely on the database
         * 2. After the database query, directly loop through redis to avoid the embarrassing situation of looping the database for the second time
         */


    @Override
    public PagedGridResult queryMyFans(String myId, Integer page, Integer pageSize) {

        /**
         * 判断粉丝是否是我的朋友(互粉,互关)
         *普通做法:
         * 多表关联+嵌套关联查询,这样会违反多表关联的规范,不可取,高并发会出现性能问题
         *
         * 常规做法:
         * 1.避免过多的表关联查询,先查询我的粉丝列表,获得fansList
         * 2.避免粉丝关注我,并且我也关注粉丝,--》循环fansList,获得每一个粉丝,再去数据库查询我是否关注他
         * 3.如果我也关注他(粉丝),说明我俩互为朋友,则标记flag为true
         *
         * 高端做法:
         * 1.关注/取关的时候,关联关系保存在redis中,不要依赖数据库
         * 2.数据库查询后,直接循环查询redis,避免第二次循环查询数据库的尴尬局面
         */
        Map<String,Object> map=new HashMap<>();
        map.put("myId",myId);
        List<FansVO> list = fansMapperCustom.queryMyFans(map);
        for(FansVO f: list){
           String relationship  = redis.get(REDIS_FANS_AND_VLOGGER_RELATIONSHIP+":"+myId+":"+f.getFanId());
           if(StringUtils.isNotBlank(relationship) && relationship.equalsIgnoreCase("1")){
                f.setFriend(true);
           }
        }
        return setterPagedGrid(list,page);
    }

 Restart it, if it is a friend here, it will show mutual relations.

 Then the display of mutual powder marks is completed above.

Guess you like

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