Defining business logic to handle requests to related models isn’t too differentfrom handling requests for standalone models. We’ll create controllers to handlerequests for todo-lists and todo items under a todo-list.

Run the CLI command for creating a RESTful CRUD controller for our routes with the following inputs:

And voilà! We now have a set of basic APIs for todo-lists, just like that!

In order to get our related Todos for each TodoList, let’s update theschema.

In src/models/todo-list.controller.ts, first import getModelSchemaRef from@loopback/rest.

Then update the following schemas in responses’s content:

  1. @get('/todo-lists', {
  2. responses: {
  3. '200': {
  4. description: 'Array of TodoList model instances',
  5. content: {
  6. 'application/json': {
  7. schema: {
  8. type: 'array',
  9. items: getModelSchemaRef(TodoList, {includeRelations: true}),
  10. },
  11. },
  12. },
  13. },
  14. },
  15. })
  16. async find(/*...*/) {/*...*/}
  17. @get('/todo-lists/{id}', {
  18. responses: {
  19. description: 'TodoList model instance',
  20. 'application/json': {
  21. schema: getModelSchemaRef(TodoList, {includeRelations: true}),
  22. },
  23. },
  24. },
  25. },
  26. })
  27. async findById(/*...*/) {/*...*/}

Let’s also update it in the TodoController:

src/models/todo.controller.ts

For the controller handling Todos of a TodoList, we’ll start with an emptycontroller:

  1. $ lb4 controller
  2. ? Controller class name: TodoListTodo
  3. Controller TodoListTodo will be created in src/controllers/todo-list-todo.controller.ts
  4. ? What kind of controller would you like to generate? Empty Controller
  5. create src/controllers/todo-list-todo.controller.ts
  6. update src/controllers/index.ts
  7. Controller TodoListTodo was created in src/controllers/

Let’s add in an injection for our TodoListRepository:

src/controllers/todo-list-todo.controller.ts

We’re now ready to add in some routes for our todo requests. To call the CRUDmethods on a todo-list’s todo items, we’ll first need to create a constrainedTodoRepository. We can achieve this by using our repository instance’s todosfactory function that we defined earlier in TodoListRepository.

src/controllers/todo-list-todo.controller.ts

  1. import {repository} from '@loopback/repository';
  2. import {TodoListRepository} from '../repositories';
  3. import {post, param, requestBody} from '@loopback/rest';
  4. import {Todo} from '../models';
  5. constructor(
  6. @repository(TodoListRepository) protected todoListRepo: TodoListRepository,
  7. ) {}
  8. @post('/todo-lists/{id}/todos')
  9. async create(
  10. @param.path.number('id') id: number,
  11. @requestBody({
  12. content: {
  13. 'application/json': {
  14. schema: getModelSchemaRef(Todo, {exclude: ['id']}),
  15. },
  16. },
  17. })
  18. todo: Omit<Todo, 'id'>,
  19. ) {
  20. return this.todoListRepo.todos(id).create(todo);
  21. }
  22. }

Using our constraining factory as we did with the POST request, we’ll definethe controller methods for the rest of the HTTP verbs for the route. Thecompleted controller should look as follows:

src/controllers/todo-list-todo.controller.ts

Check out our TodoList example to see the full source code generated for theTodoListTodo controller:

With the controllers complete, your application is ready to start up again!@loopback/boot should wire up everything for us when we start the application,so there’s nothing else we need to do before we try out our new routes.

  1. $ npm start
  2. Server is running at http://127.0.0.1:3000

Here are some new requests you can try out:

  • POST /todo-lists with a body of { "title": "grocery list" }.
  • POST /todo-lists/{id}/todos using the ID you got back from the previousPOST request and a body for a todo. Notice that response body you get backcontains property with the ID from before.
  • GET /todo-lists/{id}/todos and see if you get the todo you created frombefore.And there you have it! You now have the power to define APIs for related models!