Se puede agregar links al carrusel
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 185 KiB |
@@ -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,
|
||||
|
||||
@@ -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>(CarouselController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -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>(CarouselService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
@@ -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<Carrusel>,
|
||||
) {}
|
||||
|
||||
addImagenToCarrusel(
|
||||
carruselDto: carruselDto,
|
||||
filename,
|
||||
res: Response,
|
||||
): Promise<Carrusel> {
|
||||
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<Carrusel[]> {
|
||||
return this.carruselRepository.find()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IsNotEmpty } from "class-validator";
|
||||
|
||||
export class carruselDto {
|
||||
@IsNotEmpty()
|
||||
ruta: string
|
||||
|
||||
@IsNotEmpty()
|
||||
link: string
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export let contador = 0
|
||||
|
||||
export const actualizarContador = () => {
|
||||
contador++
|
||||
}
|
||||
@@ -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/" */
|
||||
}
|
||||
Reference in New Issue
Block a user