Skip to content

Commit 89231ff

Browse files
author
pedro
committed
feat:更新依赖和优化校验器
1 parent 374b9a7 commit 89231ff

29 files changed

Lines changed: 409 additions & 363 deletions

app/api/cms/admin.js

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22

33
const {
44
LinRouter,
5-
paginate,
65
routeMetaInfo,
76
adminRequired,
87
Success,
9-
ParametersException,
108
NotFound,
119
Failed
1210
} = require("lin-mizar");
1311

14-
const { has, set, get, toSafeInteger, isInteger } = require("lodash");
12+
const { has, set } = require("lodash");
13+
1514
const {
15+
DispatchAuthsValidator,
16+
RemoveAuthsValidator,
17+
UpdateGroupValidator,
1618
ResetPasswordValidator,
19+
AdminUsersValidator,
1720
UpdateUserInfoValidator,
1821
NewGroupValidator,
19-
UpdateGroupValidator,
20-
DispatchAuthValidator,
21-
DispatchAuthsValidator,
22-
RemoveAuthsValidator
23-
} = require("../../validators/cms");
24-
const { getSafeParamId } = require("../../libs/util");
22+
DispatchAuthValidator
23+
} = require("../../validators/admin");
24+
25+
const {
26+
PositiveIdValidator,
27+
PaginateValidator
28+
} = require("../../validators/common");
29+
2530
const { AdminDao } = require("../../dao/admin");
2631

