89 lines
3.0 KiB
TypeScript
89 lines
3.0 KiB
TypeScript
import { Controller, Post, Get, Patch, Delete, Body, Param, UseGuards, Request } from '@nestjs/common';
|
|
import { ProjectsService } from './projects.service';
|
|
import { Project } from './project.entity';
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
import { ApiTags, ApiBearerAuth, ApiOperation, ApiParam, ApiBody, ApiResponse } from '@nestjs/swagger';
|
|
|
|
class CreateProjectDto {
|
|
title: string;
|
|
description?: string;
|
|
}
|
|
|
|
class UpdateProjectDto {
|
|
title?: string;
|
|
description?: string;
|
|
}
|
|
|
|
@ApiTags('Projects')
|
|
@ApiBearerAuth()
|
|
@Controller('projects')
|
|
export class ProjectsController {
|
|
constructor(private readonly projectsService: ProjectsService) {}
|
|
|
|
@ApiOperation({ summary: 'Create new project' })
|
|
@ApiBody({
|
|
description: 'Project data',
|
|
type: CreateProjectDto,
|
|
})
|
|
@ApiResponse({ status: 201, description: 'Project created', type: Project })
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post('create')
|
|
async create(@Request() req, @Body() body: CreateProjectDto): Promise<Project> {
|
|
return this.projectsService.create({
|
|
title: body.title,
|
|
description: body.description,
|
|
ownerId: req.user?.sub,
|
|
});
|
|
}
|
|
|
|
|
|
@ApiOperation({ summary: 'Get all projects' })
|
|
@ApiResponse({ status: 200, description: 'List of projects', type: Project, isArray: true })
|
|
@Get()
|
|
async findAll(): Promise<Project[]> {
|
|
return this.projectsService.findAll();
|
|
}
|
|
|
|
@ApiOperation({ summary: 'Get my projects' })
|
|
@ApiResponse({ status: 200, description: 'List of projects', type: Project, isArray: true })
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('my')
|
|
async findMyProjects(@Request() req): Promise<Project[]> {
|
|
return this.projectsService.findByOwner(req.user.sub);
|
|
}
|
|
|
|
@ApiOperation({ summary: 'Get single project by ID' })
|
|
@ApiParam({ name: 'id', type: 'number' })
|
|
@ApiResponse({ status: 200, description: 'Found project', type: Project })
|
|
@ApiResponse({ status: 404, description: 'Project not found' })
|
|
@Get(':id')
|
|
async findOne(@Param('id') id: number): Promise<Project | null> {
|
|
return this.projectsService.findOneById(id);
|
|
}
|
|
|
|
@ApiOperation({ summary: 'Update project' })
|
|
@ApiParam({ name: 'id', type: 'number' })
|
|
@ApiBody({ description: 'Fields to update', type: UpdateProjectDto })
|
|
@ApiResponse({ status: 200, description: 'Project updated', type: Project })
|
|
@ApiResponse({ status: 404, description: 'Project not found' })
|
|
@UseGuards(JwtAuthGuard)
|
|
@Patch(':id')
|
|
async update(
|
|
@Request() req,
|
|
@Param('id') id: number,
|
|
@Body() data: UpdateProjectDto,
|
|
): Promise<Project> {
|
|
return this.projectsService.update(id, data, req.user.sub);
|
|
}
|
|
|
|
@ApiOperation({ summary: 'Delete project' })
|
|
@ApiParam({ name: 'id', type: 'number' })
|
|
@ApiResponse({ status: 200, description: 'Project deleted' })
|
|
@ApiResponse({ status: 404, description: 'Project not found' })
|
|
@UseGuards(JwtAuthGuard)
|
|
@Delete(':id')
|
|
async delete(@Request() req, @Param('id') id: number): Promise<void> {
|
|
return this.projectsService.delete(id, req.user.sub);
|
|
}
|
|
}
|