一、课程介绍
【课程名称】SpringBoot 2.x 实战仿B站高性能后端项目。
【章节】第三章3.17、3.18、3.19 添加用户关注功能(上、中、下)
【讲师】HELLOSTAR
二、课程内容
1.用户关注功能介绍
用户可以关注系统中某些用户,一旦被关注者发布了新消息或者新视频,会自动通知粉丝。
用户可以自定义关注分组,选择关注某个用户时能选择添加到分租内或者自定义分组内。
相关功能接口:关注用户、关注列表、粉丝列表、分页查询用户。
2.数据库表设计
用户关注表
用户关注分组表
用户关注表
用户id
关注用户id
关注分组id
用户关注分组表
用户id
关注分组名称
关注分组类型(默认关注、特别关注、悄悄关注、自定义关注)
3.实现思路
用户可以设置自己自定义的关注分组,将用户id和分组名称和分组类型(自定义)保存在用户关注分组中,初始有3个系统默认分组(默认关注、特别关注、悄悄关注)。
当用户关注其他用户时,会在关注分组中选择保存到哪个分组,得到该用户id下的关注分组id,并且将自己的用户id和关注者用户id,统一保存在用户关注表中。如果不选择分组,则保存为默认分组。
4.部分实现代码
关注用户
@Transactional
public void addUserFollowings(UserFollowing userFollowing) {
Long groupId = userFollowing.getGroupId();
if(groupId == null){
FollowingGroup followingGroup = followingGroupService.getByType(UserConstant.USER_FOLLOWING_GROUP_TYPE_DEFAULT);
userFollowing.setGroupId(followingGroup.getId());
}else{
FollowingGroup followingGroup = followingGroupService.getById(groupId);
if(followingGroup == null){
throw new ConditionException("关注分组不存在!");
}
}
Long followingId = userFollowing.getFollowingId();
User user = userService.getUserById(followingId);
if(user == null){
throw new ConditionException("关注的用户不存在!");
}
userFollowingDao.deleteUserFollowing(userFollowing.getUserId(), followingId);
userFollowing.setCreateTime(new Date());
userFollowingDao.addUserFollowing(userFollowing);
}新建关注分组,获取分组列表
public Long addUserFollowingGroups(FollowingGroup followingGroup) {
followingGroup.setCreateTime(new Date());
followingGroup.setType(UserConstant.USER_FOLLOWING_GROUP_TYPE_USER);
followingGroupService.addFollowingGroup(followingGroup);
return followingGroup.getId();
}
public List<FollowingGroup> getUserFollowingGroups(Long userId) {
return followingGroupService.getUserFollowingGroups(userId);
}三、课程收获
用户关注的实现思路理解:主要是三个字段进行关联。分别是:用户id,关注用户id,关注分组。关注分组可以单独配置。类似与用户,角色,用户角色之间的关系。
菜鸡日常:badbadstudy~~ daydaydown~~
四、学习过程
共同学习,写下你的评论
评论加载中...
作者其他优质文章

