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!
Inclusion of Related Models
In order to get our related Todo
s for each TodoList
, let’s update theschema
.
In src/models/todo-list.controller.ts
, first import getModelSchemaRef
from@loopback/rest
.
Then update the following schema
s in responses
’s content
:
@get('/todo-lists', {
responses: {
'200': {
description: 'Array of TodoList model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(TodoList, {includeRelations: true}),
},
},
},
},
},
})
async find(/*...*/) {/*...*/}
@get('/todo-lists/{id}', {
responses: {
description: 'TodoList model instance',
'application/json': {
schema: getModelSchemaRef(TodoList, {includeRelations: true}),
},
},
},
},
})
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:
$ lb4 controller
? Controller class name: TodoListTodo
Controller TodoListTodo will be created in src/controllers/todo-list-todo.controller.ts
? What kind of controller would you like to generate? Empty Controller
create src/controllers/todo-list-todo.controller.ts
update src/controllers/index.ts
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 todos
factory function that we defined earlier in TodoListRepository
.
src/controllers/todo-list-todo.controller.ts
import {repository} from '@loopback/repository';
import {TodoListRepository} from '../repositories';
import {post, param, requestBody} from '@loopback/rest';
import {Todo} from '../models';
constructor(
@repository(TodoListRepository) protected todoListRepo: TodoListRepository,
) {}
@post('/todo-lists/{id}/todos')
async create(
@param.path.number('id') id: number,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Todo, {exclude: ['id']}),
},
},
})
todo: Omit<Todo, 'id'>,
) {
return this.todoListRepo.todos(id).create(todo);
}
}
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.
$ npm start
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!