-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
controller-relation-template-has-many.ts.ejs
110 lines (105 loc) · 3.8 KB
/
controller-relation-template-has-many.ts.ejs
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
import {
Count,
CountSchema,
Filter,
repository,
Where,
} from '@loopback/repository';
import {
del,
get,
getModelSchemaRef,
getWhereSchemaFor,
param,
patch,
post,
requestBody,
} from '@loopback/rest';
import {
<%= sourceModelClassName %>,
<%= targetModelClassName %>,
} from '../models';
import {<%= sourceRepositoryClassName %>} from '../repositories';
export class <%= controllerClassName %> {
constructor(
@repository(<%= sourceRepositoryClassName %>) protected <%= paramSourceRepository %>: <%= sourceRepositoryClassName %>,
) { }
@get('/<%= sourceModelPath %>/{id}/<%= targetModelPath %>', {
responses: {
'200': {
description: 'Array of <%= sourceModelClassName %> has many <%= targetModelClassName %>',
content: {
'application/json': {
schema: {type: 'array', items: getModelSchemaRef(<%= targetModelClassName %>)},
},
},
},
},
})
async find(
@param.path.<%= sourceModelPrimaryKeyType %>('id') id: <%= sourceModelPrimaryKeyType %>,
@param.query.object('filter') filter?: Filter<<%= targetModelClassName %>>,
): Promise<<%= targetModelClassName %>[]> {
return this.<%= paramSourceRepository %>.<%= relationPropertyName %>(id).find(filter);
}
@post('/<%= sourceModelPath %>/{id}/<%= targetModelPath %>', {
responses: {
'200': {
description: '<%= sourceModelClassName %> model instance',
content: {'application/json': {schema: getModelSchemaRef(<%= targetModelClassName %>)}},
},
},
})
async create(
@param.path.<%= sourceModelPrimaryKeyType %>('id') id: typeof <%= sourceModelClassName %>.prototype.<%= sourceModelPrimaryKey %>,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(<%= targetModelClassName %>, {
title: 'New<%= targetModelClassName %>In<%= sourceModelClassName %>',
exclude: ['<%= targetModelPrimaryKey %>'],
optional: ['<%= foreignKeyName %>']
}),
},
},
}) <%= targetModelRequestBody %>: Omit<<%= targetModelClassName %>, '<%= targetModelPrimaryKey %>'>,
): Promise<<%= targetModelClassName %>> {
return this.<%= paramSourceRepository %>.<%= relationPropertyName %>(id).create(<%= targetModelRequestBody %>);
}
@patch('/<%= sourceModelPath %>/{id}/<%= targetModelPath %>', {
responses: {
'200': {
description: '<%= sourceModelClassName %>.<%= targetModelClassName %> PATCH success count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async patch(
@param.path.<%= sourceModelPrimaryKeyType %>('id') id: <%= sourceModelPrimaryKeyType %>,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(<%= targetModelClassName %>, {partial: true}),
},
},
})
<%= targetModelRequestBody %>: Partial<<%= targetModelClassName %>>,
@param.query.object('where', getWhereSchemaFor(<%= targetModelClassName %>)) where?: Where<<%= targetModelClassName %>>,
): Promise<Count> {
return this.<%= paramSourceRepository %>.<%= relationPropertyName %>(id).patch(<%= targetModelRequestBody %>, where);
}
@del('/<%= sourceModelPath %>/{id}/<%= targetModelPath %>', {
responses: {
'200': {
description: '<%= sourceModelClassName %>.<%= targetModelClassName %> DELETE success count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async delete(
@param.path.<%= sourceModelPrimaryKeyType %>('id') id: <%= sourceModelPrimaryKeyType %>,
@param.query.object('where', getWhereSchemaFor(<%= targetModelClassName %>)) where?: Where<<%= targetModelClassName %>>,
): Promise<Count> {
return this.<%= paramSourceRepository %>.<%= relationPropertyName %>(id).delete(where);
}
}