-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(cli): add prompt for plural model name #1314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ summary: LoopBack 4 Todo Application Tutorial - Add a Controller | |
|
|
||
| In LoopBack 4, controllers handle the request-response lifecycle for your API. | ||
| Each function on a controller can be addressed individually to handle an | ||
| incoming request (like a POST request to `/todo`), perform business logic and | ||
| incoming request (like a POST request to `/todos`), perform business logic and | ||
| then return a response. | ||
|
|
||
| In this respect, controllers are the regions _in which most of your business | ||
|
|
@@ -74,7 +74,7 @@ export class TodoController { | |
| @repository(TodoRepository) protected todoRepo: TodoRepository, | ||
| ) {} | ||
|
|
||
| @post('/todo') | ||
| @post('/todos') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the singular name in the path was intentional here. |
||
| async createTodo(@requestBody() todo: Todo) { | ||
| if (!todo.title) { | ||
| throw new HttpErrors.BadRequest('title is required'); | ||
|
|
@@ -87,7 +87,7 @@ export class TodoController { | |
| In this example, we're using two new decorators to provide LoopBack with | ||
| metadata about the route, verb and the format of the incoming request body: | ||
|
|
||
| - `@post('/todo')` creates metadata for `@loopback/rest` so that it can redirect | ||
| - `@post('/todos')` creates metadata for `@loopback/rest` so that it can redirect | ||
| requests to this function when the path and verb match. | ||
| - `@requestBody()` associates the OpenAPI schema for a Todo with the body of the | ||
| request so that LoopBack can validate the format of an incoming request | ||
|
|
@@ -124,25 +124,25 @@ export class TodoController { | |
| @repository(TodoRepository) protected todoRepo: TodoRepository, | ||
| ) {} | ||
|
|
||
| @post('/todo') | ||
| @post('/todos') | ||
| async createTodo(@requestBody() todo: Todo) { | ||
| if (!todo.title) { | ||
| throw new HttpErrors.BadRequest('title is required'); | ||
| } | ||
| return await this.todoRepo.create(todo); | ||
| } | ||
|
|
||
| @get('/todo/{id}') | ||
| @get('/todos/{id}') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here. In REST, it's common and natural to have two path namespaces for a single model - Such design has an important property: it avoids naming clashes between instance ids and remote method names - for example, it's possible to have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With that in mind, I'm wondering whether we should modify our template to 'correct' the designs of our available controller methods. Putting this into example, if given path url was
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be perfect! My only concern is about the UX and implementation complexity of such solution. I think we will have to prompt for both paths ( Thoughts?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
👍
I think now we don't append namespace prefix to the path of controller method, e.g.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The UX of this approach does seem difficult to grasp. Here are all of the possible options that may be available to the users:
As you can see, there are tons of options that can be available to the users and if we were to go with this approach, we should only pick a couple of these to avoid overloading users with choices. @strongloop/lb-next-dev What do you think?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my suggestion in #1314 (comment) is totally a different story :) As for this PR I would like to just focus on adding the prompt.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference, this is how we handle this in LB3. I also like the idea of following REST principles in our templates.
if this can be easily done, then that would make our UX easier and then we are left with a custom http path prompt. Maybe we can compromise between UX and flexibility. |
||
| async findTodoById(@param.path.number('id') id: number): Promise<Todo> { | ||
| return await this.todoRepo.findById(id); | ||
| } | ||
|
|
||
| @get('/todo') | ||
| @get('/todos') | ||
| async findTodos(): Promise<Todo[]> { | ||
| return await this.todoRepo.find(); | ||
| } | ||
|
|
||
| @put('/todo/{id}') | ||
| @put('/todos/{id}') | ||
| async replaceTodo( | ||
| @param.path.number('id') id: number, | ||
| @requestBody() todo: Todo, | ||
|
|
@@ -153,7 +153,7 @@ export class TodoController { | |
| return await this.todoRepo.replaceById(id, todo); | ||
| } | ||
|
|
||
| @patch('/todo/{id}') | ||
| @patch('/todos/{id}') | ||
| async updateTodo( | ||
| @param.path.number('id') id: number, | ||
| @requestBody() todo: Todo, | ||
|
|
@@ -162,7 +162,7 @@ export class TodoController { | |
| return await this.todoRepo.updateById(id, todo); | ||
| } | ||
|
|
||
| @del('/todo/{id}') | ||
| @del('/todos/{id}') | ||
| async deleteTodo(@param.path.number('id') id: number): Promise<boolean> { | ||
| return await this.todoRepo.deleteById(id); | ||
| } | ||
|
|
@@ -171,7 +171,7 @@ export class TodoController { | |
|
|
||
| Some additional things to note about this example: | ||
|
|
||
| - Routes like `@get('/todo/{id}')` can be paired with the `@param.path` | ||
| - Routes like `@get('/todos/{id}')` can be paired with the `@param.path` | ||
| decorators to inject those values at request time into the handler function. | ||
| - LoopBack's `@param` decorator also contains a namespace full of other | ||
| "subdecorators" like `@param.path`, `@param.query`, and `@param.header` that | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are using plural form everywhere, this no longer describes the actual implementation. Could you please fix it @shimks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I'll open a PR to fix this