|
| 1 | +import { Body, Controller, Delete, Get, HttpStatus, Param, ParseFilePipe, Patch, Post, Query, Res, UploadedFile, UseInterceptors } from '@nestjs/common' |
| 2 | +import { FileInterceptor } from '@nestjs/platform-express' |
| 3 | +import { ApiParam, ApiTags } from '@nestjs/swagger' |
| 4 | +import { FilterOptions, FilterSchema, ObjectIdValidationPipe, SearchFilterOptions, SearchFilterSchema } from '@the-software-compagny/nestjs_module_restools' |
| 5 | +import { Response } from 'express' |
| 6 | +import { Types } from 'mongoose' |
| 7 | +import { AbstractController } from '~/_common/abstracts/abstract.controller' |
| 8 | +import { ApiCreateDecorator } from '~/_common/decorators/api-create.decorator' |
| 9 | +import { ApiDeletedResponseDecorator } from '~/_common/decorators/api-deleted-response.decorator' |
| 10 | +import { ApiPaginatedDecorator } from '~/_common/decorators/api-paginated.decorator' |
| 11 | +import { ApiReadResponseDecorator } from '~/_common/decorators/api-read-response.decorator' |
| 12 | +import { ApiUpdateDecorator } from '~/_common/decorators/api-update.decorator' |
| 13 | +import { PickProjectionHelper } from '~/_common/helpers/pick-projection.helper' |
| 14 | +import { PartialProjectionType } from '~/_common/types/partial-projection.type' |
| 15 | +import { FilestorageCreateDto, FilestorageDto, FilestorageUpdateDto } from './_dto/filestorage.dto' |
| 16 | +import { TransformersFilestorageService } from './_services/transformers-filestorage.service' |
| 17 | +import { FilestorageService } from './filestorage.service' |
| 18 | + |
| 19 | +@ApiTags('core') |
| 20 | +@Controller('filestorage') |
| 21 | +export class FilestorageController extends AbstractController { |
| 22 | + protected static readonly projection: PartialProjectionType<FilestorageDto> = { |
| 23 | + type: 1, |
| 24 | + namespace: 1, |
| 25 | + path: 1, |
| 26 | + hidden: 1, |
| 27 | + } |
| 28 | + |
| 29 | + public constructor(private readonly _service: FilestorageService, private readonly transformerService: TransformersFilestorageService) { |
| 30 | + super() |
| 31 | + } |
| 32 | + |
| 33 | + @Post() |
| 34 | + @UseInterceptors(FileInterceptor('file')) |
| 35 | + @ApiCreateDecorator(FilestorageCreateDto, FilestorageDto) |
| 36 | + public async create( |
| 37 | + @Res() res: Response, |
| 38 | + @Body() body: FilestorageCreateDto, |
| 39 | + @UploadedFile(new ParseFilePipe({ fileIsRequired: false })) file?: Express.Multer.File, |
| 40 | + ): Promise<Response> { |
| 41 | + const data = await this._service.create({ ...body, file }) |
| 42 | + return res.status(HttpStatus.CREATED).json({ |
| 43 | + statusCode: HttpStatus.CREATED, |
| 44 | + data, |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + @Get() |
| 49 | + @ApiPaginatedDecorator(PickProjectionHelper(FilestorageDto, FilestorageController.projection)) |
| 50 | + public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions): Promise<Response> { |
| 51 | + const [data, total] = await this._service.findAndCount(searchFilterSchema, FilestorageController.projection, searchFilterOptions) |
| 52 | + return res.status(HttpStatus.OK).json({ |
| 53 | + statusCode: HttpStatus.OK, |
| 54 | + total, |
| 55 | + data, |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + @Get(':_id([0-9a-fA-F]{24})') |
| 60 | + @ApiParam({ name: '_id', type: String }) |
| 61 | + @ApiReadResponseDecorator(FilestorageDto) |
| 62 | + public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> { |
| 63 | + const data = await this._service.findById(_id) |
| 64 | + return res.status(HttpStatus.OK).json({ |
| 65 | + statusCode: HttpStatus.OK, |
| 66 | + data, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + @Get('path') |
| 71 | + @ApiReadResponseDecorator(FilestorageDto) |
| 72 | + public async readPath(@Query('namespace') namespace: string, @Query('path') path: string, @Res() res: Response): Promise<Response> { |
| 73 | + const data = await this._service.findOne({ namespace, path }) |
| 74 | + return res.status(HttpStatus.OK).json({ |
| 75 | + statusCode: HttpStatus.OK, |
| 76 | + data, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + @Get(':_id([0-9a-fA-F]{24})/raw') |
| 81 | + @ApiParam({ name: '_id', type: String }) |
| 82 | + @ApiReadResponseDecorator(FilestorageDto) |
| 83 | + public async readRawData(@Res() res: Response, @Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Query('mime') mime: string = ''): Promise<void> { |
| 84 | + const [data, stream, parent] = await this._service.findByIdWithRawData(_id) |
| 85 | + await this.transformerService.transform(mime, res, data, stream, parent) |
| 86 | + } |
| 87 | + |
| 88 | + @Get('path/raw') |
| 89 | + @ApiReadResponseDecorator(FilestorageDto) |
| 90 | + public async readPathRawData(@Res() res: Response, @Query('namespace') namespace: string, @Query('path') path: string, @Query('mime') mime: string = ''): Promise<void> { |
| 91 | + const [data, stream, parent] = await this._service.findOneWithRawData({ namespace, path }) |
| 92 | + await this.transformerService.transform(mime, res, data, stream, parent) |
| 93 | + } |
| 94 | + |
| 95 | + @Patch(':_id([0-9a-fA-F]{24})') |
| 96 | + @ApiParam({ name: '_id', type: String }) |
| 97 | + @ApiUpdateDecorator(FilestorageUpdateDto, FilestorageDto) |
| 98 | + public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: FilestorageUpdateDto, @Res() res: Response): Promise<Response> { |
| 99 | + const data = await this._service.update(_id, body) |
| 100 | + return res.status(HttpStatus.OK).json({ |
| 101 | + statusCode: HttpStatus.OK, |
| 102 | + data, |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + @Delete(':_id([0-9a-fA-F]{24})') |
| 107 | + @ApiParam({ name: '_id', type: String }) |
| 108 | + @ApiDeletedResponseDecorator(FilestorageDto) |
| 109 | + public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> { |
| 110 | + const data = await this._service.delete(_id) |
| 111 | + return res.status(HttpStatus.OK).json({ |
| 112 | + statusCode: HttpStatus.OK, |
| 113 | + data, |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
0 commit comments