2022-06-08 00:55:02 +00:00
|
|
|
import { ConflictException, Injectable } from '@nestjs/common';
|
2022-06-07 21:02:09 +00:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { Repository } from 'typeorm';
|
2022-06-08 00:55:02 +00:00
|
|
|
import { Equipo } from 'src/equipo/entity/equipo.entity';
|
2022-06-07 21:02:09 +00:00
|
|
|
import { EquipoPrograma } from './entity/equipo-programa.entity';
|
2022-06-08 00:55:02 +00:00
|
|
|
import { Programa } from 'src/institucion-programa/entity/programa.entity';
|
|
|
|
import { EquipoService } from '../equipo/equipo.service';
|
|
|
|
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
2022-06-07 21:02:09 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class EquipoProgramaService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(EquipoPrograma)
|
2022-06-08 00:55:02 +00:00
|
|
|
private repository: Repository<EquipoPrograma>,
|
|
|
|
private equipoService: EquipoService,
|
|
|
|
private institucionProgramaService: InstitucionProgramaService,
|
2022-06-07 21:02:09 +00:00
|
|
|
) {}
|
2022-06-08 00:55:02 +00:00
|
|
|
|
|
|
|
async create(id_equipo: number, id_programa: number) {
|
|
|
|
const equipo = await this.equipoService.findById(id_equipo);
|
|
|
|
const programa = await this.institucionProgramaService.findProgramaById(
|
|
|
|
id_programa,
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.existeEquipoPrograma(equipo, programa).then(() =>
|
|
|
|
this.repository.save(this.repository.create({ equipo, programa })),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(id_equipo_programa: number) {
|
|
|
|
return this.findById(id_equipo_programa).then((equipoPrograma) =>
|
|
|
|
this.repository.remove(equipoPrograma),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
existeEquipoPrograma(equipo: Equipo, programa: Programa) {
|
|
|
|
return this.repository
|
|
|
|
.findOne({ equipo, programa })
|
|
|
|
.then((equipoPrograma) => {
|
|
|
|
if (equipoPrograma)
|
|
|
|
throw new ConflictException(
|
|
|
|
'Ya existe un equipo programa con este equipo y este programa.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
findById(id_equipo_programa: number) {
|
|
|
|
return this.repository
|
|
|
|
.findOne({ id_equipo_programa })
|
|
|
|
.then((equipoPrograma) => {
|
|
|
|
if (!equipoPrograma)
|
|
|
|
throw new ConflictException('No existe este equipo programa.');
|
|
|
|
return equipoPrograma;
|
|
|
|
});
|
|
|
|
}
|
2022-06-07 21:02:09 +00:00
|
|
|
}
|