From 23f91c19844d803347fa517fb2c4d66391692148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 21 Jun 2019 09:06:34 +0200 Subject: [PATCH] feat(cli): modify Controller templates to allow partial updates via PATCH MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- docs/site/Controller-generator.md | 22 +++++++++++++++---- .../todo-list-tutorial-controller.md | 9 +++++++- .../controller-rest-template.ts.ejs | 19 ++++++++++++++-- ...ntroller-relation-template-has-many.ts.ejs | 10 ++++++++- .../generators/controller.integration.js | 4 ++-- .../hasmany.relation.integration.js | 9 +++++--- 6 files changed, 60 insertions(+), 13 deletions(-) diff --git a/docs/site/Controller-generator.md b/docs/site/Controller-generator.md index 7ab29ddf96cc..5d97d783f189 100644 --- a/docs/site/Controller-generator.md +++ b/docs/site/Controller-generator.md @@ -169,10 +169,17 @@ export class TodoController { }, }) async updateAll( - @requestBody() data: Todo, + @requestBody({ + content: { + 'application/json': { + schema: getModelSchemaRef(Todo, {partial: true}), + }, + }, + }) + todo: Partial @param.query.object('where', getWhereSchemaFor(Todo)) where?: Where, ): Promise { - return await this.todoRepository.updateAll(data, where); + return await this.todoRepository.updateAll(todo, where); } @get('/todos/{id}', { @@ -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, ): Promise { - await this.todoRepository.updateById(id, data); + await this.todoRepository.updateById(id, todo); } @del('/todos/{id}', { diff --git a/docs/site/tutorials/todo-list/todo-list-tutorial-controller.md b/docs/site/tutorials/todo-list/todo-list-tutorial-controller.md index f197b0381448..f7309418be1f 100644 --- a/docs/site/tutorials/todo-list/todo-list-tutorial-controller.md +++ b/docs/site/tutorials/todo-list/todo-list-tutorial-controller.md @@ -173,7 +173,14 @@ export class TodoListTodoController { }) async patch( @param.path.number('id') id: number, - @requestBody() todo: Partial, + @requestBody({ + content: { + 'application/json': { + schema: getModelSchemaRef(Todo, {partial: true}), + }, + }, + }) + todo: Partial @param.query.object('where', getWhereSchemaFor(Todo)) where?: Where, ): Promise { return await this.todoListRepo.todos(id).patch(todo, where); diff --git a/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs b/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs index cf49640903bd..686dd4082ac8 100644 --- a/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs +++ b/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs @@ -10,6 +10,7 @@ import { param, get, getFilterSchemaFor, + getModelSchemaRef, getWhereSchemaFor, patch, put, @@ -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 { return await this.<%= repositoryNameCamel %>.updateAll(<%= modelVariableName %>, where); @@ -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 { await this.<%= repositoryNameCamel %>.updateById(id, <%= modelVariableName %>); } diff --git a/packages/cli/generators/relation/templates/controller-relation-template-has-many.ts.ejs b/packages/cli/generators/relation/templates/controller-relation-template-has-many.ts.ejs index 8aaf5f732b3f..0270d7e8584b 100644 --- a/packages/cli/generators/relation/templates/controller-relation-template-has-many.ts.ejs +++ b/packages/cli/generators/relation/templates/controller-relation-template-has-many.ts.ejs @@ -8,6 +8,7 @@ import { import { del, get, + getModelSchemaRef, getWhereSchemaFor, param, patch, @@ -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 { return await this.<%= paramSourceRepository %>.<%= relationPropertyName %>(id).patch(<%= targetModelRequestBody %>, where); diff --git a/packages/cli/test/integration/generators/controller.integration.js b/packages/cli/test/integration/generators/controller.integration.js index 0803c67d009b..f3160979a182 100644 --- a/packages/cli/test/integration/generators/controller.integration.js +++ b/packages/cli/test/integration/generators/controller.integration.js @@ -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(|,\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(|,\s+)\)/, ]; patchUpdateAllRegEx.forEach(regex => { assert.fileContent(expectedFile, regex); @@ -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); diff --git a/packages/cli/test/integration/generators/hasmany.relation.integration.js b/packages/cli/test/integration/generators/hasmany.relation.integration.js index 2c6e596c9896..59468136845c 100644 --- a/packages/cli/test/integration/generators/hasmany.relation.integration.js +++ b/packages/cli/test/integration/generators/hasmany.relation.integration.js @@ -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,\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,\n/, /\@param\.query\.object\('where', getWhereSchemaFor\(Order\)\) where\?: Where,\n/, /\): Promise {\n/, /return await this\.customerRepository\.orders\(id\).patch\(order, where\);\n {2}}\n/, @@ -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,\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,\n/, /\@param\.query\.object\('where', getWhereSchemaFor\(OrderClass\)\) where\?: Where,\n/, /\): Promise {\n/, /return await this\.customerClassRepository\.orderClasses\(id\)\.patch\(orderClass, where\);\n {2}}\n/, @@ -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,\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,\n/, /\@param\.query\.object\('where', getWhereSchemaFor\(OrderClassType\)\) where\?: Where,\n/, /\): Promise {\n/, /return await this\.customerClassTypeRepository\.orderClassTypes\(id\).patch\(orderClassType, where\);\n {2}}\n/,