Files
backend/src/project/controllers/project.controller.ts
2025-04-21 10:58:14 +04:00

130 lines
3.8 KiB
TypeScript

import { ControllerRoute } from '@app/common/constants/controller-route';
import {
Body,
Controller,
Delete,
Get,
Header,
HttpStatus,
Param,
Post,
Put,
Query,
Res,
UseGuards,
} from '@nestjs/common';
import { Response } from 'express';
import {
ApiBearerAuth,
ApiOperation,
ApiProduces,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { ProjectService } from '../services';
import { CreateProjectDto, GetProjectParam } from '../dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
@ApiTags('Project Module')
@Controller({
version: '1',
path: ControllerRoute.PROJECT.ROUTE,
})
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: ControllerRoute.PROJECT.ACTIONS.CREATE_PROJECT_SUMMARY,
description: ControllerRoute.PROJECT.ACTIONS.CREATE_PROJECT_DESCRIPTION,
})
@Post()
async createProject(@Body() dto: CreateProjectDto): Promise<BaseResponseDto> {
return this.projectService.createProject(dto);
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: ControllerRoute.PROJECT.ACTIONS.UPDATE_PROJECT_SUMMARY,
description: ControllerRoute.PROJECT.ACTIONS.UPDATE_PROJECT_DESCRIPTION,
})
@Put(':projectUuid')
async updateProject(
@Param() params: GetProjectParam,
@Body() dto: CreateProjectDto,
): Promise<BaseResponseDto> {
return this.projectService.updateProject(params.projectUuid, dto);
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: ControllerRoute.PROJECT.ACTIONS.DELETE_PROJECT_SUMMARY,
description: ControllerRoute.PROJECT.ACTIONS.DELETE_PROJECT_DESCRIPTION,
})
@Delete(':projectUuid')
async deleteProject(
@Param() params: GetProjectParam,
): Promise<BaseResponseDto> {
return this.projectService.deleteProject(params.projectUuid);
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: ControllerRoute.PROJECT.ACTIONS.LIST_PROJECTS_SUMMARY,
description: ControllerRoute.PROJECT.ACTIONS.LIST_PROJECTS_DESCRIPTION,
})
@Get()
async list(
@Query() query: PaginationRequestGetListDto,
): Promise<BaseResponseDto> {
return this.projectService.listProjects(query);
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: ControllerRoute.PROJECT.ACTIONS.GET_PROJECT_SUMMARY,
description: ControllerRoute.PROJECT.ACTIONS.GET_PROJECT_DESCRIPTION,
})
@Get(':projectUuid')
async findOne(@Param() params: GetProjectParam): Promise<BaseResponseDto> {
return this.projectService.getProject(params.projectUuid);
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: ControllerRoute.PROJECT.ACTIONS.EXPORT_STRUCTURE_CSV_SUMMARY,
description:
ControllerRoute.PROJECT.ACTIONS.EXPORT_STRUCTURE_CSV_DESCRIPTION,
})
@Get(':projectUuid/structure/export-csv')
@ApiProduces('text/csv')
@ApiResponse({
status: 200,
description:
'A CSV file containing project details and their structural hierarchy (Project, Community, Space, Parent Space).',
})
@Header('Content-Type', 'text/csv')
@Header('Content-Disposition', 'attachment; filename=project-structure.csv')
async exportStructures(
@Param() params: GetProjectParam,
@Res() res: Response,
): Promise<void> {
try {
const csvStream = await this.projectService.exportToCsv(params);
csvStream.pipe(res as unknown as NodeJS.WritableStream);
} catch (error) {
res
.status(error.status || HttpStatus.INTERNAL_SERVER_ERROR)
.json({ message: error.message || 'Failed to generate CSV file.' });
}
}
}