14-码路博客《广告模块》

7/25/2022

# 1. 创建⼴告

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

// router/advertise.js

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

const router = new Router();

// 创建广告
router.post("/advertise", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.createAdvertise);

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

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

// app.js

// ...

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

// ...

// 注册广告模块路由
app.use(advertise.routes());
advertise.allowedMethods();

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

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

// controller/AdvertiseController.js

const AdvertiseModel = require("../models/AdvertiseModel");
const {
    advertiseValidator
} = require("../validators/advertise");
const res = require("../core/helper");
class ReplyController {
    // 创建广告
    static async createAdvertise(ctx, next) {
        advertiseValidator(ctx);
        const {
            title
        } = ctx.request.body;
        let hasAdvertise = await AdvertiseModel.findOne({
            title
        });
        if (hasAdvertise) {
            throw new global.errs.Existing("广告已存在");
        }
        let advertise = await AdvertiseModel.create(ctx.request.body);
        ctx.body = res.json(advertise);
    }
}
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

在postman中测试如下:

# 2. 获取所有广告列表

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

// router/advertise.js

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

const router = new Router();

// 创建广告
router.post("/advertise", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.createAdvertise);

// 获取广告列表
router.get("/advertise", AdvertiseController.getAdvertiseList);

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

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

// controller/AdvertiseController.js

// 获取广告列表
static async getAdvertiseList(ctx, next) {
    let advertiseList = await AdvertiseModel.find();
    ctx.status = 200;
    ctx.body = res.json(advertiseList);
}
1
2
3
4
5
6
7
8

在postman中测试如下:

# 3. 获取广告详情

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

// router/advertise.js

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

const router = new Router();

// 创建广告
router.post("/advertise", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.createAdvertise);

// 获取广告列表
router.get("/advertise", AdvertiseController.getAdvertiseList);

// 获取广告详情
router.get("/advertise/:_id", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.getAdvertiseDetailById);

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

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

// controller/AdvertiseController.js

// 获取广告详情
static async getAdvertiseDetailById(ctx, next) {
    const _id = ctx.params._id;
    const advertiseDetail = await AdvertiseModel.findById({
        _id
    });
    if (!advertiseDetail) {
        throw new global.errs.NotFound("此广告不存在");
    }
    ctx.status = 200;
    ctx.body = res.json(advertiseDetail);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

在postman中测试如下:

# 4. 更新广告

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

// router/advertise.js

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

const router = new Router();

// 创建广告
router.post("/advertise", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.createAdvertise);

// 获取广告列表
router.get("/advertise", AdvertiseController.getAdvertiseList);

// 获取广告详情
router.get("/advertise/:_id", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.getAdvertiseDetailById);

// 更新广告
router.put("/advertise/:_id", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.updateAdvertiseById);

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

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

// controller/AdvertiseController.js

// 更新广告
static async updateAdvertiseById(ctx, next) {
    const _id = ctx.params._id;
    const advertiseDetail = await AdvertiseModel.findByIdAndUpdate({
            _id
        },
        ctx.request.body
    );
    if (!advertiseDetail) {
        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
15
16

在postman中测试如下:

# 5. 删除广告

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

// router/advertise.js

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

const router = new Router();

// 创建广告
router.post("/advertise", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.createAdvertise);

// 获取广告列表
router.get("/advertise", AdvertiseController.getAdvertiseList);

// 获取广告详情
router.get("/advertise/:_id", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.getAdvertiseDetailById);

// 更新广告
router.put("/advertise/:_id", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.updateAdvertiseById);

// 删除
router.delete("/advertise/:_id", jwtAuth({
    secret: config.security.secretKey
}), AdvertiseController.deleteAdvertiseById);

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
30
31
32
33

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

// controller/AdvertiseController.js

// 删除广告
static async deleteAdvertiseById(ctx, next) {
    const _id = ctx.params._id;
    const advertiseDetail = await AdvertiseModel.findByIdAndDelete({
        _id
    });
    if (!advertiseDetail) {
        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