-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
test-foo-bar.controller.ts
124 lines (97 loc) 路 3.18 KB
/
test-foo-bar.controller.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {
Context, Delete, Get, HttpResponseCreated, HttpResponseMethodNotAllowed,
HttpResponseNoContent, HttpResponseNotFound, HttpResponseOK, Patch, Post,
Put, ValidateBody, ValidateParams, ValidateQuery
} from '@foal/core';
import { getRepository } from 'typeorm';
import { TestFooBar } from '../entities';
const testFooBarSchema = {
additionalProperties: false,
properties: {
text: { type: 'string', maxLength: 255 },
},
required: [ 'text' ],
type: 'object',
};
export class TestFooBarController {
@Get('/')
@ValidateQuery({
properties: {
skip: { type: 'number' },
take: { type: 'number' },
},
type: 'object',
})
async get(ctx: Context) {
const testFooBars = await getRepository(TestFooBar).find({
skip: ctx.request.query.skip,
take: ctx.request.query.take
});
return new HttpResponseOK(testFooBars);
}
@Get('/:testFooBarId')
@ValidateParams({ properties: { testFooBarId: { type: 'number' } }, type: 'object' })
async getById(ctx: Context) {
const testFooBar = await getRepository(TestFooBar).findOne(ctx.request.params.testFooBarId);
if (!testFooBar) {
return new HttpResponseNotFound();
}
return new HttpResponseOK(testFooBar);
}
@Post('/')
@ValidateBody(testFooBarSchema)
async post(ctx: Context) {
const testFooBar = await getRepository(TestFooBar).save(ctx.request.body);
return new HttpResponseCreated(testFooBar);
}
@Post('/:testFooBarId')
postById() {
return new HttpResponseMethodNotAllowed();
}
@Patch('/')
patch() {
return new HttpResponseMethodNotAllowed();
}
@Patch('/:testFooBarId')
@ValidateParams({ properties: { testFooBarId: { type: 'number' } }, type: 'object' })
@ValidateBody({ ...testFooBarSchema, required: [] })
async patchById(ctx: Context) {
const testFooBar = await getRepository(TestFooBar).findOne(ctx.request.params.testFooBarId);
if (!testFooBar) {
return new HttpResponseNotFound();
}
Object.assign(testFooBar, ctx.request.body);
await getRepository(TestFooBar).save(testFooBar);
return new HttpResponseOK(testFooBar);
}
@Put('/')
put() {
return new HttpResponseMethodNotAllowed();
}
@Put('/:testFooBarId')
@ValidateParams({ properties: { testFooBarId: { type: 'number' } }, type: 'object' })
@ValidateBody(testFooBarSchema)
async putById(ctx: Context) {
const testFooBar = await getRepository(TestFooBar).findOne(ctx.request.params.testFooBarId);
if (!testFooBar) {
return new HttpResponseNotFound();
}
Object.assign(testFooBar, ctx.request.body);
await getRepository(TestFooBar).save(testFooBar);
return new HttpResponseOK(testFooBar);
}
@Delete('/')
delete() {
return new HttpResponseMethodNotAllowed();
}
@Delete('/:testFooBarId')
@ValidateParams({ properties: { testFooBarId: { type: 'number' } }, type: 'object' })
async deleteById(ctx: Context) {
const testFooBar = await getRepository(TestFooBar).findOne(ctx.request.params.testFooBarId);
if (!testFooBar) {
return new HttpResponseNotFound();
}
await getRepository(TestFooBar).delete(ctx.request.params.testFooBarId);
return new HttpResponseNoContent();
}
}