Skip to content

Commit 527ea42

Browse files
egg-redis
1 parent 8150b21 commit 527ea42

File tree

9 files changed

+39
-74
lines changed

9 files changed

+39
-74
lines changed

17-nodejs/02-egg/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ prettier
5151

5252
sequelize
5353
https://sequelize.org/master/manual/query-interface.html
54-
https://sequelize.org/master/variable/index.html#static-variable-DataTypes
54+
https://sequelize.org/master/variable/index.html#static-variable-DataTypes
55+
56+
validate
57+
https://github.com/node-modules/parameter#rule
Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,36 @@
11
import { Controller } from 'egg';
22

3-
// 定义创建接口的请求参数规则
4-
// https://github.com/node-modules/parameter#rule
5-
const createRule = {
6-
name: 'string',
7-
type: { type: 'enum', values: [ 'ask', 'share' ], required: false },
8-
};
9-
103
export default class DemoController extends Controller {
11-
// GET /demo
4+
5+
// GET /
126
public async index() {
137
const { ctx } = this;
14-
ctx.logger.debug('test logger');
8+
ctx.response.redirect('/public/test.html');
9+
}
10+
11+
// GET /api/demo
12+
public async testEnv() {
13+
const { ctx } = this;
1514
ctx.body = `<h1>${ctx.app.env}</h1>`; // local
1615
ctx.response.type = 'text/html';
1716
ctx.status = 200;
18-
19-
// ctx.app.redis.get()
20-
// client.on('connect', function () {
21-
// // set 语法
22-
// client.set('lubanH5makerTest', 'csxiaoyao', function (err, data) {
23-
// console.log(1, data)
24-
// })
25-
// // get 语法
26-
// client.get('lubanH5makerTest', function (err, data) {
27-
// console.log(2, data)
28-
// })
29-
// })
3017
}
3118

32-
// GET /demo/new
33-
public async new() {
19+
// GET /api/demo/:id
20+
public async testThrowError() {
3421
const { app, ctx } = this;
22+
ctx.logger.debug('testThrowError with id: %j', ctx.params.id);
3523
ctx.throw({
3624
code: app.config.CODE.TEST_ERROR,
3725
message: '测试错误抛出',
3826
});
3927
}
4028

41-
// POST /demo
42-
public async create() {
43-
const { ctx } = this;
44-
// 校验 `ctx.request.body` 是否符合我们预期的格式
45-
// 如果参数校验未通过,将会抛出一个 status = 422 的异常
46-
ctx.validate(createRule, ctx.request.body);
47-
// 调用 service 创建一个 demo
48-
const data = await ctx.service.demo.create(ctx.request.body);
49-
// 设置响应体和状态码
50-
ctx.helper.rest({
51-
...data,
52-
}, 'ok', 0);
53-
}
54-
// GET /demo/:id
55-
public async show() {
56-
const { ctx } = this;
57-
ctx.logger.debug('fetch id: %j', ctx.params.id);
58-
console.log('test show');
59-
}
60-
// GET /demo/:id/edit edit_demo
61-
public async edit() {
62-
console.log('test edit');
63-
}
64-
// PUT /demo/:id demo
65-
public async update() {
66-
console.log('test update');
67-
}
68-
// DELETE /demo/:id demo
69-
public async destroy() {
70-
console.log('test destroy');
29+
// POST /api/demo
30+
public async testRedis() {
31+
const { app, ctx } = this;
32+
await app.redis.set('test', ctx.request.body.test);
33+
const data = await app.redis.get('test');
34+
ctx.helper.rest(data);
7135
}
7236
}

17-nodejs/02-egg/app/middleware/errorHandler.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export default () => {
3232

3333
// 404 单独处理
3434
if (ctx.status === 404 && !ctx.body) {
35-
console.log(ctx.acceptJSON);
3635
if (ctx.acceptJSON) {
3736
ctx.body = { error: 'Not Found' };
3837
} else {

17-nodejs/02-egg/app/model/post.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// import { Application } from 'egg';
2-
// export default function(app: Application) {
3-
export default function(app) {
1+
import { Application } from 'egg';
2+
export default function(app: Application) {
43
const { STRING, INTEGER } = app.Sequelize;
54

65
const Post = app.model.define('posts', {
@@ -33,7 +32,7 @@ export default function(app) {
3332
}
3433

3534
static async findByIdWithUser(id: number, userId: number) {
36-
return await this.findOne({
35+
return await app.model.Post.findOne({
3736
where: { id, user_id: userId },
3837
});
3938
}

17-nodejs/02-egg/app/model/user.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// import { Application } from 'egg';
2-
// export default function(app: Application) {
3-
export default function(app) {
1+
import { Application } from 'egg';
2+
export default function(app: Application) {
43
const { STRING, BIGINT, INTEGER } = app.Sequelize;
54

65
const User = app.model.define('users', {

17-nodejs/02-egg/app/router.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ export default (app: Application) => {
44
const { controller, router } = app;
55

66
router.get('/', controller.demo.index);
7-
router.resources('demo', '/api/demo', controller.demo);
7+
router.get('/api/demo', controller.demo.testEnv);
8+
router.get('/api/demo/:id', controller.demo.testThrowError);
9+
router.post('/api/demo', controller.demo.testRedis);
10+
811
router.resources('users', '/api/users', controller.user);
912
router.resources('posts', '/api/posts', controller.post);
1013
};

17-nodejs/02-egg/app/service/demo.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

17-nodejs/02-egg/typings/app/service/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ type AnyClass = new (...args: any[]) => any;
66
type AnyFunc<T = any> = (...args: any[]) => T;
77
type CanExportFunc = AnyFunc<Promise<any>> | AnyFunc<IterableIterator<any>>;
88
type AutoInstanceType<T, U = T extends CanExportFunc ? T : T extends AnyFunc ? ReturnType<T> : T> = U extends AnyClass ? InstanceType<U> : U;
9-
import ExportDemo from '../../../app/service/demo';
109
import ExportPost from '../../../app/service/post';
1110
import ExportUser from '../../../app/service/user';
1211

1312
declare module 'egg' {
1413
interface IService {
15-
demo: AutoInstanceType<typeof ExportDemo>;
1614
post: AutoInstanceType<typeof ExportPost>;
1715
user: AutoInstanceType<typeof ExportUser>;
1816
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import 'egg';
22

33
declare module 'egg' {
4-
4+
interface Application {
5+
redis: redis;
6+
}
7+
interface Application {
8+
model: model;
9+
}
10+
interface Application {
11+
Sequelize: Sequelize;
12+
}
513
}

0 commit comments

Comments
 (0)