13-码路博客《回复模块》

7/25/2022

# 1. 创建回复

新建router/reply.js,代码如下:

// router/reply.js

const Router = require("@koa/router");
const ReplyController = require("../controller/ReplyController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");

const router = new Router();

// 创建回复
router.post("/reply", ReplyController.createReply);

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13

在app.js中,注册对应的路由,如下:

// app.js

// ...

// 路由的引入
const user = require("./router/user.js")
const admin = require("./router/admin.js")
const category = require("./router/category.js")
const reply = require("./router/reply.js")

// ...

// 注册回复模块路由
app.use(reply.routes());
reply.allowedMethods();

// ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

创建对应的控制器,如下:

// controller/ReplyController.js

const ReplyModel = require("../models/ReplyModel");
const CommentModel = require("../models/CommentModel");
const {
    replyValidator
} = require("../validators/reply");
const res = require("../core/helper");
class ReplyController {
    // 创建回复
    static async createReply(ctx, next) {
        replyValidator(ctx);
        const {
            comment_id
        } = ctx.request.body;
        const comment = await CommentModel.findById({
            _id: comment_id
        });
        if (!comment) {
            throw new global.errs.NotFound("没有找到相关评论");
        }
        const reply = await ReplyModel.create(ctx.request.body);
        // 要想调用get方法 必须将数据转成json
        reply.toJSON({
            getters: true
        });
        ctx.body = res.json(reply);
    }
}
module.exports = ReplyController;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

在postman中测试如下:

# 2. 获取所有回复列表

在router/reply.js,配置路由如下:

// router/reply.js

const Router = require("@koa/router");
const ReplyController = require("../controller/ReplyController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");

const router = new Router();

// 创建回复
router.post("/reply", ReplyController.createReply);

// 获取评论的回复列表
router.get("/reply", ReplyController.getReplyList);

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

在对应的控制器中,实现getReplyList方法,如下:

// controller/ReplyController.js

// 获取回复列表
static async getReplyList(ctx, next) {
    const comment_id = ctx.query.comment_id;
    let replyList = null;
    if (comment_id) {
        replyList = await ReplyModel.find({
            comment_id
        }).sort({
            _id: -1
        });
    } else {
        replyList = await ReplyModel.find().sort({
            _id: -1
        });
    }

    ctx.status = 200;
    ctx.body = res.json(replyList);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

在postman中测试如下:

# 3. 获取回复详情

在router/reply.js,配置路由如下:

// router/reply.js

const Router = require("@koa/router");
const ReplyController = require("../controller/ReplyController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");

const router = new Router();

// 创建回复
router.post("/reply", ReplyController.createReply);

// 获取评论的回复列表
router.get("/reply", ReplyController.getReplyList);

// 回复详情
router.get("/reply/:_id", ReplyController.getReplyDetailById);

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

在对应的控制器中,实现getReplyDetailById方法,如下:

// controller/ReplyController.js

// 获取该评论的回复详情
static async getReplyDetailById(ctx, next) {
    const _id = ctx.params._id;
    const replyDetail = await ReplyModel.findById({
        _id
    });
    if (!replyDetail) {
        throw new global.errs.NotFound("没有相关评论的回复详情信息");
    }
    ctx.body = res.json(replyDetail);
}
1
2
3
4
5
6
7
8
9
10
11
12
13

在postman中测试如下:

# 4. 更新单条回复

在router/reply.js,配置路由如下:

// router/reply.js

const Router = require("@koa/router");
const ReplyController = require("../controller/ReplyController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");

const router = new Router();

// 创建回复
router.post("/reply", ReplyController.createReply);

// 获取评论的回复列表
router.get("/reply", ReplyController.getReplyList);

// 回复详情
router.get("/reply/:_id", ReplyController.getReplyDetailById);

// 更新单个回复
router.put("/reply/:_id", jwtAuth({
    secret: config.security.secretKey
}), ReplyController.updateReplyById);

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

在对应的控制器中,实现updateReplyById方法,如下:

// controller/ReplyController.js

// 更新单条回复
static async updateReplyById(ctx, next) {
    const _id = ctx.params._id;
    const reply = await ReplyModel.findByIdAndUpdate({
        _id
    }, ctx.request.body);
    if (!reply) {
        throw new global.errs.NotFound("没有找到相关评论信息");
    }
    ctx.status = 200;
    ctx.body = res.success("更新成功");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

在postman中测试如下:

# 5. 删除回复

在router/reply.js,配置路由如下:

// router/reply.js

const Router = require("@koa/router");
const ReplyController = require("../controller/ReplyController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");

const router = new Router();

// 创建回复
router.post("/reply", ReplyController.createReply);

// 获取评论的回复列表
router.get("/reply", ReplyController.getReplyList);

// 回复详情
router.get("/reply/:_id", ReplyController.getReplyDetailById);

// 更新单个回复
router.put("/reply/:_id", jwtAuth({
    secret: config.security.secretKey
}), ReplyController.updateReplyById);

// 删除回复
router.delete("/reply/:_id", jwtAuth({
    secret: config.security.secretKey
}), ReplyController.deleteReplyById);

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

在对应的控制器中,实现deleteReplyById方法,如下:

// controller/ReplyController.js

// 删除某条回复
static async deleteReplyById(ctx, next) {
    const _id = ctx.params._id;
    const reply = await ReplyModel.findByIdAndDelete({
        _id
    });
    if (!reply) {
        throw new global.errs.NotFound("没有找到相关评论信息");
    }
    ctx.status = 200;
    ctx.body = res.success("删除成功");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

在postman中测试如下:

Last Updated: 12/25/2022, 10:02:14 PM