tasks.controller.ts
nest.js/examples/backend/src/tasks/tasks.controller.ts
import {
Controller,
Get,
Post,
Put,
Delete,
Body,
Param,
HttpCode,
HttpStatus,
Options,
} from '@nestjs/common';
import { TasksService } from './tasks.service';
import { CreateTaskDto } from './dto/create-task.dto';
@Controller('tasks')
export class TasksController {
constructor(private readonly tasksService: TasksService) {}
// GET /api/tasks - Get all tasks
@Get()
findAll() {
return {
success: true,
data: this.tasksService.findAll(),
};
}
// GET /api/tasks/:id - Get a specific task
@Get(':id')
findOne(@Param('id') id: string) {
return {
success: true,
data: this.tasksService.findOne(id),
};
}
// POST /api/tasks - Create a new task
@Post()
@HttpCode(HttpStatus.CREATED)
create(@Body() createTaskDto: CreateTaskDto) {
return {
success: true,
message: 'Task created successfully',
data: this.tasksService.create(createTaskDto),
};
}
// PUT /api/tasks/:id - Update a task
@Put(':id')
update(@Param('id') id: string, @Body() updateTaskDto: Partial<CreateTaskDto>) {
return {
success: true,
message: 'Task updated successfully',
data: this.tasksService.update(id, updateTaskDto),
};
}
// DELETE /api/tasks/:id - Delete a task
@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
remove(@Param('id') id: string) {
this.tasksService.remove(id);
return {
success: true,
message: 'Task deleted successfully',
};
}
// OPTIONS /api/tasks - CORS preflight
@Options()
@HttpCode(HttpStatus.NO_CONTENT)
options() {
return;
}
}
Articoli correlati
app.controller.ts
app.controller.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/app.controller.ts).
Leggi l'articolo →app.module.ts
app.module.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/app.module.ts).
Leggi l'articolo →app.service.ts
app.service.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/app.service.ts).
Leggi l'articolo →main.ts
main.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/main.ts).
Leggi l'articolo →todos.controller.ts
todos.controller.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/todos/todos.controller.ts).
Leggi l'articolo →todos.module.ts
todos.module.ts — typescript source code from the nest.js learning materials (nest.js/01-beginner/code/todos/todos.module.ts).
Leggi l'articolo →