75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Participante } from './participante.entity';
|
|
import { Repository } from 'typeorm';
|
|
import { CreateParticipanteDto } from './dto/create-participante.dto';
|
|
//import { UpdateAdminDto } from 'src/admin/dto/update.admin.dto';
|
|
import { UpdateParticipanteDto } from './dto/update.participante.dto';
|
|
|
|
@Injectable()
|
|
export class ParticipanteService {
|
|
|
|
constructor(
|
|
@InjectRepository(Participante) private participanteRepository: Repository<Participante>
|
|
) {}
|
|
|
|
async createParticipante(participante: CreateParticipanteDto) {
|
|
const participanteFound = await this.participanteRepository.findOne({
|
|
where: {
|
|
correo: participante.correo
|
|
}
|
|
})
|
|
|
|
if (participanteFound)
|
|
return new HttpException('Participante already exists', HttpStatus.CONFLICT)
|
|
|
|
return this.participanteRepository.save(participante)
|
|
}
|
|
|
|
getParticipantes() {
|
|
return this.participanteRepository.find({
|
|
relations: ['tipo_user', 'participanteEventos']
|
|
})
|
|
}
|
|
|
|
async getParticipante(id_participante: number) {
|
|
const participanteFound = await this.participanteRepository.findOne({
|
|
where: {
|
|
id_participante
|
|
},
|
|
relations: ['tipo_user', 'participanteEventos']
|
|
})
|
|
|
|
if (!participanteFound)
|
|
return new HttpException('Participante not found', HttpStatus.NOT_FOUND);
|
|
|
|
return participanteFound;
|
|
}
|
|
|
|
async deleteParticipante(id_participante: number) {
|
|
const result = await this.participanteRepository.delete({ id_participante })
|
|
|
|
if (result.affected === 0) {
|
|
return new HttpException('Participante not found', HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
async updateParticipante(id_participante: number, participante: UpdateParticipanteDto) {
|
|
const participanteFound = await this.participanteRepository.findOne({
|
|
where: {
|
|
id_participante
|
|
}
|
|
});
|
|
|
|
if (!participanteFound) {
|
|
return new HttpException('Participante not found', HttpStatus.NOT_FOUND)
|
|
}
|
|
|
|
const updateParticipante = Object.assign(participanteFound, participante)
|
|
return this.participanteRepository.save(updateParticipante)
|
|
}
|
|
|
|
}
|