forked from CIDWA/cedetec_api_nest
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
ParseIntPipe,
|
|
Post,
|
|
Res,
|
|
UploadedFile,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import { CarruselService } from './carrusel.service';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { diskStorage } from 'multer';
|
|
import { actualizarContador, contador } from './utils';
|
|
import * as path from 'path';
|
|
import { carruselDto } from './dto/carruselDto.dto';
|
|
|
|
@Controller('carrusel')
|
|
export class CarruselController {
|
|
constructor(private carruselService: CarruselService) {}
|
|
|
|
@Post('addImage')
|
|
@UseInterceptors(
|
|
FileInterceptor('imagen', {
|
|
storage: diskStorage({
|
|
destination: './public/carrusel',
|
|
filename: function (req, file, cb) {
|
|
actualizarContador();
|
|
const { ext } = path.parse(file.originalname);
|
|
const filename = `carrusel${contador}${ext}`;
|
|
return cb(null, filename);
|
|
},
|
|
}),
|
|
fileFilter: function (req, file, cb) {
|
|
if (!file.originalname.match(/\.(jpg|jpeg|png|webp)$/)) {
|
|
return {
|
|
message:
|
|
'Lo sentimos los archivos permitidos para usar el carrusel son jpg jpeg png webp',
|
|
};
|
|
}
|
|
cb(null, true);
|
|
},
|
|
}),
|
|
)
|
|
addImagenToCarrusel(@Body() carruselDto: carruselDto, @UploadedFile() file) {
|
|
return this.carruselService.addImagenToCarrusel(carruselDto, file.filename);
|
|
}
|
|
|
|
@Delete('delete/:id')
|
|
deleteImagenByRuta(@Param('id', ParseIntPipe) id: number) {
|
|
return this.carruselService.deleteImagenByRuta(id);
|
|
}
|
|
|
|
@Get('imagenes')
|
|
getAllImagenes() {
|
|
return this.carruselService.getAllImages();
|
|
}
|
|
}
|