标签接口设计思路
思路:一个动态可以有多个标签,一个标签也可以对应多个动态
创建标签接口
① 创建标签表
CREATE TABLE IF NOT EXISTS `label`( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10) NOT NULL UNIQUE, createAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updateAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
② 定义路由接口–>验证用户登录
const Router = require ('koa-router') const { create, } = require('../controller/label_contrller') const { verifyAuth } = require('../middleware/auth_middleware') const labelRouter = new Router ({prefix: '/label'}) // 创建标签 labelRouter.post('/',verifyAuth,create) module.exports = labelRouter
③ Controller和Service中处理内容
标签与动态关系
① 创建标签和动态关系表
CREATE TABLE IF NOT EXISTS `moment_label`( moment_id INT NOT NULL, label_id INT NOT NULL, createAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updateAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY(moment_id, label_id), FOREIGN KEY (moment_id) REFERENCES moment(id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (label_id) REFERENCES label(id) ON DELETE CASCADE ON UPDATE CASCADE );
② 给动态添加标签接口
1. 给动态添加新标签的接口
ps: 补充完整的动态路由代码
const Router = require('koa-router') const { create, detail, list, update, remove, addLabels, fileInfo } = require('../controller/moment_controller') const { verifyAuth, verifyPermission } = require('../middleware/auth_middleware') const { verifyLabelExists } = require('../middleware/label_middleware') const momentRouter = new Router({prefix: '/moment'}) // 创建动态 momentRouter.post('/',verifyAuth,create) // 获取动态信息 momentRouter.get('/',list) momentRouter.get('/:momentId',detail) // 修改动态接口 // 条件:1.用户必须登录 2. 用户具备权限 momentRouter.patch('/:momentId',verifyAuth,verifyPermission,update) momentRouter.delete('/:momentId',verifyAuth,verifyPermission,remove) // 给动态添加标签 momentRouter.post('/:momentId/labels', verifyAuth, verifyPermission, verifyLabelExists, addLabels); // 动态配图 momentRouter.get('/images/:filename',fileInfo) module.exports = momentRouter
2. 给动态内容绑定标签
查询标签接口
① 路由接口
② 查询动态列表,展示标签数量
③查询动态详情,展示标签列表