120 lines
3.1 KiB
TypeScript
120 lines
3.1 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 { AuthGuard } from '@nestjs/passport';
|
||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiParam, ApiBody, ApiResponse } from '@nestjs/swagger';
|
||
|
||
class CreateProjectDto {
|
||
title: string;
|
||
description?: string;
|
||
}
|
||
|
||
@ApiTags('Projects')
|
||
@ApiBearerAuth() // Все маршруты под Bearer Token (Try it out -> authorize)
|
||
@Controller('projects')
|
||
export class ProjectsController {
|
||
constructor(private readonly projectsService: ProjectsService) {}
|
||
|
||
@ApiOperation({ summary: 'Create new project' })
|
||
@ApiBody({
|
||
description: 'Project data',
|
||
type: CreateProjectDto,
|
||
examples: {
|
||
example1: {
|
||
summary: 'Simple project',
|
||
value: {
|
||
title: 'My project',
|
||
description: 'Test desc',
|
||
},
|
||
},
|
||
},
|
||
})
|
||
@ApiResponse({
|
||
status: 201,
|
||
description: 'Project created',
|
||
type: Project,
|
||
})
|
||
@UseGuards(AuthGuard('jwt'))
|
||
@Post('create')
|
||
async create(@Request() req, @Body() body: CreateProjectDto): Promise<Project> {
|
||
return this.projectsService.create({
|
||
title: body.title,
|
||
description: body.description,
|
||
ownerId: req.user.userId,
|
||
});
|
||
}
|
||
|
||
@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 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',
|
||
schema: {
|
||
properties: {
|
||
title: { type: 'string', example: 'Updated Title' },
|
||
description: { type: 'string', example: 'Updated desc' },
|
||
},
|
||
},
|
||
})
|
||
@ApiResponse({
|
||
status: 200,
|
||
description: 'Project updated',
|
||
type: Project,
|
||
})
|
||
@ApiResponse({
|
||
status: 404,
|
||
description: 'Project not found',
|
||
})
|
||
@UseGuards(AuthGuard('jwt'))
|
||
@Patch(':id')
|
||
async update(
|
||
@Param('id') id: number,
|
||
@Body() data: { title?: string; description?: string },
|
||
): Promise<Project> {
|
||
return this.projectsService.update(id, data);
|
||
}
|
||
|
||
@ApiOperation({ summary: 'Delete project' })
|
||
@ApiParam({ name: 'id', type: 'number' })
|
||
@ApiResponse({
|
||
status: 200,
|
||
description: 'Project deleted',
|
||
})
|
||
@ApiResponse({
|
||
status: 404,
|
||
description: 'Project not found',
|
||
})
|
||
@UseGuards(AuthGuard('jwt'))
|
||
@Delete(':id')
|
||
async delete(@Param('id') id: number): Promise<void> {
|
||
return this.projectsService.delete(id);
|
||
}
|
||
}
|