评论接口设计思路
发布评论接口
① 创建新的comment评论表
CREATE TABLE IF NOT EXISTS `comment`( id INT PRIMARY KEY AUTO_INCREMENT, content VARCHAR(1000) NOT NULL, moment_id INT NOT NULL, user_id INT NOT NULL, comment_id INT DEFAULT NULL, createAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updateAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY(moment_id) REFERENCES moment(id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(comment_id) REFERENCES comment(id) ON DELETE CASCADE ON UPDATE CASCADE );
② 定义路由接口–>验证用户登录
const Router = require('koa-router') const { create, } = require('../controller/comment_controller') const { verifyAuth, } = require ('../middleware/auth_middleware') const commentRouter = new Router({prefix:'/comment'}) // 发表评论 commentRouter.post('/',verifyAuth,create) module.exports = commentRouter
③ Controller和Service中处理内容
修改评论接口
① 定义理由接口
② 验证用户登录(更新verifyPermission)
见上面(verifyAuth)
③ 验证用户权限
PS:这里原本是封装了verifyPermission中间件,现在稍作更改
定义更为符合RESTful风格的接口代码
④ Controller和Service中处理内容
回复评论接口
① 定义路由接口
② 验证用户登录
见上面(verifyAuth)
③ Controller和Service中处理内容
删除评论接口
① 定义路由接口
② 验证用户登录和验证用户权限
③ Controller和Service中处理内容
查询动态时,同时显示评论信息
① 查询多个动态时,显示评论的个数
② 查询单个动态时,显示评论的列表
PS:
附上完整的路由接口代码。
const Router = require('koa-router') const { create, reply, update, remove, list } = require('../controller/comment_controller') const { verifyAuth, verifyPermission } = require ('../middleware/auth_middleware') const commentRouter = new Router({prefix:'/comment'}) // 发表评论 commentRouter.post('/',verifyAuth,create) // 回复评论 commentRouter.post('/:commentId/reply',verifyAuth,reply) // 修改评论 commentRouter.patch('/:commentId',verifyAuth,verifyPermission,update) // 删除评论 commentRouter.delete('/:commentId', verifyAuth, verifyPermission, remove); // 获取评论列表 commentRouter.get('/',list) module.exports = commentRouter