灏天阁

Node/Koa2[60]:取消关注接口处理

· Yin灏

取消关注接口

src/routes/api/blog-profile.js

//...
const { unFollow } = require("../../controller/user-relation");
// 取消关注
router.post("/unFollow", loginCheck, async (ctx, next) => {
  const { id: myUserId } = ctx.session.userInfo;
  const { userId: curUserId } = ctx.request.body;
  // controller
  ctx.body = await follow(myUserId, curUserId);
});
//...

controller 层处理

src/controller/user-relation.js

//...
const { deleteFollower } = require("../services/user-relation");
async function follow(myUserId, curUserId) {
  // services 层
  const result = await deleteFollower(myUserId, curUserId);
  if (result) {
    return new SuccessModel();
  }
  return new ErrorModel(deleteFollowerFailInfo);
}
//...

services 层

src/services/user-relation.js

async function deleteFollower(userId, followerId) {
  const result = await UserRelation.destroy({
    where: {
      userId,
      followerId,
    },
  });
  return result > 0;
}

- Book Lists -