Skip to content

Commit e3ef86b

Browse files
author
Kevin Delisle
committed
fix(cli): rework template to use inline param decoration
1 parent 9a6dd21 commit e3ef86b

File tree

2 files changed

+31
-78
lines changed

2 files changed

+31
-78
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,51 @@ import {<%= repositoryName %>} from '../repositories';
77
export class <%= name %>Controller {
88

99
constructor(
10-
@inject('repositories.<%= modelNameCamel %>')
10+
@inject('repositories.<%= repositoryName %>')
1111
public <%= repositoryNameCamel %> : <%= repositoryName %>,
1212
) {}
1313

1414
@post('/<%= modelNameCamel %>')
15-
@param.body('obj', <%= modelName %>)
16-
async create(obj: <%= modelName %>) : Promise<<%= modelName %>> {
15+
async create(@param.body('obj') obj: <%= modelName %>)
16+
: Promise<<%= modelName %>> {
1717
return await this.<%= repositoryNameCamel %>.create(obj);
1818
}
1919

2020
@get('/<%= modelNameCamel %>/count')
21-
@param.query.string('where')
22-
async count(where: Where) : Promise<number> {
21+
async count(@param.query.string('where') where: Where) : Promise<number> {
2322
return await this.<%= repositoryNameCamel %>.count(where);
2423
}
2524

2625
@get('/<%= modelNameCamel %>')
27-
@param.query.string('filter')
28-
async find(filter: Filter) : Promise<<%= modelName %>[]> {
29-
return await this.<%= repositoryNameCamel %>.find(filter);
26+
async find(@param.query.string('filter') filter: Filter)
27+
: Promise<<%= modelName %>[]> {
28+
return await this.<%= repositoryNameCamel %>.find(filter);
3029
}
3130

3231
@patch('/<%= modelNameCamel %>')
33-
@param.query.string('where')
34-
@param.body('obj', <%= modelName %>)
35-
async updateAll(where: Where, obj: <%= modelName %>) : Promise<number> {
36-
return await this.<%= repositoryNameCamel %>.updateAll(where, obj);
32+
async updateAll(@param.query.string('where') where: Where,
33+
@param.body('obj') obj: <%= modelName %>) : Promise<number> {
34+
return await this.<%= repositoryNameCamel %>.updateAll(where, obj);
3735
}
3836

3937
@del('/<%= modelNameCamel %>')
40-
@param.query.string('where')
41-
async deleteAll(where: Where) : Promise<number> {
38+
async deleteAll(@param.query.string('where') where: Where) : Promise<number> {
4239
return await this.<%= repositoryNameCamel %>.deleteAll(where);
4340
}
4441

4542
@get('/<%= modelNameCamel %>/{id}')
46-
async findById(id: <%= idType %>) : Promise<<%= modelName %>> {
43+
async findById(@param.path.number('id') id: <%= idType %>) : Promise<<%= modelName %>> {
4744
return await this.<%= repositoryNameCamel %>.findById(id);
4845
}
4946

5047
@patch('/<%= modelNameCamel %>/{id}')
51-
@param.body('obj', <%= modelName %>)
52-
async updateById(id: <%= idType %>, obj: <%= modelName %>) : Promise<boolean> {
48+
async updateById(@param.path.number('id') id: <%= idType %>, @param.body('obj')
49+
obj: <%= modelName %>) : Promise<boolean> {
5350
return await this.<%= repositoryNameCamel %>.updateById(id, obj);
5451
}
5552

5653
@del('/<%= modelNameCamel %>/{id}')
57-
async deleteById(id: <%= idType %>) : Promise<boolean> {
54+
async deleteById(@param.path.number('id') id: <%= idType %>) : Promise<boolean> {
5855
return await this.<%= repositoryNameCamel %>.deleteById(id);
5956
}
6057
}

packages/cli/test/controller.js

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -270,104 +270,60 @@ describe('lb4 controller', () => {
270270

271271
/**
272272
* Assertions against the template to determine if it contains the
273-
* required signatures for a CRUD controller.
274-
* @param {String} tmpDir The temporary directory path to check. Uses
275-
* withInputName to determine the file name.
273+
* required signatures for a REST CRUD controller, specifically to ensure
274+
* that decorators are grouped correctly (for their corresponding
275+
* target functions)
276+
*
277+
* This function calls checkCrudContents.
278+
* @param {String} tmpDir
276279
*/
277-
function checkCrudContents(tmpDir) {
280+
function checkRestCrudContents(tmpDir) {
278281
assert.fileContent(tmpDir + withInputName, /class FooBarController/);
279282

280283
// Repository and injection
281284
assert.fileContent(
282285
tmpDir + withInputName,
283-
/\@inject\('repositories.foo'\)/
286+
/\@inject\('repositories.BarRepository'\)/
284287
);
285288
assert.fileContent(
286289
tmpDir + withInputName,
287290
/barRepository \: BarRepository/
288291
);
289-
// Check function signatures
290-
assert.fileContent(
291-
tmpDir + withInputName,
292-
/async create\(obj\: Foo\) \: Promise\<Foo\>/
293-
);
294-
assert.fileContent(
295-
tmpDir + withInputName,
296-
/async count\(where\: Where\) \: Promise\<number\>/
297-
);
298-
assert.fileContent(
299-
tmpDir + withInputName,
300-
/async find\(filter\: Filter\) \: Promise\<Foo\[\]\>/
301-
);
302-
assert.fileContent(
303-
tmpDir + withInputName,
304-
/async updateAll\(where\: Where, obj\: Foo\) \: Promise\<number\>/
305-
);
306-
assert.fileContent(
307-
tmpDir + withInputName,
308-
/async deleteAll\(where\: Where\) \: Promise\<number\>/
309-
);
310-
assert.fileContent(
311-
tmpDir + withInputName,
312-
/async findById\(id\: number\) \: Promise\<Foo\>/
313-
);
314-
assert.fileContent(
315-
tmpDir + withInputName,
316-
/async updateById\(id\: number, obj\: Foo\) \: Promise\<boolean\>/
317-
);
318-
assert.fileContent(
319-
tmpDir + withInputName,
320-
/async deleteById\(id\: number\) \: Promise\<boolean\>/,
321-
''
322-
);
323-
}
324-
325-
/**
326-
* Assertions against the template to determine if it contains the
327-
* required signatures for a REST CRUD controller, specifically to ensure
328-
* that decorators are grouped correctly (for their corresponding
329-
* target functions)
330-
*
331-
* This function calls checkCrudContents.
332-
* @param {String} tmpDir
333-
*/
334-
function checkRestCrudContents(tmpDir) {
335-
checkCrudContents(tmpDir);
336292

337293
// Assert that the decorators are present in the correct groupings!
338294
assert.fileContent(
339295
tmpDir + withInputName,
340-
/\@post\('\/foo'\)\s{1,}\@param.body\('obj', Foo\)\s{1,}async create/
296+
/\@post\('\/foo'\)\s{1,}async create\(\@param.body\('obj'\)/
341297
);
342298

343299
assert.fileContent(
344300
tmpDir + withInputName,
345-
/\@get\('\/foo\/count'\)\s{1,}\@param.query.string\('where'\)\s{1,}async count/
301+
/\@get\('\/foo\/count'\)\s{1,}async count\(\@param.query.string\('where'\)/
346302
);
347303

348304
assert.fileContent(
349305
tmpDir + withInputName,
350-
/\@get\('\/foo'\)\s{1,}\@param.query.string\('filter'\)\s{1,}async find/
306+
/\@get\('\/foo'\)\s{1,}async find\(\@param.query.string\('filter'\)/
351307
);
352308
assert.fileContent(
353309
tmpDir + withInputName,
354-
/\@patch\('\/foo'\)\s{1,}\@param.query.string\('where'\)\s{1,}\@param.body\('obj', Foo\)\s{1,}async updateAll/
310+
/\@patch\('\/foo'\)\s{1,}async updateAll\(\@param.query.string\('where'\) where: Where,\s{1,}\@param.body\('obj'\)/
355311
);
356312
assert.fileContent(
357313
tmpDir + withInputName,
358-
/\@del\('\/foo'\)\s{1,}\@param.query.string\('where'\)\s{1,}async deleteAll/
314+
/\@del\('\/foo'\)\s{1,}async deleteAll\(\@param.query.string\('where'\)/
359315
);
360316
assert.fileContent(
361317
tmpDir + withInputName,
362-
/\@get\('\/foo\/{id}'\)\s{1,}async findById/
318+
/\@get\('\/foo\/{id}'\)\s{1,}async findById\(\@param.path.number\('id'\)/
363319
);
364320
assert.fileContent(
365321
tmpDir + withInputName,
366-
/\@patch\('\/foo\/{id}'\)\s{1,}\@param.body\('obj', Foo\)\s{1,}async updateById/
322+
/\@patch\('\/foo\/{id}'\)\s{1,}async updateById\(\@param.path.number\('id'\) id: number, \@param.body\('obj'\)/
367323
);
368324
assert.fileContent(
369325
tmpDir + withInputName,
370-
/\@del\('\/foo\/{id}'\)\s{1,}async deleteById/
326+
/\@del\('\/foo\/{id}'\)\s{1,}async deleteById\(\@param.path.number\('id'\) id: number\)/
371327
);
372328
}
373329
});

0 commit comments

Comments
 (0)