Redis5.5、好友关注、关注和取关、共同关注、关注推销

1、关注-取关

一个接口实现关注和取消关注,2是要关注的用户id,true:关注时候传,false取关的时候传;

进到详情页会先发送一个请求,查看是否已关注该用户 ,然后可以尝试关注用户,或取消关注用户

 博主与粉丝的关系,都是用户,可以利用一张中间表来实现

 修改表,设计表将id改为自增长即可

关注:就是将关系插入到这张表中

取关:就是将表的该条数据删除即可,还有一种方式,就是新家一个字段为布尔值,true or false ,考虑到取关 会有多余的数据存在该表里,有些多余,就不用嘞

新建两个接口

@RestController
@RequestMapping("/follow")
public class FollowController {

    @Resource
    private IFollowService followService;

    /**
     * 尝试关注或取关
     * @param followUserId 被关注的用户id,谁去关注?当前登录用户
     * @param isFollow true 关注,false 取关
     * @return
     */
    @PutMapping("/{id}/{isFollow}")
    public Result follow(@PathVariable("id") Long followUserId, @PathVariable("isFollow") Boolean isFollow) {
        return followService.follow(followUserId, isFollow);
    }

    /**
     * 是否关注用户
     * @param followUserId 被关注的用户id
     * @return
     */
    @GetMapping("/or/not/{id}")
    public Result isFollow(@PathVariable("id") Long followUserId) {
        return followService.isFollow(followUserId);
    }

 实现类

@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        // String key = "follows:" + userId;
        // 1.判断到底是关注还是取关
        if (isFollow) {
            // 2.关注,新增数据
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            save(follow);
        } else {
            // 3.取关,删除 delete from tb_follow where user_id = ? and follow_user_id = ?
            // new QueryWrapper<> myBatisPlus固定写法
            remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId).eq("follow_user_id", followUserId));
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long followUserId) {
        // 1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        // 2.查询是否关注 select count(*) from tb_follow where user_id = ? and follow_user_id = ?
        // 查数据用one ,查条数用count
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        // 3.判断 >0 就是关注
        return Result.ok(count > 0);
    }

实施:点击关注,变成已关注,因为会去再查询一次,判断是否已关注 

以上代码即可实现关注-取关功能

2、共同关注 

userController新添加查询用户信息接口

/**
     * 根据用户id,查询用户信息
     * @param userId 用户id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryUserById(@PathVariable("id") Long userId){
        // 查询详情
        User user = userService.getById(userId);
        if (user == null) {
            return Result.ok();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        // 返回
        return Result.ok(userDTO);
    }

 BlogController 类新添加 查询探店笔记接口

/**
     * 根据用户id查询探店笔记,分页查询
     * @param current 页数
     * @param id 用户id
     * @return
     */
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

重启项目即可实现上述功能。查看当前用户+用户笔记;

再该页面就有一个共同关注按钮,点击该按钮会发送一个请求

共同关注:查询博主和当前登录用户关注的交集

如下图:利用set集合实现求交集

 我们如果利用Redis,就需要把当前用户关注的列表保存到redis集合,目标用户关注的列表也保存到redis集合里,然后求他们的交集即可

需要改造接口,我们每关注一个人不仅仅需要放到数据库里,同时也把它放到redis里,key就是当前用户id,值就是关注的用户的id,redis的set集合,不重复,可以存多个值

@Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        // 1.判断到底是关注还是取关
        if (isFollow) {
            // 2.关注,新增数据
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            if (isSuccess) {
                // 把关注用户的id,放入redis的set集合 sadd userId followerUserId
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
        } else {
            // 3.取关,删除 delete from tb_follow where user_id = ? and follow_user_id = ?
            boolean isSuccess = remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId).eq("follow_user_id", followUserId));
            if (isSuccess) {
                // 把关注用户的id从Redis集合中移除
                stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
            }
        }
        return Result.ok();
    }

上多个账号操作,1号关注了5号和2号,5号关注了2号(简单理解为1号关注了女神5号,和好哥们2号,5号女神关注了2号;哈哈,通过共同关注功能,1号提前发现了点事,好伤心)

FollowController 类添加 方法 查看共同关注 求交集

/**
     * 查看共同关注的人
     * @param id 查看博主的id
     * @return
     */
    @GetMapping("/common/{id}")
    public Result followCommons(@PathVariable("id") Long id){
        return followService.followCommons(id);
    }

业务层

@Override
    public Result followCommons(Long id) {
        // 1.获取当前用户
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        // 2.求交集
        String key2 = "follows:" + id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
        if (intersect == null || intersect.isEmpty()) {
            // 无交集
            return Result.ok(Collections.emptyList());
        }
        // 3.解析id集合
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        // 4.查询用户,这里顺序不重要,所以不用向以前那样麻烦了
        List<UserDTO> users = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(users);
    }

以上代码即可实现查看共同关注的列表,如:小鱼同学查看可爱多,发现他们都共同关注了:可可今天不吃肉

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值