Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Patch, | |
| Delete, | |
| HttpCode, | |
| HttpStatus, | |
| Body, | |
| Param, | |
| Query, | |
| Req, | |
| ParseIntPipe, | |
| UseGuards, | |
| } from '@nestjs/common'; | |
| import { | |
| ApiTags, | |
| ApiBearerAuth, | |
| ApiOperation, | |
| ApiResponse, | |
| ApiParam, | |
| ApiQuery, | |
| } from '@nestjs/swagger'; | |
| import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; | |
| import { RolesGuard } from '../../auth/guards/roles.guard'; | |
| import { TasksService } from '../services/tasks.service'; | |
| import { RemindersService } from '../services/reminders.service'; | |
| import { | |
| CreateTaskDto, | |
| UpdateTaskDto, | |
| TaskQueryDto, | |
| CreateReminderDto, | |
| } from '../dto'; | |
| ('📋 Tasks & Reminders') | |
| ('JWT-auth') | |
| ('api/tasks') | |
| (JwtAuthGuard, RolesGuard) | |
| export class TasksController { | |
| constructor( | |
| private readonly tasksService: TasksService, | |
| private readonly remindersService: RemindersService, | |
| ) {} | |
| () | |
| ({ summary: 'List my tasks' }) | |
| ({ status: 200, description: 'Paginated list of tasks' }) | |
| async findAll(() query: TaskQueryDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.tasksService.findAll(userId, query); | |
| } | |
| () | |
| ({ summary: 'Create a new task' }) | |
| ({ status: 201, description: 'Task created' }) | |
| async create(() dto: CreateTaskDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.tasksService.create(dto, userId); | |
| } | |
| // Static routes BEFORE :id param routes | |
| ('upcoming') | |
| ({ summary: 'Get upcoming tasks' }) | |
| ({ | |
| name: 'days', | |
| required: false, | |
| type: Number, | |
| description: 'Number of days ahead (default 7)', | |
| }) | |
| ({ status: 200, description: 'List of upcoming tasks' }) | |
| async findUpcoming(('days') days: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.tasksService.findUpcoming(userId, days ? +days : 7); | |
| } | |
| ('reminders') | |
| ({ summary: 'List my reminders' }) | |
| ({ status: 200, description: 'List of reminders' }) | |
| async findAllReminders(() req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.remindersService.findAll(userId); | |
| } | |
| ('reminders') | |
| ({ summary: 'Create a reminder' }) | |
| ({ status: 201, description: 'Reminder created' }) | |
| async createReminder(() dto: CreateReminderDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.remindersService.create(dto, userId); | |
| } | |
| ('reminders/:id') | |
| ({ summary: 'Delete a reminder' }) | |
| ({ name: 'id', description: 'Reminder ID' }) | |
| ({ status: 200, description: 'Reminder deleted' }) | |
| ({ status: 404, description: 'Reminder not found' }) | |
| async removeReminder(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| await this.remindersService.remove(id, userId); | |
| return { message: 'Reminder deleted successfully' }; | |
| } | |
| // Parameterized routes AFTER static routes | |
| (':id') | |
| ({ summary: 'Get task detail' }) | |
| ({ name: 'id', description: 'Task ID' }) | |
| ({ status: 200, description: 'Task details' }) | |
| ({ status: 404, description: 'Task not found' }) | |
| async findOne(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.tasksService.findOne(id, userId); | |
| } | |
| (':id') | |
| ({ summary: 'Update a task' }) | |
| ({ name: 'id', description: 'Task ID' }) | |
| ({ status: 200, description: 'Task updated' }) | |
| ({ status: 404, description: 'Task not found' }) | |
| async update( | |
| ('id', ParseIntPipe) id: number, | |
| () dto: UpdateTaskDto, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.tasksService.update(id, dto, userId); | |
| } | |
| (':id/complete') | |
| ({ summary: 'Mark task as complete' }) | |
| ({ name: 'id', description: 'Task ID' }) | |
| (HttpStatus.OK) | |
| ({ status: 200, description: 'Task marked as complete' }) | |
| ({ status: 404, description: 'Task not found' }) | |
| async complete( | |
| ('id', ParseIntPipe) id: number, | |
| () body: { notes?: string; timeTakenMinutes?: number }, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.tasksService.complete( | |
| id, | |
| userId, | |
| body.notes, | |
| body.timeTakenMinutes, | |
| ); | |
| } | |
| (':id') | |
| ({ summary: 'Delete a task' }) | |
| ({ name: 'id', description: 'Task ID' }) | |
| ({ status: 200, description: 'Task deleted' }) | |
| ({ status: 404, description: 'Task not found' }) | |
| async remove(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| await this.tasksService.remove(id, userId); | |
| return { message: 'Task deleted successfully' }; | |
| } | |
| } | |