Building a To-Do REST API with NestJS - Set Up GET Queries
Creating the TodoController
controllers handle incoming requests and return responses to client
import{ Controller, Get, Param }from'@nestjs/common';import{ TodoDto }from'./dto/todo.dto';import{ TodoService }from'./todo.service';@Controller('todos')exportclassTodoController{constructor(privatereadonly todoService: TodoService){}@Get()findAll(): TodoDto[]{returnthis.todoService.findAll();}// Extend the /todos route to GET a single Todo via the /todos/:id route@Get(':id')findOne(// Use the @Param('id') decorator to decoare that the `:id` in the route will// map to the `id` parameter@Param('id') id:string,): TodoDto {returnthis.todoService.findOne(id);}}
Creating the TodoService
import{ Injectable }from'@nestjs/common';import{ TodoDto }from'./dto/todo.dto';@Injectable()exportclassTodoService{// temp dataprivate todos: TodoDto[]=[{
id:'1',
title:'Learn NestJS',
description:'Learn how to build with NestJS',
completed:true,},{
id:'2',
title:'Learn REST',
description:'Learn how to build interactive REST APIs',
completed:false,},];findAll(showIncomplete?:boolean): TodoDto[]{returnthis.todos;}fetchOne(id:string): TodoDto |null{returnthis.todos.find((todo)=> todo.id === id)||null;}}
Connecting the Controller and Service
In TodoController, we inject TodoService through the constructor using Dependency Injection.
In the findAll method of TodoController, we call this.todoService.findAll() to fetch all To-Dos.
In the findOne method of TodoController, we call this.todoService.findOne(id) to fetch a specific To-Do by ID.