作者和粉丝

follow控制器 - 图1

一个用户有两个角色 : 作者 和 粉丝 作者 和 粉丝 的关系 又是多对多关系 : 一个作者可以有多个粉丝 + 一个粉丝可以关注多个作者

关联表实现

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
  • 返回被关注者信息
    • 基本信息和被关注状态

实现代码

  1. // 添加关注
  2. module.exports.follow = async (req, res,next) => {
  3. try {
  4. // ● 获取参数 : 被关注的用户名
  5. const username = req.params.username
  6. // ● 校验 : 提供被关注用户
  7. // ○ 参数校验
  8. // ○ 业务验证:被关注用户是否存在
  9. const userA = await User.findOne({
  10. where:{
  11. username
  12. }
  13. })
  14. if(!userA){
  15. throw new HttpException(404,'被关注的用户名称不存在','user with this username not found')
  16. }
  17. // ● 关注者信息(粉丝)
  18. // ○ 获取email : 通过token
  19. const {email} = req.user
  20. // ○ 获取用户信息
  21. const userB = await User.findByPk(email)
  22. // ● 添加关注
  23. // ○ 建立关系:被关注者主键和关注者主键存储到数据库中表followers
  24. await userA.addFollowers(userB)
  25. // ● 返回被关注者信息
  26. // ○ 基本信息和被关注状态
  27. const profile = {
  28. username:userA.username,
  29. bio:userA.bio,
  30. avatar:userA.avatar,
  31. following:true
  32. }
  33. res.status(200)
  34. .json({
  35. status:1,
  36. message:'关注成功',
  37. data:profile
  38. })
  39. } catch (error) {
  40. next(error)
  41. }
  42. };

取消关注

实现思路

  • 获取参数 : 被关注的用户名
  • 校验 : 提供被关注用户
    • 参数校验
    • 业务验证:被关注用户是否存在
  • 关注者信息(粉丝)
    • 获取email : 通过token
    • 获取用户信息
  • 取消关注
    • 建立关系:被关注者主键和关注者主键存储到数据库中表followers
  • 返回被关注者信息
    • 基本信息和被关注状态

实现代码

  1. // 取消关注
  2. module.exports.cancelFollow = async (req, res,next) => {
  3. try {
  4. // ● 获取参数 : 被关注的用户名
  5. const username = req.params.username
  6. // ● 校验 : 提供被关注用户
  7. // ○ 参数校验
  8. // ○ 业务验证:被关注用户是否存在
  9. const userA = await User.findOne({
  10. where:{
  11. username
  12. }
  13. })
  14. if(!userA){
  15. throw new HttpException(404,'被关注的用户名称不存在','user with this username not found')
  16. }
  17. // ● 关注者信息(粉丝)
  18. // ○ 获取email : 通过token
  19. const {email} = req.user
  20. // ○ 获取用户信息
  21. const userB = await User.findByPk(email)
  22. // ● 添加关注
  23. // ○ 建立关系:被关注者主键和关注者主键存储到数据库中表followers
  24. await userA.removeFollowers(userB)
  25. // ● 返回被关注者信息
  26. // ○ 基本信息和被关注状态
  27. const profile = {
  28. username:userA.username,
  29. bio:userA.bio,
  30. avatar:userA.avatar,
  31. following:false
  32. }
  33. res.status(200)
  34. .json({
  35. status:1,
  36. message:'取消关注成功',
  37. data:profile
  38. })
  39. } catch (error) {
  40. next(error)
  41. }
  42. };

获取作者粉丝

实现思路

  • 获取参数 : 作者用户名 userAuthor
  • 校验 : 提供被关注用户
    • 参数校验
    • 业务验证:
      • 获取作者信息 : 连表查询 获取所有粉丝 [emails]
      • 作者信息是否存在
  • 验证是否关注
    • 当前登录粉丝 email : 通过token
    • 是否关注 :判断 当前登录的用户email 是否再作者的所有粉丝的emails 里面
  • 返回被关注者信息
    • 基本信息
    • 关注状态
    • 粉丝信息

实现代码

  1. // 获取粉丝 & 判断当前登录的用户是否关注
  2. module.exports.getFollowers = async (req, res,next) => {
  3. try {
  4. // ● 获取参数 : 作者用户名 userAuthor
  5. const username = req.params.username
  6. // ● 校验 : 提供被关注用户
  7. // ○ 参数校验
  8. // ○ 业务验证:
  9. // ■ 获取作者信息 : 连表查询 获取所有粉丝 [emails]
  10. // ■ 作者信息是否存在
  11. const userAuthor = await User.findOne({
  12. where:{
  13. username
  14. },
  15. include:['followers'] //通过followers 中间表关联查询 [user1,user2]
  16. })
  17. // console.log('userAuthor',userAuthor);
  18. if(!userAuthor){
  19. throw new HttpException(404,'被关注的用户名称不存在','user with this username not found')
  20. }
  21. // ● 验证是否关注
  22. // ○ 当前登录粉丝 email : 通过token
  23. // ○ 是否关注 :判断 当前登录的用户email 是否再作者的所有粉丝的emails 里面
  24. const {email} = req.user
  25. let following = false
  26. let followers = []
  27. for (const user of userAuthor.followers) {
  28. // console.log(user.dataValues);
  29. if(email===user.dataValues.email){
  30. following = true
  31. }
  32. delete user.dataValues.password
  33. delete user.dataValues.Followers
  34. followers.push(user.dataValues)
  35. }
  36. // console.log(followers);
  37. // ● 返回被关注者信息
  38. // ○ 基本信息
  39. // ○ 关注状态
  40. // ○ 粉丝信息
  41. const profile ={
  42. username:userAuthor.username,
  43. bio:userAuthor.bio,
  44. avatar:userAuthor.avatar,
  45. following,
  46. followers
  47. }
  48. res.status(200)
  49. .json({
  50. status:1,
  51. message:'获取关注信息成功',
  52. data:profile
  53. })
  54. } catch (error) {
  55. next(error)
  56. }
  57. };