64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { EventoParticipante } from 'src/evento-participante/evento-participante/eventoParticipante.entity';
|
|
import { Participante } from 'src/participante/participante.entity';
|
|
import { Repository } from 'typeorm';
|
|
import { actualizarEventoDto } from './dto/actualizarEvento.dto';
|
|
import { crearEventoDto } from './dto/crearEvento.dto';
|
|
import { Evento } from './evento.entity';
|
|
|
|
@Injectable()
|
|
export class EventosService {
|
|
constructor(
|
|
@InjectRepository(Evento) private eventoRepository: Repository<Evento>,
|
|
@InjectRepository(EventoParticipante)
|
|
private eventoParticipanteRepository: Repository<EventoParticipante>,
|
|
) {}
|
|
|
|
getEventos() {
|
|
return this.eventoRepository.find();
|
|
}
|
|
|
|
getEventosPorId(id: number) {
|
|
return this.eventoRepository.findOne({
|
|
where: {
|
|
id_evento: id,
|
|
},
|
|
});
|
|
}
|
|
|
|
postEvento(evento: crearEventoDto) {
|
|
const nuevoEvento = this.eventoRepository.create(evento);
|
|
return this.eventoRepository.save(nuevoEvento);
|
|
}
|
|
|
|
updateEvento(id: number, actualizacion: actualizarEventoDto) {
|
|
return this.eventoRepository.update({ id_evento: id }, actualizacion);
|
|
}
|
|
|
|
deleteParticipantes(id: number) {
|
|
this.eventoParticipanteRepository.delete({ id_evento: id });
|
|
}
|
|
|
|
deleteEvento(id: number) {
|
|
this.eventoRepository.delete({ id_evento: id });
|
|
}
|
|
|
|
async deleteEventoParticipante(id: number) {
|
|
const queryRunner =
|
|
this.eventoRepository.manager.connection.createQueryRunner();
|
|
await queryRunner.connect();
|
|
await queryRunner.startTransaction();
|
|
try {
|
|
await this.deleteParticipantes(id);
|
|
await this.deleteEvento(id);
|
|
await queryRunner.commitTransaction();
|
|
} catch (err) {
|
|
await queryRunner.rollbackTransaction();
|
|
throw err;
|
|
} finally {
|
|
await queryRunner.release();
|
|
}
|
|
}
|
|
}
|