Skip to content

Commit c7c6695

Browse files
committed
feat(cli): modify Controller templates to allow partial updates via PATCH
Signed-off-by: Miroslav Bajtoš <[email protected]>
1 parent 88df640 commit c7c6695

File tree

6 files changed

+60
-13
lines changed

6 files changed

+60
-13
lines changed

docs/site/Controller-generator.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,17 @@ export class TodoController {
169169
},
170170
})
171171
async updateAll(
172-
@requestBody() data: Todo,
172+
@requestBody({
173+
content: {
174+
'application/json': {
175+
schema: getModelSchemaRef(Todo, {partial: true}),
176+
},
177+
},
178+
})
179+
todo: Partial<Todo>
173180
@param.query.object('where', getWhereSchemaFor(Todo)) where?: Where<Todo>,
174181
): Promise<number> {
175-
return await this.todoRepository.updateAll(data, where);
182+
return await this.todoRepository.updateAll(todo, where);
176183
}
177184

178185
@get('/todos/{id}', {
@@ -196,9 +203,16 @@ export class TodoController {
196203
})
197204
async updateById(
198205
@param.path.number('id') id: number,
199-
@requestBody() data: Todo,
206+
@requestBody({
207+
content: {
208+
'application/json': {
209+
schema: getModelSchemaRef(Todo, {partial: true}),
210+
},
211+
},
212+
})
213+
todo: Partial<Todo>,
200214
): Promise<void> {
201-
await this.todoRepository.updateById(id, data);
215+
await this.todoRepository.updateById(id, todo);
202216
}
203217

204218
@del('/todos/{id}', {

docs/site/tutorials/todo-list/todo-list-tutorial-controller.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,14 @@ export class TodoListTodoController {
256256
})
257257
async patch(
258258
@param.path.number('id') id: number,
259-
@requestBody() todo: Partial<Todo>,
259+
@requestBody({
260+
content: {
261+
'application/json': {
262+
schema: getModelSchemaRef(Todo, {partial: true}),
263+
},
264+
},
265+
})
266+
todo: Partial<Todo>
260267
@param.query.object('where', getWhereSchemaFor(Todo)) where?: Where<Todo>,
261268
): Promise<Count> {
262269
return await this.todoListRepo.todos(id).patch(todo, where);

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
param,
1111
get,
1212
getFilterSchemaFor,
13+
getModelSchemaRef,
1314
getWhereSchemaFor,
1415
patch,
1516
put,
@@ -78,7 +79,14 @@ export class <%= className %>Controller {
7879
},
7980
})
8081
async updateAll(
81-
@requestBody() <%= modelVariableName %>: <%= modelName %>,
82+
@requestBody({
83+
content: {
84+
'application/json': {
85+
schema: getModelSchemaRef(<%= modelName %>, {partial: true}),
86+
},
87+
},
88+
})
89+
<%= modelVariableName %>: <%= modelName %>,
8290
@param.query.object('where', getWhereSchemaFor(<%= modelName %>)) where?: Where<<%= modelName %>>,
8391
): Promise<Count> {
8492
return await this.<%= repositoryNameCamel %>.updateAll(<%= modelVariableName %>, where);
@@ -105,7 +113,14 @@ export class <%= className %>Controller {
105113
})
106114
async updateById(
107115
@param.path.<%= idType %>('id') id: <%= idType %>,
108-
@requestBody() <%= modelVariableName %>: <%= modelName %>,
116+
@requestBody({
117+
content: {
118+
'application/json': {
119+
schema: getModelSchemaRef(<%= modelName %>, {partial: true}),
120+
},
121+
},
122+
})
123+
<%= modelVariableName %>: <%= modelName %>,
109124
): Promise<void> {
110125
await this.<%= repositoryNameCamel %>.updateById(id, <%= modelVariableName %>);
111126
}

packages/cli/generators/relation/templates/controller-relation-template-has-many.ts.ejs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {
99
del,
1010
get,
11+
getModelSchemaRef,
1112
getWhereSchemaFor,
1213
param,
1314
patch,
@@ -69,7 +70,14 @@ export class <%= controllerClassName %> {
6970
})
7071
async patch(
7172
@param.path.<%= sourceModelPrimaryKeyType %>('id') id: <%= sourceModelPrimaryKeyType %>,
72-
@requestBody() <%= targetModelRequestBody %>: Partial<<%= targetModelClassName %>>,
73+
@requestBody({
74+
content: {
75+
'application/json': {
76+
schema: getModelSchemaRef(<%= targetModelClassName %>, {partial: true}),
77+
},
78+
},
79+
})
80+
<%= targetModelRequestBody %>: Partial<<%= targetModelClassName %>>,
7381
@param.query.object('where', getWhereSchemaFor(<%= targetModelClassName %>)) where?: Where<<%= targetModelClassName %>>,
7482
): Promise<Count> {
7583
return await this.<%= paramSourceRepository %>.<%= relationPropertyName %>(id).patch(<%= targetModelRequestBody %>, where);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ function checkRestCrudContents() {
287287
/'200': {/,
288288
/description: 'ProductReview PATCH success count'/,
289289
/content: {'application\/json': {schema: CountSchema}},\s{1,}},\s{1,}},\s{1,}}\)/,
290-
/async updateAll\(\s{1,}\@requestBody\(\) productReview: ProductReview,\s{1,} @param\.query\.object\('where', getWhereSchemaFor\(ProductReview\)\) where\?: Where<ProductReview>(|,\s+)\)/,
290+
/async updateAll\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {partial: true}\),\s+},\s+},\s+}\)\s+productReview: ProductReview,\s{1,} @param\.query\.object\('where', getWhereSchemaFor\(ProductReview\)\) where\?: Where<ProductReview>(|,\s+)\)/,
291291
];
292292
patchUpdateAllRegEx.forEach(regex => {
293293
assert.fileContent(expectedFile, regex);
@@ -312,7 +312,7 @@ function checkRestCrudContents() {
312312
/responses: {/,
313313
/'204': {/,
314314
/description: 'ProductReview PATCH success'/,
315-
/async updateById\(\s{1,}\@param.path.number\('id'\) id: number,\s{1,}\@requestBody\(\) productReview: ProductReview,\s+\)/,
315+
/async updateById\(\s+\@param.path.number\('id'\) id: number,\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {partial: true}\),\s+},\s+},\s+}\)\s+productReview: ProductReview,\s+\)/,
316316
];
317317
patchUpdateByIdRegEx.forEach(regex => {
318318
assert.fileContent(expectedFile, regex);

packages/cli/test/integration/generators/hasmany.relation.integration.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ context('check if the controller file created ', () => {
494494
/description: 'Customer.Order PATCH success count',\n/,
495495
/content: { 'application\/json': { schema: CountSchema } },\n/,
496496
/},\n {4}},\n {2}}\)\n {2}async patch\(\n/,
497-
/\@param\.path\.number\('id'\) id: number,\n {4}\@requestBody\(\) order: Partial<Order>,\n/,
497+
/\@param\.path\.number\('id'\) id: number,\n {4}/,
498+
/\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(Order, {partial: true}\),\s+},\s+},\s+}\)\s+order: Partial<Order>,\n/,
498499
/\@param\.query\.object\('where', getWhereSchemaFor\(Order\)\) where\?: Where<Order>,\n/,
499500
/\): Promise<Count> {\n/,
500501
/return await this\.customerRepository\.orders\(id\).patch\(order, where\);\n {2}}\n/,
@@ -505,7 +506,8 @@ context('check if the controller file created ', () => {
505506
/description: 'CustomerClass.OrderClass PATCH success count',\n/,
506507
/content: { 'application\/json': { schema: CountSchema } },\n/,
507508
/},\n {4}},\n {2}}\)\n {2}async patch\(\n/,
508-
/\@param\.path\.number\('id'\) id: number,\n {4}\@requestBody\(\) orderClass: Partial<OrderClass>,\n/,
509+
/\@param\.path\.number\('id'\) id: number,\n {4}/,
510+
/\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(OrderClass, {partial: true}\),\s+},\s+},\s+}\)\s+orderClass: Partial<OrderClass>,\n/,
509511
/\@param\.query\.object\('where', getWhereSchemaFor\(OrderClass\)\) where\?: Where<OrderClass>,\n/,
510512
/\): Promise<Count> {\n/,
511513
/return await this\.customerClassRepository\.orderClasses\(id\)\.patch\(orderClass, where\);\n {2}}\n/,
@@ -516,7 +518,8 @@ context('check if the controller file created ', () => {
516518
/description: 'CustomerClassType.OrderClassType PATCH success count',\n/,
517519
/content: { 'application\/json': { schema: CountSchema } },\n/,
518520
/},\n {4}},\n {2}}\)\n {2}async patch\(\n/,
519-
/\@param\.path\.number\('id'\) id: number,\n {4}\@requestBody\(\) orderClassType: Partial<OrderClassType>,\n/,
521+
/\@param\.path\.number\('id'\) id: number,\n {4}/,
522+
/\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(OrderClassType, {partial: true}\),\s+},\s+},\s+}\)\s+orderClassType: Partial<OrderClassType>,\n/,
520523
/\@param\.query\.object\('where', getWhereSchemaFor\(OrderClassType\)\) where\?: Where<OrderClassType>,\n/,
521524
/\): Promise<Count> {\n/,
522525
/return await this\.customerClassTypeRepository\.orderClassTypes\(id\).patch\(orderClassType, where\);\n {2}}\n/,

0 commit comments

Comments
 (0)