作者和粉丝

一个用户有两个角色 : 作者 和 粉丝 作者 和 粉丝 的关系 又是多对多关系 : 一个作者可以有多个粉丝 + 一个粉丝可以关注多个作者
关联表实现
| userA_email | userB_email |
|---|---|
| userA_email | userC_email |
| userA_email | userD_email |
| userB_email | userA_email |
| userB_email | userC_email |
| userB_email | userD_email |
查找UserA 作者的粉丝 查找userC粉丝所关注的作者
关注作者
实现思路
- 获取参数 : 作者用户名
- 校验 :作者
- 参数校验
- 业务验证:作者用户是否存在
- 粉丝信息
- 获取email : 通过token
- 获取粉丝信息
- 添加关注
- 建立关系:被关注者主键和关注者主键存储到数据库中表follows
- 返回被关注者信息
- 基本信息和被关注状态
实现代码
// 添加关注module.exports.follow = async (req, res,next) => {try {// ● 获取参数 : 被关注的用户名const username = req.params.username// ● 校验 : 提供被关注用户// ○ 参数校验// ○ 业务验证:被关注用户是否存在const userA = await User.findOne({where:{username}})if(!userA){throw new HttpException(404,'被关注的用户名称不存在','user with this username not found')}// ● 关注者信息(粉丝)// ○ 获取email : 通过tokenconst {email} = req.user// ○ 获取用户信息const userB = await User.findByPk(email)// ● 添加关注// ○ 建立关系:被关注者主键和关注者主键存储到数据库中表followersawait userA.addFollowers(userB)// ● 返回被关注者信息// ○ 基本信息和被关注状态const profile = {username:userA.username,bio:userA.bio,avatar:userA.avatar,following:true}res.status(200).json({status:1,message:'关注成功',data:profile})} catch (error) {next(error)}};
取消关注
实现思路
- 获取参数 : 被关注的用户名
- 校验 : 提供被关注用户
- 参数校验
- 业务验证:被关注用户是否存在
- 关注者信息(粉丝)
- 获取email : 通过token
- 获取用户信息
- 取消关注
- 建立关系:被关注者主键和关注者主键存储到数据库中表followers
- 返回被关注者信息
- 基本信息和被关注状态
实现代码
// 取消关注module.exports.cancelFollow = async (req, res,next) => {try {// ● 获取参数 : 被关注的用户名const username = req.params.username// ● 校验 : 提供被关注用户// ○ 参数校验// ○ 业务验证:被关注用户是否存在const userA = await User.findOne({where:{username}})if(!userA){throw new HttpException(404,'被关注的用户名称不存在','user with this username not found')}// ● 关注者信息(粉丝)// ○ 获取email : 通过tokenconst {email} = req.user// ○ 获取用户信息const userB = await User.findByPk(email)// ● 添加关注// ○ 建立关系:被关注者主键和关注者主键存储到数据库中表followersawait userA.removeFollowers(userB)// ● 返回被关注者信息// ○ 基本信息和被关注状态const profile = {username:userA.username,bio:userA.bio,avatar:userA.avatar,following:false}res.status(200).json({status:1,message:'取消关注成功',data:profile})} catch (error) {next(error)}};
获取作者粉丝
实现思路
- 获取参数 : 作者用户名 userAuthor
- 校验 : 提供被关注用户
- 参数校验
- 业务验证:
- 获取作者信息 : 连表查询 获取所有粉丝 [emails]
- 作者信息是否存在
- 验证是否关注
- 当前登录粉丝 email : 通过token
- 是否关注 :判断 当前登录的用户email 是否再作者的所有粉丝的emails 里面
- 返回被关注者信息
- 基本信息
- 关注状态
- 粉丝信息
实现代码
// 获取粉丝 & 判断当前登录的用户是否关注module.exports.getFollowers = async (req, res,next) => {try {// ● 获取参数 : 作者用户名 userAuthorconst username = req.params.username// ● 校验 : 提供被关注用户// ○ 参数校验// ○ 业务验证:// ■ 获取作者信息 : 连表查询 获取所有粉丝 [emails]// ■ 作者信息是否存在const userAuthor = await User.findOne({where:{username},include:['followers'] //通过followers 中间表关联查询 [user1,user2]})// console.log('userAuthor',userAuthor);if(!userAuthor){throw new HttpException(404,'被关注的用户名称不存在','user with this username not found')}// ● 验证是否关注// ○ 当前登录粉丝 email : 通过token// ○ 是否关注 :判断 当前登录的用户email 是否再作者的所有粉丝的emails 里面const {email} = req.userlet following = falselet followers = []for (const user of userAuthor.followers) {// console.log(user.dataValues);if(email===user.dataValues.email){following = true}delete user.dataValues.passworddelete user.dataValues.Followersfollowers.push(user.dataValues)}// console.log(followers);// ● 返回被关注者信息// ○ 基本信息// ○ 关注状态// ○ 粉丝信息const profile ={username:userAuthor.username,bio:userAuthor.bio,avatar:userAuthor.avatar,following,followers}res.status(200).json({status:1,message:'获取关注信息成功',data:profile})} catch (error) {next(error)}};
