10-码路博客《分类模块》
码路教育 7/25/2022
# 1. 创建分类
新建router/category.js,代码如下:
// router/category.js
const Router = require("@koa/router");
const CategoryController = require("../controller/CategoryController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");
const router = new Router();
// 创建分类
router.post("/category", jwtAuth({
secret: config.security.secretKey
}), CategoryController.create);
module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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")
// ...
// 注册分类模块路由
app.use(category.routes())
category.allowedMethods();
// ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
创建对应的控制器,如下:
// controller/CategoryController.js
const {
categoryValidator
} = require("../validators/category.js");
const CategoryModel = require("../models/CategoryModel");
const res = require("../core/helper");
class CategoryController {
static async create(ctx, next) {
categoryValidator(ctx);
const {
name,
keyword
} = ctx.request.body;
const hasCategory = await CategoryModel.findOne({
name
});
if (hasCategory) throw new global.errs.Existing("分类名已存在");
await CategoryModel.create({
name,
keyword
});
ctx.body = res.success("创建分类成功");
}
}
module.exports = CategoryController;
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
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
在postman中测试如下:
# 2. 获取所有分类
在router/category.js,配置对应路由:
// router/category.js
const Router = require("@koa/router");
const CategoryController = require("../controller/CategoryController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");
const router = new Router();
// 创建分类
router.post("/category", jwtAuth({
secret: config.security.secretKey
}), CategoryController.create);
// 获取分类列表
router.get("/category", jwtAuth({
secret: config.security.secretKey
}), CategoryController.getCategoryList);
module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
在对应的控制器中,实现getCategoryList方法,如下:
// controller/CategoryController.js
static async getCategoryList(ctx, next) {
const {
pageIndex = 1, pageSize = 10
} = ctx.query;
// 获取整个分类的总数
const totalSize = await CategoryModel.find().countDocuments();
console.log(typeof pageIndex);
const categoryList = await CategoryModel.find()
.skip(parseInt(pageIndex - 1) * parseInt(pageSize))
.limit(parseInt(pageSize))
.sort({
_id: -1
});
ctx.body = res.json({
content: categoryList,
totalSize,
pageIndex: parseInt(pageIndex),
pageSize: parseInt(pageSize),
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
在postman中测试如下:
# 3. 更新分类
在router/category.js,配置对应路由:
// router/category.js
const Router = require("@koa/router");
const CategoryController = require("../controller/CategoryController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");
const router = new Router();
// 创建分类
router.post("/category", jwtAuth({
secret: config.security.secretKey
}), CategoryController.create);
// 获取分类列表
router.get("/category", jwtAuth({
secret: config.security.secretKey
}), CategoryController.getCategoryList);
// 更新分类
router.put("/category/:_id", jwtAuth({
secret: config.security.secretKey
}), CategoryController.updateCategoryById);
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
在对应的控制器中,实现updateCategoryById方法,如下:
// controller/CategoryController.js
// 更新分类
static async updateCategoryById(ctx, next) {
const _id = ctx.params._id;
const {
name,
keyword
} = ctx.request.body;
const category = await CategoryModel.findByIdAndUpdate({
_id
}, {
name,
keyword
});
if (!category) throw new global.errs.NotFound("没有找到相关分类");
ctx.body = res.json("更新成功");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
在postman中测试如下:
# 4. 删除分类
在router/category.js,配置对应路由:
// router/category.js
const Router = require("@koa/router");
const CategoryController = require("../controller/CategoryController");
const jwtAuth = require("koa-jwt");
const config = require("../config/index.js");
const router = new Router();
// 创建分类
router.post("/category", jwtAuth({
secret: config.security.secretKey
}), CategoryController.create);
// 获取分类列表
router.get("/category", jwtAuth({
secret: config.security.secretKey
}), CategoryController.getCategoryList);
// 更新分类
router.put("/category/:_id", jwtAuth({
secret: config.security.secretKey
}), CategoryController.updateCategoryById);
// 删除分类
router.delete("/category/:_id", jwtAuth({
secret: config.security.secretKey
}), CategoryController.deleteCategoryById);
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
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
在对应的控制器中,实现deleteCategoryById方法,如下:
// controller/CategoryController.js
// 删除分类
static async deleteCategoryById(ctx, next) {
const _id = ctx.params._id;
const category = await CategoryModel.findByIdAndDelete({
_id
});
if (!category) throw new global.errs.NotFound("没有找到相关分类");
ctx.body = res.json("删除成功");
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
在postman中测试如下: