2025-03-03 17:28:22 +03:00

95 lines
3.2 KiB
TypeScript

import { Controller, Post, Get, Patch, Delete, Body, Param, UseGuards, Request } from '@nestjs/common';
import { TasksService } from './tasks.service';
import { Task } from './task.entity';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiParam, ApiBody, ApiResponse } from '@nestjs/swagger';
class CreateTaskDto {
title: string;
projectId: number;
assignedUserId?: number;
deadline?: string;
}
class UpdateTaskDto {
title?: string;
status?: string; // 'todo', 'in_progress', 'done'
deadline?: string;
assignedUserId?: number;
}
@ApiTags('Tasks')
@ApiBearerAuth()
@Controller('tasks')
@UseGuards(JwtAuthGuard)
export class TasksController {
constructor(private readonly tasksService: TasksService) {}
@ApiOperation({ summary: 'Create new task' })
@ApiBody({
description: 'Task creation data',
type: CreateTaskDto,
})
@ApiResponse({ status: 201, description: 'Task created', type: Task })
@Post('create')
async create(@Request() req, @Body() body: CreateTaskDto): Promise<Task> {
return this.tasksService.create({
title: body.title,
projectId: body.projectId,
assignedUserId: body.assignedUserId ?? req.user.sub,
deadline: body.deadline ? new Date(body.deadline) : undefined,
});
}
@ApiOperation({ summary: 'Get all tasks' })
@ApiResponse({ status: 200, description: 'List of tasks', type: Task, isArray: true })
@Get()
async findAll(): Promise<Task[]> {
return this.tasksService.findAll();
}
@ApiOperation({ summary: 'Get task by ID' })
@ApiParam({ name: 'id', type: 'number' })
@ApiResponse({ status: 200, description: 'Found task', type: Task })
@ApiResponse({ status: 404, description: 'Task not found' })
@Get(':id')
async findOne(@Param('id') id: number): Promise<Task | null> {
return this.tasksService.findOneById(id);
}
@ApiOperation({ summary: 'Get tasks by project ID' })
@ApiParam({ name: 'projectId', type: 'number' })
@ApiResponse({ status: 200, description: 'List of tasks for the specified project', type: Task, isArray: true })
@Get('project/:projectId')
async findByProject(@Param('projectId') projectId: number): Promise<Task[]> {
return this.tasksService.findByProjectId(projectId);
}
@ApiOperation({ summary: 'Update task' })
@ApiParam({ name: 'id', type: 'number' })
@ApiBody({
description: 'Fields to update in the task',
type: UpdateTaskDto,
})
@ApiResponse({ status: 200, description: 'Task updated', type: Task })
@ApiResponse({ status: 404, description: 'Task not found' })
@Patch(':id')
async update(@Request() req, @Param('id') id: number, @Body() body: UpdateTaskDto): Promise<Task> {
return this.tasksService.update(id, {
title: body.title,
status: body.status,
deadline: body.deadline ? new Date(body.deadline) : undefined,
assignedUserId: body.assignedUserId ?? req.user.sub,
});
}
@ApiOperation({ summary: 'Delete task' })
@ApiParam({ name: 'id', type: 'number' })
@ApiResponse({ status: 200, description: 'Task deleted' })
@ApiResponse({ status: 404, description: 'Task not found' })
@Delete(':id')
async delete(@Request() req, @Param('id') id: number) {
return this.tasksService.delete(id);
}
}