Adding PUT and DELETE to the TodoController
import { Body, Controller, Delete, Param, Put } from '@nestjs/common';
import { UpdateTodoDto } from './dtos/todo.dto';
import { TodoService } from './todo.service';
@Controller('todos')
export class TodoController {
constructor(private readonly todoService: TodoService) {}
@Put(':id')
update(
@Param('id') id: string,
@Body() todo: UpdateTodoDto,
) {
return this.todoService.updateTodo(id, todo);
}
@Delete(':id')
remove(
@Param('id') id: string,
) {
return this.todoService.deleteTodo(id);
}
}
Adding edit and delete to the TodoService
import { Injectable } from '@nestjs/common';
import { UpdateTodoDto } from './dtos/todo.dto';
@Injectable()
export class TodoService {
private todos: TodoDto[] = [];
updateTodo(id: string, todo: UpdateTodoDto): TodoDto | null {
const todoIndex = this.todos.findIndex((todo) => todo.id === id);
if (todoIndex < 0) {
return null;
}
this.todos[todoIndex] = {
...this.todos[todoIndex],
...todo,
};
return this.todos[todoIndex];
}
deleteTodo(id: string): void {
this.todos = this.todos.filter((todo) => todo.id != id);
}
}
Create the UpdateTodoDto
export class CreateTodoDto {
readonly description: string;
readonly title: string;
}
export class TodoDto extends CreateTodoDto {
readonly completed: boolean;
readonly id: string;
}
export class UpdateTodoDto {
readonly completed?: TodoDto['completed'];
readonly description?: TodoDto['description'];
readonly title?: TodoDto['title'];
}