todos.controller.ts
nest.js/01-beginner/code/todos/todos.controller.ts
import {
Controller,
Get,
Post,
Put,
Delete,
Body,
Param,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import { TodosService } from './todos.service';
import { CreateTodoDto } from './dto/create-todo.dto';
@Controller('todos')
export class TodosController {
constructor(private readonly todosService: TodosService) {}
// GET /todos - Get all todos
@Get()
findAll() {
return {
success: true,
data: this.todosService.findAll(),
};
}
// GET /todos/:id - Get a specific todo
@Get(':id')
findOne(@Param('id') id: string) {
return {
success: true,
data: this.todosService.findOne(id),
};
}
// POST /todos - Create a new todo
@Post()
@HttpCode(HttpStatus.CREATED)
create(@Body() createTodoDto: CreateTodoDto) {
return {
success: true,
message: 'Todo created successfully',
data: this.todosService.create(createTodoDto),
};
}
// PUT /todos/:id - Update a todo
@Put(':id')
update(@Param('id') id: string, @Body() updateTodoDto: Partial<CreateTodoDto>) {
return {
success: true,
message: 'Todo updated successfully',
data: this.todosService.update(id, updateTodoDto),
};
}
// DELETE /todos/:id - Delete a todo
@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
remove(@Param('id') id: string) {
this.todosService.remove(id);
return {
success: true,
message: 'Todo deleted successfully',
};
}
}
Artigos relacionados
app.controller.ts
app.controller.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/app.controller.ts).
Ler artigo →app.module.ts
app.module.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/app.module.ts).
Ler artigo →app.service.ts
app.service.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/app.service.ts).
Ler artigo →main.ts
main.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/main.ts).
Ler artigo →todos.module.ts
todos.module.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/todos/todos.module.ts).
Ler artigo →todos.service.ts
todos.service.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/todos/todos.service.ts).
Ler artigo →