首页 > 其他分享 >点赞收藏关注feed的实现

点赞收藏关注feed的实现

时间:2023-01-24 22:34:27浏览次数:41  
标签:feed 点赞 userId 收藏 user follow import com id

package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@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);
            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();
    }

    @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 = ?
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        // 3.判断
        return Result.ok(count > 0);
    }

    @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);
    }
}

标签:feed,点赞,userId,收藏,user,follow,import,com,id
From: https://www.cnblogs.com/xuechuyang/p/17066465.html

相关文章

  • 我的收藏周刊043
    文章分享亚马逊第1天文化要素博客分享lyric海德沙龙NickLane站点分享3.S3IP书籍分享文学纪念碑广西师范大学出版社出版,其中的陀思妥耶夫斯基系列......
  • 17个面向Web 开发人员的杀手级网站,值得你收藏
    保持网站方便可能是最终的生产力技巧,以下是我用来让我的生活更轻松的一些最好的网站。让我们一起来看看它们。1、图片API地址:https://source.unsplash.com/世界上最强大的......
  • Hive 刷题——向用户推荐朋友收藏的商品
    需求描述现需要请向所有用户推荐其朋友收藏但是用户自己未收藏的商品,请从好友关系表(friendship_info)和收藏表(favor_info)中查询出应向哪位用户推荐哪些商品。期望结果如下......
  • 张寓博当选山东省收藏者协会副主席兼美术评论委员会主任
    内容:张寓博1990年出生于山东即墨青岛著名公益人企业家美术评论家收藏家受到过众多政治家以及老一辈艺术名家亲临指导对其美术评论收藏领域辨别真假有较深造诣经过......
  • SQL 优化 20连问,建议收藏
    一、查询SQL尽量不要使用select*,而是具体字段1、反例SELECT * FROM user2、正例SELECT id,username,tel FROM user3、理由节省资源、减少网络开销。可能用......
  • 我的收藏周刊042
    文章分享一个“水龙头”,让可编程网络硬件火了这篇文章提到了一个观点,即在网络基础设施之上提供了各种客户定制化方案,也就是云计算模式,但是对于基础网络设施来说却越来越......
  • 新手最应记住和收藏的 Mac 键盘快捷键列表整理
    最近趁着「苹果教育优惠免费送AirPods」,很多同学都纷纷入手了新的Mac!然而对于初次接触macOS的朋友来说,除了要寻找不同的APP软件之外,还有一件事情也直接影响着使用电脑......
  • b站收藏夹xhr地址
    https://api.bilibili.com/x/v3/fav/resource/list?media_id=&pn=1&ps=20&keyword=&order=mtime&type=0&tid=0&platform=web&jsonp=jsonp  我曾经看过一个视频,查找......
  • BBS项目文章详情页点赞点踩以及评论
    目录BBS项目文章详情页点赞点踩以及评论一、编写思路概览二、编写思路详细1.开设一个全新的路由2.写视图函数3.写文章详情页前端页面4.开设文章点赞点踩以及评论路由5.写点......
  • 2023年了,这些Linux命令得会吧!还不收藏起来?
    一、关机与重启指令(1)shutdown-关机语法:shutdownshutdown-hnow【立即关机】shutdown-h1【1分钟后关机,shutdown后不填默认一分钟后关机】shutdown-rnow【立即重启......