Skip to content

Commit

Permalink
feat(cli): modify Controller templates to allow partial updates via P…
Browse files Browse the repository at this point in the history
…ATCH

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Jun 21, 2019
1 parent f23dfdc commit 23f91c1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
22 changes: 18 additions & 4 deletions docs/site/Controller-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,17 @@ export class TodoController {
},
})
async updateAll(
@requestBody() data: Todo,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Todo, {partial: true}),
},
},
})
todo: Partial<Todo>
@param.query.object('where', getWhereSchemaFor(Todo)) where?: Where<Todo>,
): Promise<number> {
return await this.todoRepository.updateAll(data, where);
return await this.todoRepository.updateAll(todo, where);
}

@get('/todos/{id}', {
Expand All @@ -196,9 +203,16 @@ export class TodoController {
})
async updateById(
@param.path.number('id') id: number,
@requestBody() data: Todo,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Todo, {partial: true}),
},
},
})
todo: Partial<Todo>,
): Promise<void> {
await this.todoRepository.updateById(id, data);
await this.todoRepository.updateById(id, todo);
}

@del('/todos/{id}', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,14 @@ export class TodoListTodoController {
})
async patch(
@param.path.number('id') id: number,
@requestBody() todo: Partial<Todo>,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Todo, {partial: true}),
},
},
})
todo: Partial<Todo>
@param.query.object('where', getWhereSchemaFor(Todo)) where?: Where<Todo>,
): Promise<Count> {
return await this.todoListRepo.todos(id).patch(todo, where);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
param,
get,
getFilterSchemaFor,
getModelSchemaRef,
getWhereSchemaFor,
patch,
put,
Expand Down Expand Up @@ -78,7 +79,14 @@ export class <%= className %>Controller {
},
})
async updateAll(
@requestBody() <%= modelVariableName %>: <%= modelName %>,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(<%= modelName %>, {partial: true}),
},
},
})
<%= modelVariableName %>: <%= modelName %>,
@param.query.object('where', getWhereSchemaFor(<%= modelName %>)) where?: Where<<%= modelName %>>,
): Promise<Count> {
return await this.<%= repositoryNameCamel %>.updateAll(<%= modelVariableName %>, where);
Expand All @@ -105,7 +113,14 @@ export class <%= className %>Controller {
})
async updateById(
@param.path.<%= idType %>('id') id: <%= idType %>,
@requestBody() <%= modelVariableName %>: <%= modelName %>,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(<%= modelName %>, {partial: true}),
},
},
})
<%= modelVariableName %>: <%= modelName %>,
): Promise<void> {
await this.<%= repositoryNameCamel %>.updateById(id, <%= modelVariableName %>);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {
del,
get,
getModelSchemaRef,
getWhereSchemaFor,
param,
patch,
Expand Down Expand Up @@ -69,7 +70,14 @@ export class <%= controllerClassName %> {
})
async patch(
@param.path.<%= sourceModelPrimaryKeyType %>('id') id: <%= sourceModelPrimaryKeyType %>,
@requestBody() <%= targetModelRequestBody %>: Partial<<%= targetModelClassName %>>,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(<%= targetModelClassName %>, {partial: true}),
},
},
})
<%= targetModelRequestBody %>: Partial<<%= targetModelClassName %>>,
@param.query.object('where', getWhereSchemaFor(<%= targetModelClassName %>)) where?: Where<<%= targetModelClassName %>>,
): Promise<Count> {
return await this.<%= paramSourceRepository %>.<%= relationPropertyName %>(id).patch(<%= targetModelRequestBody %>, where);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function checkRestCrudContents() {
/'200': {/,
/description: 'ProductReview PATCH success count'/,
/content: {'application\/json': {schema: CountSchema}},\s{1,}},\s{1,}},\s{1,}}\)/,
/async updateAll\(\s{1,}\@requestBody\(\) productReview: ProductReview,\s{1,} @param\.query\.object\('where', getWhereSchemaFor\(ProductReview\)\) where\?: Where<ProductReview>(|,\s+)\)/,
/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+)\)/,
];
patchUpdateAllRegEx.forEach(regex => {
assert.fileContent(expectedFile, regex);
Expand All @@ -312,7 +312,7 @@ function checkRestCrudContents() {
/responses: {/,
/'204': {/,
/description: 'ProductReview PATCH success'/,
/async updateById\(\s{1,}\@param.path.number\('id'\) id: number,\s{1,}\@requestBody\(\) productReview: ProductReview,\s+\)/,
/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+\)/,
];
patchUpdateByIdRegEx.forEach(regex => {
assert.fileContent(expectedFile, regex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ context('check if the controller file created ', () => {
/description: 'Customer.Order PATCH success count',\n/,
/content: { 'application\/json': { schema: CountSchema } },\n/,
/},\n {4}},\n {2}}\)\n {2}async patch\(\n/,
/\@param\.path\.number\('id'\) id: number,\n {4}\@requestBody\(\) order: Partial<Order>,\n/,
/\@param\.path\.number\('id'\) id: number,\n {4}/,
/\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(Order, {partial: true}\),\s+},\s+},\s+}\)\s+order: Partial<Order>,\n/,
/\@param\.query\.object\('where', getWhereSchemaFor\(Order\)\) where\?: Where<Order>,\n/,
/\): Promise<Count> {\n/,
/return await this\.customerRepository\.orders\(id\).patch\(order, where\);\n {2}}\n/,
Expand All @@ -505,7 +506,8 @@ context('check if the controller file created ', () => {
/description: 'CustomerClass.OrderClass PATCH success count',\n/,
/content: { 'application\/json': { schema: CountSchema } },\n/,
/},\n {4}},\n {2}}\)\n {2}async patch\(\n/,
/\@param\.path\.number\('id'\) id: number,\n {4}\@requestBody\(\) orderClass: Partial<OrderClass>,\n/,
/\@param\.path\.number\('id'\) id: number,\n {4}/,
/\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(OrderClass, {partial: true}\),\s+},\s+},\s+}\)\s+orderClass: Partial<OrderClass>,\n/,
/\@param\.query\.object\('where', getWhereSchemaFor\(OrderClass\)\) where\?: Where<OrderClass>,\n/,
/\): Promise<Count> {\n/,
/return await this\.customerClassRepository\.orderClasses\(id\)\.patch\(orderClass, where\);\n {2}}\n/,
Expand All @@ -516,7 +518,8 @@ context('check if the controller file created ', () => {
/description: 'CustomerClassType.OrderClassType PATCH success count',\n/,
/content: { 'application\/json': { schema: CountSchema } },\n/,
/},\n {4}},\n {2}}\)\n {2}async patch\(\n/,
/\@param\.path\.number\('id'\) id: number,\n {4}\@requestBody\(\) orderClassType: Partial<OrderClassType>,\n/,
/\@param\.path\.number\('id'\) id: number,\n {4}/,
/\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(OrderClassType, {partial: true}\),\s+},\s+},\s+}\)\s+orderClassType: Partial<OrderClassType>,\n/,
/\@param\.query\.object\('where', getWhereSchemaFor\(OrderClassType\)\) where\?: Where<OrderClassType>,\n/,
/\): Promise<Count> {\n/,
/return await this\.customerClassTypeRepository\.orderClassTypes\(id\).patch\(orderClassType, where\);\n {2}}\n/,
Expand Down

0 comments on commit 23f91c1

Please sign in to comment.