|
| 1 | +--- |
| 2 | +lang: en |
| 3 | +title: "Add TodoList and TodoList's Todo Controller" |
| 4 | +keywords: LoopBack 4.0, LoopBack 4 |
| 5 | +tags: |
| 6 | +sidebar: lb4_sidebar |
| 7 | +permalink: /doc/en/lb4/todo-list-tutorial-controller.html |
| 8 | +summary: LoopBack 4 TodoList Application Tutorial - Add TodoList and TodoList's Todo Controller |
| 9 | +--- |
| 10 | + |
| 11 | +### Controllers with related models |
| 12 | + |
| 13 | +Defining business logic to handle requests to related models isn't too different |
| 14 | +from handling requests for standalone models. We'll create controllers to handle |
| 15 | +requests for todo-lists and todo items under a todo-list. |
| 16 | + |
| 17 | +### Create TodoList controller |
| 18 | + |
| 19 | +Run the CLI command for creating a RESTful CRUD controller for our `TodoList` |
| 20 | +routes with the following inputs: |
| 21 | + |
| 22 | +```sh |
| 23 | +$ lb4 controller |
| 24 | +? Controller class name: TodoList |
| 25 | +? What kind of controller would you like to generate? REST Controller with CRUD functions |
| 26 | +? What is the name of the model to use with this CRUD repository? TodoList |
| 27 | +? What is the name of your CRUD repository? TodoRepository |
| 28 | +? What is the type of your ID? number |
| 29 | +? What is the base HTTP path name of the CRUD operations? /todo-lists |
| 30 | + create src/controllers/todo-list.controller.ts |
| 31 | + update src/controllers/index.ts |
| 32 | + |
| 33 | +Controller TodoList was created in src/controllers/ |
| 34 | +``` |
| 35 | + |
| 36 | +And voilà! We now have a set of basic APIs for todo-lists just like that! |
| 37 | + |
| 38 | +### Create TodoList's Todo controller |
| 39 | + |
| 40 | +For the controller handling `Todos` of a `TodoList`, we'll start with an empty |
| 41 | +controller: |
| 42 | + |
| 43 | +```sh |
| 44 | +$ lb4 controller |
| 45 | +? Controller class name: TodoListTodo |
| 46 | +? What kind of controller would you like to generate? Empty Controller |
| 47 | + create src/controllers/todo-list-todo.controller.ts |
| 48 | + update src/controllers/index.ts |
| 49 | + |
| 50 | +Controller TodoListTodo was created in src/controllers/ |
| 51 | +``` |
| 52 | + |
| 53 | +Let's add in an injection for our `TodoListRepository`: |
| 54 | + |
| 55 | +#### src/controllers/todo-list-todo.controller.ts |
| 56 | + |
| 57 | +```ts |
| 58 | +import {repository} from '@loopback/repository'; |
| 59 | +import {TodoListRepository} from '../repositories'; |
| 60 | + |
| 61 | +export class TodoListTodoController { |
| 62 | + constructor( |
| 63 | + @repository(TodoListRepository) protected todoListRepo: TodoListRepository, |
| 64 | + ) {} |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +With this, we're now ready to add in some routes for our todo requests. To call |
| 69 | +the CRUD methods on a todo-list's todo items, we'll first need to create a |
| 70 | +constrained `TodoRepository`. We can achieve this by using our repository |
| 71 | +instance's `todos` factory function that we defined earlier in |
| 72 | +`TodoListRepository`. |
| 73 | + |
| 74 | +The `POST` request from `/todo-lists/{id}/todos` should look something like |
| 75 | +this: |
| 76 | + |
| 77 | +#### src/controllers/todo-list-todo.controller.ts |
| 78 | + |
| 79 | +```ts |
| 80 | +import {repository} from '@loopback/repository'; |
| 81 | +import {TodoListRepository} from '../repositories'; |
| 82 | +import {post, param, requestBody} from '@loopback/rest'; |
| 83 | +import {Todo} from '../models'; |
| 84 | + |
| 85 | +export class TodoListTodoController { |
| 86 | + constructor( |
| 87 | + @repository(TodoListRepository) protected todoListRepo: TodoListRepository, |
| 88 | + ) {} |
| 89 | + |
| 90 | + @post('/todo-lists/{id}/todos') |
| 91 | + async create(@param.path.number('id') id: number, @requestBody() todo: Todo) { |
| 92 | + return await this.todoListRepo.todos(id).create(todo); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Using our constraining factory as we did with the `POST` request, we'll define |
| 98 | +the controller methods for the rest of the HTTP verbs for the route. The |
| 99 | +completed controller should look as follows: |
| 100 | + |
| 101 | +#### src/controllers/todo-list-todo.controller.ts |
| 102 | + |
| 103 | +```ts |
| 104 | +import {repository, Filter, Where} from '@loopback/repository'; |
| 105 | +import {TodoListRepository} from '../repositories'; |
| 106 | +import {post, get, patch, del, param, requestBody} from '@loopback/rest'; |
| 107 | +import {Todo} from '../models'; |
| 108 | + |
| 109 | +export class TodoListTodoController { |
| 110 | + constructor( |
| 111 | + @repository(TodoListRepository) protected todoListRepo: TodoListRepository, |
| 112 | + ) {} |
| 113 | + |
| 114 | + @post('/todo-lists/{id}/todos') |
| 115 | + async create(@param.path.number('id') id: number, @requestBody() todo: Todo) { |
| 116 | + return await this.todoListRepo.todos(id).create(todo); |
| 117 | + } |
| 118 | + |
| 119 | + @get('/todo-lists/{id}/todos') |
| 120 | + async find( |
| 121 | + @param.path.number('id') id: number, |
| 122 | + @param.query.string('filter') filter?: Filter, |
| 123 | + ) { |
| 124 | + return await this.todoListRepo.todos(id).find(filter); |
| 125 | + } |
| 126 | + |
| 127 | + @patch('/todo-lists/{id}/todos') |
| 128 | + async patch( |
| 129 | + @param.path.number('id') id: number, |
| 130 | + @requestBody() todo: Partial<Todo>, |
| 131 | + @param.query.string('where') where?: Where, |
| 132 | + ) { |
| 133 | + return await this.todoListRepo.todos(id).patch(todo, where); |
| 134 | + } |
| 135 | + |
| 136 | + @del('/todo-lists/{id}/todos') |
| 137 | + async delete( |
| 138 | + @param.path.number('id') id: number, |
| 139 | + @param.query.string('where') where?: Where, |
| 140 | + ) { |
| 141 | + return await this.todoListRepo.todos(id).delete(where); |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### Try it out |
| 147 | + |
| 148 | +With the controllers complete, your application is ready to start up again! |
| 149 | +`@loopback/boot` should wire up everything for us when we start the application, |
| 150 | +so there's nothing else we need to do before we try out our new routes. |
| 151 | + |
| 152 | +```sh |
| 153 | +$ npm start |
| 154 | +Server is running at http://127.0.0.1:3000 |
| 155 | +``` |
| 156 | + |
| 157 | +Here are some new requests you can try out: |
| 158 | + |
| 159 | +- `POST /todo-lists` with a body of `{ "title": "grocery list" }`. |
| 160 | +- `POST /todo-lists/{id}/todos` using the ID you got back from the previous |
| 161 | + `POST` request and a body for a todo. Notice that response body you get back |
| 162 | + contains property `todoListId` with the ID from before. |
| 163 | +- `GET /todos/{id}/todos` and see if you get the todo you created from before. |
| 164 | + |
| 165 | +And there you have it! You now have the power to define API for related models! |
| 166 | + |
| 167 | +### Navigation |
| 168 | + |
| 169 | +Previous step: [Add TodoList repository](todo-list-tutorial-repository.md) |
0 commit comments