Files
pcpuma_unam_api/src/institucion-programa/institucion-programa.service.ts
T

52 lines
1.5 KiB
TypeScript
Raw Normal View History

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';
import { Programa } from './entity/programa.entity';
2022-05-09 16:49:41 -05:00
@Injectable()
export class InstitucionProgramaService {
constructor(
@InjectRepository(InstitucionPrograma)
private institucionProgramaRepository: Repository<InstitucionPrograma>,
@InjectRepository(Programa)
private programaRepository: Repository<Programa>,
2022-05-09 16:49:41 -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
}