|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Get, |
| 4 | + Post, |
| 5 | + Body, |
| 6 | + Patch, |
| 7 | + Param, |
| 8 | + Delete, |
| 9 | + Query, |
| 10 | + UseGuards, |
| 11 | + HttpStatus, |
| 12 | +} from '@nestjs/common'; |
| 13 | +import { |
| 14 | + ApiTags, |
| 15 | + ApiOperation, |
| 16 | + ApiResponse, |
| 17 | + ApiBearerAuth, |
| 18 | + ApiParam, |
| 19 | + ApiQuery, |
| 20 | +} from '@nestjs/swagger'; |
| 21 | +import { AssetsService } from './assets.service'; |
| 22 | +import { CreateAssetDto } from './dto/create-asset.dto'; |
| 23 | +import { UpdateAssetDto } from './dto/update-asset.dto'; |
| 24 | +import { PaginationDto } from '../common/dto/pagination.dto'; |
| 25 | +import { JwtAuthGuard } from '../auth/jwt-auth.guard'; |
| 26 | +import { RolesGuard } from '../auth/roles.guard'; |
| 27 | +import { Roles } from '../auth/roles.decorator'; |
| 28 | +import { UserRole } from '../users/entities/user.entity'; |
| 29 | + |
| 30 | +@ApiTags('Assets') |
| 31 | +@Controller('assets') |
| 32 | +@UseGuards(JwtAuthGuard, RolesGuard) |
| 33 | +@ApiBearerAuth('JWT-auth') |
| 34 | +export class AssetsController { |
| 35 | + constructor(private readonly assetsService: AssetsService) {} |
| 36 | + |
| 37 | + @Post() |
| 38 | + @Roles(UserRole.ADMIN) |
| 39 | + @ApiOperation({ summary: 'Create a new asset (ADMIN only)' }) |
| 40 | + @ApiResponse({ |
| 41 | + status: HttpStatus.CREATED, |
| 42 | + description: 'Asset successfully created', |
| 43 | + }) |
| 44 | + @ApiResponse({ |
| 45 | + status: HttpStatus.BAD_REQUEST, |
| 46 | + description: 'Invalid input data', |
| 47 | + }) |
| 48 | + create(@Body() createAssetDto: CreateAssetDto) { |
| 49 | + return this.assetsService.create(createAssetDto); |
| 50 | + } |
| 51 | + |
| 52 | + @Get() |
| 53 | + @ApiOperation({ summary: 'Get all assets with pagination' }) |
| 54 | + @ApiQuery({ name: 'page', required: false, type: Number }) |
| 55 | + @ApiQuery({ name: 'limit', required: false, type: Number }) |
| 56 | + @ApiResponse({ |
| 57 | + status: HttpStatus.OK, |
| 58 | + description: 'Return paginated assets', |
| 59 | + }) |
| 60 | + findAll(@Query() paginationDto: PaginationDto) { |
| 61 | + return this.assetsService.findAll(paginationDto); |
| 62 | + } |
| 63 | + |
| 64 | + @Get(':id') |
| 65 | + @ApiOperation({ summary: 'Get an asset by ID' }) |
| 66 | + @ApiParam({ name: 'id', description: 'Asset ID' }) |
| 67 | + @ApiResponse({ |
| 68 | + status: HttpStatus.OK, |
| 69 | + description: 'Return the asset', |
| 70 | + }) |
| 71 | + @ApiResponse({ |
| 72 | + status: HttpStatus.NOT_FOUND, |
| 73 | + description: 'Asset not found', |
| 74 | + }) |
| 75 | + findOne(@Param('id') id: string) { |
| 76 | + return this.assetsService.findOne(id); |
| 77 | + } |
| 78 | + |
| 79 | + @Patch(':id') |
| 80 | + @Roles(UserRole.ADMIN) |
| 81 | + @ApiOperation({ summary: 'Update an asset (ADMIN only)' }) |
| 82 | + @ApiParam({ name: 'id', description: 'Asset ID' }) |
| 83 | + @ApiResponse({ |
| 84 | + status: HttpStatus.OK, |
| 85 | + description: 'Asset successfully updated', |
| 86 | + }) |
| 87 | + @ApiResponse({ |
| 88 | + status: HttpStatus.BAD_REQUEST, |
| 89 | + description: 'Invalid input data', |
| 90 | + }) |
| 91 | + @ApiResponse({ |
| 92 | + status: HttpStatus.NOT_FOUND, |
| 93 | + description: 'Asset not found', |
| 94 | + }) |
| 95 | + update(@Param('id') id: string, @Body() updateAssetDto: UpdateAssetDto) { |
| 96 | + return this.assetsService.update(id, updateAssetDto); |
| 97 | + } |
| 98 | + |
| 99 | + @Delete(':id') |
| 100 | + @Roles(UserRole.ADMIN) |
| 101 | + @ApiOperation({ summary: 'Delete an asset (ADMIN only)' }) |
| 102 | + @ApiParam({ name: 'id', description: 'Asset ID' }) |
| 103 | + @ApiResponse({ |
| 104 | + status: HttpStatus.NO_CONTENT, |
| 105 | + description: 'Asset successfully deleted', |
| 106 | + }) |
| 107 | + @ApiResponse({ |
| 108 | + status: HttpStatus.NOT_FOUND, |
| 109 | + description: 'Asset not found', |
| 110 | + }) |
| 111 | + remove(@Param('id') id: string) { |
| 112 | + return this.assetsService.remove(id); |
| 113 | + } |
| 114 | +} |
0 commit comments