灏天阁

Node/Koa2[59]: 关注接口处理

· Yin灏

关注接口

src/routes/api/blog-profile.js

//...
const { follow } = require("../../controller/user-relation");
// 关注
router.post("/follow", 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 { addFollower } = require("../services/user-relation");
async function follow(myUserId, curUserId) {
  // services 层
  try {
    await addFollower(myUserId, curUserId);
    return new SuccessModel();
  } catch (ex) {
    return new ErrorModel(addFollowerFailInfo);
  }
}
//...

services 层

src/services/user-relation.js

async function addFollower(userId, followerId) {
  const result = await UserRelation.create({
    userId,
    followerId,
  });
  return result.dataValues;
}

- Book Lists -