Skip to content

Commit b0ce84b

Browse files
Agnes Linagnes512
authored andcommitted
feat(cli): update controller template to enable filter for findById endpoint
1 parent 0f96f2b commit b0ce84b

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

docs/site/Controller-generator.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,19 @@ export class TodoController {
202202
responses: {
203203
'200': {
204204
description: 'Todo model instance',
205-
content: {'application/json': {schema: getModelSchemaRef(Todo)}},
205+
content: {
206+
'application/json': {
207+
schema: getModelSchemaRef(Todo, {includeRelations: true}),
208+
},
209+
},
206210
},
207211
},
208212
})
209-
async findById(@param.path.number('id') id: number): Promise<Todo> {
210-
return this.todoRepository.findById(id);
213+
async findById(
214+
@param.path.number('id') id: number,
215+
@param.query.object('filter', getFilterSchemaFor(Todo)) filter?: Filter<Todo>
216+
): Promise<Todo> {
217+
return this.todoRepository.findById(id, filter);
211218
}
212219

213220
@patch('/todos/{id}', {

docs/site/Sequence.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,25 @@ from the path object.
231231
responses: {
232232
'200': {
233233
description: 'Note model instance',
234-
content: {'application/json': {schema: getModelSchemaRef(Note)}},
234+
content: {
235+
'application/json': {
236+
schema: getModelSchemaRef(Note, {includeRelations: true}),
237+
},
238+
},
235239
},
236240
},
237241
})
238-
async findById(@param.path.string('id') id: string): Promise<Note> {
239-
return this.noteRepository.findById(id);
242+
async findById(
243+
@param.path.string('id') id: string,
244+
@param.query.object('filter', getFilterSchemaFor(Note)) filter?: Filter<Note>
245+
): Promise<Note> {
246+
return this.noteRepository.findById(id, filter);
240247
}
241248
```
242249

250+
(Notice: the filter for `findById()` method only supports the `include` clause
251+
for now.)
252+
243253
You can also specify a parameter which is an object value encoded as a JSON
244254
string or in multiple nested keys. For a JSON string, a sample value would be
245255
`location={"lang": 23.414, "lat": -98.1515}`. For the same `location` object, it
@@ -378,7 +388,7 @@ details.
378388
```
379389

380390
During development and testing, it may be useful to see all error details in the
381-
HTTP responsed returned by the server. This behavior can be enabled by enabling
391+
HTTP response returned by the server. This behavior can be enabled by enabling
382392
the `debug` flag in error-handler configuration as shown in the code example
383393
below. See strong-error-handler
384394
[docs](https://github.com/strongloop/strong-error-handler#options) for a list of

packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export class <%= className %>Controller {
7070
description: 'Array of <%= modelName %> model instances',
7171
content: {
7272
'application/json': {
73-
schema: {type: 'array', items: getModelSchemaRef(<%= modelName %>)},
73+
schema: {
74+
type: 'array',
75+
items: getModelSchemaRef(<%= modelName %>, {includeRelations: true}),
76+
},
7477
},
7578
},
7679
},
@@ -108,12 +111,19 @@ export class <%= className %>Controller {
108111
responses: {
109112
'200': {
110113
description: '<%= modelName %> model instance',
111-
content: {'application/json': {schema: getModelSchemaRef(<%= modelName %>)}},
114+
content: {
115+
'application/json': {
116+
schema: getModelSchemaRef(<%= modelName %>, {includeRelations: true}),
117+
},
118+
},
112119
},
113120
},
114121
})
115-
async findById(@param.path.<%= idType %>('id') id: <%= idType %>): Promise<<%= modelName %>> {
116-
return this.<%= repositoryNameCamel %>.findById(id);
122+
async findById(
123+
@param.path.<%= idType %>('id') id: <%= idType %>,
124+
@param.query.object('filter', getFilterSchemaFor(<%= modelName %>)) filter?: Filter<<%= modelName %>>
125+
): Promise<<%= modelName %>> {
126+
return this.<%= repositoryNameCamel %>.findById(id, filter);
117127
}
118128

119129
@patch('<%= httpPathName %>/{id}', {

packages/cli/snapshots/integration/generators/controller.integration.snapshots.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ export class ProductReviewController {
8080
description: 'Array of ProductReview model instances',
8181
content: {
8282
'application/json': {
83-
schema: {type: 'array', items: getModelSchemaRef(ProductReview)},
83+
schema: {
84+
type: 'array',
85+
items: getModelSchemaRef(ProductReview, {includeRelations: true}),
86+
},
8487
},
8588
},
8689
},
@@ -118,12 +121,19 @@ export class ProductReviewController {
118121
responses: {
119122
'200': {
120123
description: 'ProductReview model instance',
121-
content: {'application/json': {schema: getModelSchemaRef(ProductReview)}},
124+
content: {
125+
'application/json': {
126+
schema: getModelSchemaRef(ProductReview, {includeRelations: true}),
127+
},
128+
},
122129
},
123130
},
124131
})
125-
async findById(@param.path.number('id') id: number): Promise<ProductReview> {
126-
return this.barRepository.findById(id);
132+
async findById(
133+
@param.path.number('id') id: number,
134+
@param.query.object('filter', getFilterSchemaFor(ProductReview)) filter?: Filter<ProductReview>
135+
): Promise<ProductReview> {
136+
return this.barRepository.findById(id, filter);
127137
}
128138
129139
@patch('/product-reviews/{id}', {
@@ -249,7 +259,10 @@ export class ProductReviewController {
249259
description: 'Array of ProductReview model instances',
250260
content: {
251261
'application/json': {
252-
schema: {type: 'array', items: getModelSchemaRef(ProductReview)},
262+
schema: {
263+
type: 'array',
264+
items: getModelSchemaRef(ProductReview, {includeRelations: true}),
265+
},
253266
},
254267
},
255268
},
@@ -287,12 +300,19 @@ export class ProductReviewController {
287300
responses: {
288301
'200': {
289302
description: 'ProductReview model instance',
290-
content: {'application/json': {schema: getModelSchemaRef(ProductReview)}},
303+
content: {
304+
'application/json': {
305+
schema: getModelSchemaRef(ProductReview, {includeRelations: true}),
306+
},
307+
},
291308
},
292309
},
293310
})
294-
async findById(@param.path.number('id') id: number): Promise<ProductReview> {
295-
return this.barRepository.findById(id);
311+
async findById(
312+
@param.path.number('id') id: number,
313+
@param.query.object('filter', getFilterSchemaFor(ProductReview)) filter?: Filter<ProductReview>
314+
): Promise<ProductReview> {
315+
return this.barRepository.findById(id, filter);
296316
}
297317
298318
@patch('/product-reviews/{id}', {

0 commit comments

Comments
 (0)