2022-05-31 14:26:57 -05:00
|
|
|
import {
|
|
|
|
|
ConflictException,
|
|
|
|
|
Injectable,
|
|
|
|
|
NotFoundException,
|
|
|
|
|
} from '@nestjs/common';
|
2022-05-09 16:49:41 -05:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
import { Repository } from 'typeorm';
|
|
|
|
|
import { InstitucionPrograma } from './entity/institucion-programa.entity';
|
2022-05-31 14:26:57 -05:00
|
|
|
import { Programa } from './entity/programa.entity';
|
2022-05-09 16:49:41 -05:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class InstitucionProgramaService {
|
|
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(InstitucionPrograma)
|
2022-05-31 14:26:57 -05:00
|
|
|
private institucionProgramaRepository: Repository<InstitucionPrograma>,
|
|
|
|
|
@InjectRepository(Programa)
|
|
|
|
|
private programaRepository: Repository<Programa>,
|
2022-05-09 16:49:41 -05:00
|
|
|
) {}
|
2022-05-31 14:26:57 -05:00
|
|
|
|
|
|
|
|
create(programa: string) {
|
|
|
|
|
return this.programaRepository
|
|
|
|
|
.findOne({ programa })
|
|
|
|
|
.then((existePrograma) => {
|
|
|
|
|
if (existePrograma)
|
|
|
|
|
throw new ConflictException('Ya existe este programa.');
|
|
|
|
|
return this.programaRepository.save(
|
|
|
|
|
this.programaRepository.create({ programa }),
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.then((_) => ({ message: 'Se creo correctamente el programa.' }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findAll() {
|
|
|
|
|
return this.programaRepository.find();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findById(id_programa: number) {
|
|
|
|
|
return this.programaRepository.findOne({ id_programa }).then((programa) => {
|
|
|
|
|
if (!programa) throw new NotFoundException('No existe este programa.');
|
|
|
|
|
return programa;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findByPrograma(programa: string, validarNoExiste = true) {
|
|
|
|
|
return this.programaRepository.findOne({ programa }).then((programa) => {
|
|
|
|
|
if (validarNoExiste && !programa)
|
|
|
|
|
throw new NotFoundException('No existe este programa.');
|
|
|
|
|
return programa;
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-09 16:49:41 -05:00
|
|
|
}
|