diff --git a/public/carrusel/carrusel2.jpg b/public/carrusel/carrusel2.jpg new file mode 100644 index 0000000..e0ca0de Binary files /dev/null and b/public/carrusel/carrusel2.jpg differ diff --git a/src/app.module.ts b/src/app.module.ts index e89639a..98eab76 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -50,6 +50,8 @@ import { CarouselController } from './carousel/carousel.controller'; import { CarouselService } from './carousel/carousel.service'; import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path'; +import { CarruselModule } from './carrusel/carrusel.module'; +import { Carrusel } from './entities/carrusel.entity'; @Module({ imports: [ @@ -93,6 +95,8 @@ import { join } from 'path'; Equipo, EquipoEvento, EquipoMiembro, + /* Agregado el 2 de junio */ + Carrusel ], synchronize: true, }), @@ -118,6 +122,7 @@ import { join } from 'path'; MulterModule.register({ dest: './uploads', // directorio de destino para guardar los archivos cargados }), + CarruselModule, ], controllers: [ AppController, diff --git a/src/carousel/carousel.controller.spec.ts b/src/carousel/carousel.controller.spec.ts deleted file mode 100644 index 8a06408..0000000 --- a/src/carousel/carousel.controller.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { CarouselController } from './carousel.controller'; - -describe('CarouselController', () => { - let controller: CarouselController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [CarouselController], - }).compile(); - - controller = module.get(CarouselController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/carousel/carousel.service.spec.ts b/src/carousel/carousel.service.spec.ts deleted file mode 100644 index 56b5090..0000000 --- a/src/carousel/carousel.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { CarouselService } from './carousel.service'; - -describe('CarouselService', () => { - let service: CarouselService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [CarouselService], - }).compile(); - - service = module.get(CarouselService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/src/carrusel/carrusel.controller.ts b/src/carrusel/carrusel.controller.ts new file mode 100644 index 0000000..26e8b0b --- /dev/null +++ b/src/carrusel/carrusel.controller.ts @@ -0,0 +1,70 @@ +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|gif)$/)) { + return cb( + new Error( + 'Lo sentimos, para mejorar la calidad de las imagenes en el carousel solo se aceptan imagenes con terminación: .jpeg .jpg .png .webp', + ), + false, + ); + } + cb(null, true); + }, + }), + ) + addImagenToCarrusel( + @Body() carruselDto: carruselDto, + @UploadedFile() file, + @Res() res, + ) { + return this.carruselService.addImagenToCarrusel( + carruselDto, + file.filename, + res, + ); + } + + @Delete('delete/:id') + deleteImagenByRuta(@Param('id', ParseIntPipe) id: number) { + return this.carruselService.deleteImagenByRuta(id); + } + + @Get("imagenes") + getAllImagenes(){ + return this.carruselService.getAllImages() + } +} diff --git a/src/carrusel/carrusel.module.ts b/src/carrusel/carrusel.module.ts new file mode 100644 index 0000000..eb603cd --- /dev/null +++ b/src/carrusel/carrusel.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { CarruselController } from './carrusel.controller'; +import { CarruselService } from './carrusel.service'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Carrusel } from 'src/entities/carrusel.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([Carrusel])], + controllers: [CarruselController], + providers: [CarruselService], +}) +export class CarruselModule {} diff --git a/src/carrusel/carrusel.service.ts b/src/carrusel/carrusel.service.ts new file mode 100644 index 0000000..2e51d15 --- /dev/null +++ b/src/carrusel/carrusel.service.ts @@ -0,0 +1,59 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Carrusel } from 'src/entities/carrusel.entity'; +import { Repository } from 'typeorm'; +import { carruselDto } from './dto/carruselDto.dto'; +import * as fs from 'fs'; +import { Response } from 'express'; + +@Injectable() +export class CarruselService { + constructor( + @InjectRepository(Carrusel) + private carruselRepository: Repository, + ) {} + + addImagenToCarrusel( + carruselDto: carruselDto, + filename, + res: Response, + ): Promise { + carruselDto.ruta = `/public/carrusel/${filename}`; + const carruselCreate = this.carruselRepository.create(carruselDto); + + return this.carruselRepository + .save(carruselCreate) + .then((carrusel) => { + res + .status(201) + .json({ message: 'Imagen agregada correctamente', carrusel }); + return carrusel; + }) + .catch((error) => { + res.status(500).json({ + message: 'Error al agregar la imagen', + error, + }); + throw error; + }); + } + + async deleteImagenByRuta(id: number) { + try { + const res = await this.carruselRepository.findOne({ + where: { id_imagen_carrusel: id }, + }); + + await fs.promises.unlink(`./${res.ruta}`); + + return this.carruselRepository.delete({ id_imagen_carrusel: id }); + } catch (error) { + console.error('Error:', error); + throw error; + } + } + + getAllImages(): Promise { + return this.carruselRepository.find() + } +} diff --git a/src/carrusel/dto/carruselDto.dto.ts b/src/carrusel/dto/carruselDto.dto.ts new file mode 100644 index 0000000..e0ff309 --- /dev/null +++ b/src/carrusel/dto/carruselDto.dto.ts @@ -0,0 +1,9 @@ +import { IsNotEmpty } from "class-validator"; + +export class carruselDto { + @IsNotEmpty() + ruta: string + + @IsNotEmpty() + link: string +} \ No newline at end of file diff --git a/src/carrusel/utils.ts b/src/carrusel/utils.ts new file mode 100644 index 0000000..8460fc1 --- /dev/null +++ b/src/carrusel/utils.ts @@ -0,0 +1,5 @@ +export let contador = 0 + +export const actualizarContador = () => { + contador++ +} \ No newline at end of file diff --git a/src/entities/carrusel.entity.ts b/src/entities/carrusel.entity.ts new file mode 100644 index 0000000..7d092e0 --- /dev/null +++ b/src/entities/carrusel.entity.ts @@ -0,0 +1,15 @@ +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; + +@Entity() +export class Carrusel { + @PrimaryGeneratedColumn() + id_imagen_carrusel: number + + @Column({length: 40}) + ruta: string + /* "/public/carrusel/carrusel1.jpg" */ + + @Column({length: 60}) + link: string + /* "https://www.acatlan.unam.mx/" */ +} \ No newline at end of file