Building a To-Do REST API with NestJS - Enhancing the API with Filters and Specific Modifiers


Adding Filters to the Todo API

Updating findAll method in TodoController

import { Controller, Get, Query } from '@nestjs/common';

import { TodoDto } from './dtos/todo.dto';
import { TodoService } from './todo.service';

@Controller('todos')
export class TodoController {
  constructor(private readonly todoService: TodoService) {}

  @Get()
  findAll(
    // Introduce query parameter called `showIncomplete`
    @Query('showIncomplete') showIncomplete: boolean,
  ): TodoDto[] {
    return this.todoService.findAll(showIncomplete);
  }

  // ... Existing GET, POST, PUT, and DELETE handlers
}

Updating findAll method in TodoService

import { Injectable } from '@nestjs/common';

import { TodoDto } from './dtos/todo.dto';

@Injectable()
export class TodoService {
  private todos: TodoDto[] = [];

  findAll(showIncomplete?: boolean): TodoDto[] {
    if (showIncomplete) {
      return this.todos.filter((todo) => !todo.completed);
    }

    return this.todos;
  }

  // ... Existing `findOne`, `create`, `update`, and `delete` methods
}

Adding Specific Modifiers to the Todo API

  • path parameters can be used to identify specific resources in an API (e.g. /todos/:id/complete)

Adding complete method to TodoController

import { Controller, Param, Put } from '@nestjs/common';

import { TodoDto } from './dtos/todo.dto';
import { TodoService } from './todo.service';

@Controller('todos')
export class TodoController {
  constructor(private readonly todoService: TodoService) {}

  // ... Existing GET, POST, and PUT handlers

  @Put(':id/complete')
  complete(@Param('id') id: string): TodoDto {
    return this.todoService.markTodoComplete(id);
  }

  // ... Existing DELETE handler
}

Adding complete method to TodoService

import { Injectable } from '@nestjs/common';

import { TodoDto } from './dtos/todo.dto';

@Injectable()
export class TodoService {
  private todos: TodoDto[] = [];

  // ... Existing `findOne`, `findAll`, `create`, `update`, and `delete` methods

  markTodoComplete(id: string): TodoDto | null {
    return this.updateTodo(id, { completed: true });
  }
}
Made with Gatsby G Logo