Files
formularios_api/src/evento/evento.service.ts
T

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-04-01 15:07:11 -06:00
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Evento } from './evento.entity';
import { CreateEventoDto } from './dto/create-evento.dto';
import { UpdateEventoDto } from './dto/update.evento.dto';
@Injectable()
export class EventoService {
constructor(
@InjectRepository(Evento) private eventoRepository: Repository<Evento>,
) {}
async createEvento(evento: CreateEventoDto) {
const eventoFound = await this.eventoRepository.findOne({
where: {
nombre_evento: evento.nombre_evento,
tipo_evento: evento.tipo_evento
}
})
if (eventoFound)
return new HttpException('Evento already exists', HttpStatus.CONFLICT)
const createEvento = this.eventoRepository.create(evento)
return this.eventoRepository.save(createEvento)
}
getEventos() {
return this.eventoRepository.find({
relations: ['participantes']
})
}
async getEvento(id_evento: number) {
const eventoFound = await this.eventoRepository.findOne({
where: {
id_evento
},
relations: ['participantes']
})
if (!eventoFound)
return new HttpException('Evento not found', HttpStatus.NOT_FOUND);
return eventoFound
}
async deleteEvento(id_evento: number) {
const result = await this.eventoRepository.delete({ id_evento })
if (result.affected === 0) {
return new HttpException('Evento not found', HttpStatus.NOT_FOUND);
}
return result
}
async updateEvento(id_evento: number, evento: UpdateEventoDto) {
const eventoFound = await this.eventoRepository.findOne({
where: {
id_evento
}
})
if (!eventoFound)
return new HttpException('Evento not found', HttpStatus.NOT_FOUND)
const updateEvento = Object.assign(eventoFound, evento)
return this.eventoRepository.save(updateEvento)
}
}