2732
const admin = new LinRouter({
@@ -65,13 +70,12 @@ admin.linGet(
6570
},
6671
adminRequired,
6772
async ctx => {
68-
const groupId = get(ctx.request.query, "group_id");
69-
const { start, count } = paginate(ctx);
73+
const v = await new AdminUsersValidator().validate(ctx);
7074
const { users, total } = await adminDao.getUsers(
7175
ctx,
72-
groupId,
73-
start,
74-
count
76+
v.get("query.group_id"),
77+
v.get("query.start"),
78+
v.get("query.count")
7579
);
7680
ctx.json({
7781
collection: users,
@@ -92,13 +96,7 @@ admin.linPut(
9296
adminRequired,
9397
async ctx => {
9498
const v = await new ResetPasswordValidator().validate(ctx);
95-
const id = toSafeInteger(get(ctx.params, "id"));
96-
if (!isInteger(id)) {
97-
throw new ParametersException({
98-
msg: "路由参数错误"
99-
});
100-
}
101-
await adminDao.changeUserPassword(ctx, v, id);
99+
await adminDao.changeUserPassword(ctx, v);
102100
ctx.json(
103101
new Success({
104102
msg: "密码修改成功"
@@ -117,12 +115,8 @@ admin.linDelete(
117115
},
118116
adminRequired,
119117
async ctx => {
120-
const id = toSafeInteger(get(ctx.params, "id"));
121-
if (!isInteger(id)) {
122-
throw new ParametersException({
123-
msg: "路由参数错误"
124-
});
125-
}
118+
const v = await new PositiveIdValidator().validate(ctx);
119+
const id = v.get("path.id");
126120
await adminDao.deleteUser(ctx, id);
127121
ctx.json(
128122
new Success({
@@ -143,13 +137,7 @@ admin.linPut(
143137
adminRequired,
144138
async ctx => {
145139
const v = await new UpdateUserInfoValidator().validate(ctx);
146-
const id = toSafeInteger(get(ctx.params, "id"));
147-
if (!isInteger(id)) {
148-
throw new ParametersException({
149-
msg: "路由参数错误"
150-
});
151-
}
152-
await adminDao.updateUserInfo(ctx, v, id);
140+
await adminDao.updateUserInfo(ctx, v);
153141
ctx.json(
154142
new Success({
155143
msg: "操作成功"
@@ -168,8 +156,12 @@ admin.linGet(
168156
},
169157
adminRequired,
170158
async ctx => {
171-
const { start, count } = paginate(ctx);
172-
const { groups, total } = await adminDao.getGroups(ctx, start, count);
159+
const v = await new PaginateValidator().validate(ctx);
160+
const { groups, total } = await adminDao.getGroups(
161+
ctx,
162+
v.get("query.start"),
163+
v.get("query.count")
164+
);
173165
if (total < 1) {
174166
throw new NotFound({
175167
msg: "未找到任何权限组"
@@ -212,13 +204,8 @@ admin.linGet(
212204
},
213205
adminRequired,
214206
async ctx => {
215-
const id = toSafeInteger(get(ctx.params, "id"));
216-
if (!isInteger(id)) {
217-
throw new ParametersException({
218-
msg: "路由参数错误"
219-
});
220-
}
221-
const group = await adminDao.getGroup(ctx, id);
207+
const v = await new PositiveIdValidator().validate(ctx);
208+
const group = await adminDao.getGroup(ctx, v.get("path.id"));
222209
ctx.json(group);
223210
}
224211
);
@@ -262,8 +249,7 @@ admin.linPut(
262249
adminRequired,
263250
async ctx => {
264251
const v = await new UpdateGroupValidator().validate(ctx);
265-
const id = getSafeParamId(ctx);
266-
await adminDao.updateGroup(ctx, v, id);
252+
await adminDao.updateGroup(ctx, v);
267253
ctx.json(
268254
new Success({
269255
msg: "更新分组成功"
@@ -282,7 +268,8 @@ admin.linDelete(
282268
},
283269
adminRequired,
284270
async ctx => {
285-
const id = getSafeParamId(ctx);
271+
const v = await new PositiveIdValidator().validate(ctx);
272+
const id = v.get("path.id");
286273
await adminDao.deleteGroup(ctx, id);
287274
ctx.json(
288275
new Success({

app/api/cms/log.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

3-
const { LinRouter, groupRequired, NotFound, paginate } = require("lin-mizar");
4-
const { LogFindValidator } = require("../../validators/cms");
3+
const { LinRouter, groupRequired, NotFound } = require("lin-mizar");
4+
const { LogFindValidator } = require("../../validators/log");
5+
const { PaginateValidator } = require("../../validators/common");
56
const { get } = require("lodash");
67
const { LogDao } = require("../../dao/log");
78

@@ -24,8 +25,7 @@ log.linGet(
2425
groupRequired,
2526
async ctx => {
2627
const v = await new LogFindValidator().validate(ctx);
27-
const { start, count } = paginate(ctx);
28-
const { rows, total } = await logDao.getLogs(v, start, count);
28+
const { rows, total } = await logDao.getLogs(v);
2929
if (total < 1) {
3030
throw new NotFound({
3131
msg: "没有找到相关日志"
@@ -50,8 +50,7 @@ log.linGet(
5050
async ctx => {
5151
const v = await new LogFindValidator().validate(ctx);
5252
const keyword = get(ctx.request.query, "keyword", "");
53-
const { start, count } = paginate(ctx);
54-
const { logs, total } = await logDao.searchLogs(v, start, count, keyword);
53+
const { logs, total } = await logDao.searchLogs(v, keyword);
5554
if (total < 1) {
5655
throw new NotFound({
5756
msg: "没有找到相关日志"
@@ -74,8 +73,8 @@ log.linGet(
7473
},
7574
groupRequired,
7675
async ctx => {
77-
const { start, count } = paginate(ctx);
78-
const arr = await logDao.getUserNames(start, count);
76+
const v = await new PaginateValidator().validate(ctx);
77+
const arr = await logDao.getUserNames(v.get("query.start"), v.get("query.count"));
7978
ctx.json(arr);
8079
}
8180
);

app/api/cms/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
LoginValidator,
1616
UpdateInfoValidator,
1717
ChangePasswordValidator
18-
} = require("../../validators/cms");
18+
} = require("../../validators/user");
1919

2020
const { UserDao } = require("../../dao/user");
2121

app/api/v1/book.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ const { getSafeParamId } = require("../../libs/util");
55
const {
66
BookSearchValidator,
77
CreateOrUpdateBookValidator
8-
} = require("../../validators/cms");
8+
} = require("../../validators/book");
9+
10+
const {
11+
PositiveIdValidator
12+
} = require("../../validators/common");
13+
914
const { BookNotFound } = require("../../libs/err-code");
1015
const { Book } = require("../../models/book");
1116
const { BookDao } = require("../../dao/book");
@@ -23,7 +28,8 @@ exports.bookApi = bookApi;
2328
const bookDto = new BookDao();
2429

2530
bookApi.get("/:id", async ctx => {
26-
const id = getSafeParamId(ctx);
31+
const v = await new PositiveIdValidator().validate();
32+
const id = v.get("path.id");
2733
const book = await bookDto.getBook(id);
2834
if (!book) {
2935
throw new NotFound({
@@ -83,7 +89,8 @@ bookApi.linDelete(
8389
},
8490
groupRequired,
8591
async ctx => {
86-
const id = getSafeParamId(ctx);
92+
const v = await new PositiveIdValidator().validate();
93+
const id = v.get("path.id");
8794
await bookDto.deleteBook(id);
8895
ctx.json(
8996
new Success({

app/config/secure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
exports.db = {
4-
database: "lin-cms2",
4+
database: "lin-cms4",
55
host: "localhost",
66
port: 3306,
77
username: "root",

app/dao/admin.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class AdminDao {
5757
};
5858
}
5959

60-
async changeUserPassword (ctx, v, id) {
60+
async changeUserPassword (ctx, v) {
61+
const id = v.get("path.id");
6162
const user = await ctx.manager.userModel.findOne({
6263
where: {
6364
id: id,
@@ -89,7 +90,8 @@ class AdminDao {
8990
user.softDelete();
9091
}
9192

92-
async updateUserInfo (ctx, v, id) {
93+
async updateUserInfo (ctx, v) {
94+
const id = v.get("path.id");
9395
const user = await ctx.manager.userModel.findOne({
9496
where: {
9597
id: id,
@@ -212,7 +214,8 @@ class AdminDao {
212214
return true;
213215
}
214216

215-
async updateGroup (ctx, v, id) {
217+
async updateGroup (ctx, v) {
218+
const id = v.get("path.id");
216219
const exit = await ctx.manager.groupModel.findById(id);
217220
if (!exit) {
218221
throw new NotFound({

app/dao/log.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const { db } = require("lin-mizar/lin/db");
66
const Sequelize = require("sequelize");
77

88
class LogDao {
9-
async getLogs (v, start, count1) {
9+
async getLogs (v) {
10+
const start = v.get("query.start");
11+
const count1 = v.get("query.count");
1012
let condition = {};
1113
v.get("body.name") && set(condition, "user_name", v.get("body.name"));
1214
v.get("body.start") &&
@@ -26,7 +28,9 @@ class LogDao {
2628
};
2729
}
2830

29-
async searchLogs (v, start, count1, keyword) {
31+
async searchLogs (v, keyword) {
32+
const start = v.get("query.start");
33+
const count1 = v.get("query.count");
3034
let condition = {};
3135
v.get("body.name") && set(condition, "user_name", v.get("body.name"));
3236
v.get("body.start") &&

app/models/book.js

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@
22

33
const { InfoCrudMixin } = require("lin-mizar/lin/interface");
44
const { merge } = require("lodash");
5-
const Sequelize = require("sequelize");
5+
const { Sequelize, Model } = require("sequelize");
66
const { db } = require("lin-mizar/lin/db");
77

8-
let Book = db.define(
9-
"book",
10-
merge(
11-
{
12-
id: {
13-
type: Sequelize.INTEGER,
14-
primaryKey: true,
15-
autoIncrement: true
16-
},
17-
title: {
18-
type: Sequelize.STRING(50),
19-
allowNull: false
20-
},
21-
author: {
22-
type: Sequelize.STRING(30),
23-
allowNull: true,
24-
defaultValue: "未名"
25-
},
26-
summary: {
27-
type: Sequelize.STRING(1000),
28-
allowNull: true
29-
},
30-
image: {
31-
type: Sequelize.STRING(100),
32-
allowNull: true
33-
}
8+
class Book extends Model {}
9+
10+
Book.init(
11+
{
12+
id: {
13+
type: Sequelize.INTEGER,
14+
primaryKey: true,
15+
autoIncrement: true
16+
},
17+
title: {
18+
type: Sequelize.STRING(50),
19+
allowNull: false
3420
},
35-
InfoCrudMixin.attributes
36-
),
21+
author: {
22+
type: Sequelize.STRING(30),
23+
allowNull: true,
24+
defaultValue: "未名"
25+
},
26+
summary: {
27+
type: Sequelize.STRING(1000),
28+
allowNull: true
29+
},
30+
image: {
31+
type: Sequelize.STRING(100),
32+
allowNull: true
33+
}
34+
},
3735
merge(
3836
{
39-
tableName: "book"
37+
tableName: "book",
38+
modelName: "book",
39+
sequelize: db
4040
},
4141
InfoCrudMixin.options
4242
)
@@ -54,10 +54,4 @@ Book.prototype.toJSON = function () {
5454
return origin;
5555
};
5656

57-
Book.prototype.softDelete = function () {
58-
this.delete_time = new Date();
59-
// 更新数据库
60-
this.save();
61-
};
62-
6357
exports.Book = Book;

0 commit comments

Comments
 (0)