Files
pcpuma_unam_api/src/programa/programa.service.ts
T
2022-05-13 18:07:51 -05:00

42 lines
1.3 KiB
TypeScript

import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Programa } from './entity/programa.entity';
@Injectable()
export class ProgramaService {
constructor(
@InjectRepository(Programa) private repository: Repository<Programa>,
) {}
create(programa: string) {
return this.repository
.findOne({ programa })
.then((existePrograma) => {
if (existePrograma)
throw new ConflictException('Ya existe este programa.');
return this.repository.save(this.repository.create({ programa }));
})
.then((_) => ({ message: 'Se creo correctamente el programa.' }));
}
findAll() {
return this.repository.find();
}
findById(id_programa: number) {
return this.repository.findOne({ id_programa }).then((programa) => {
if(!programa) throw new NotFoundException('No existe este programa')
return programa
})
}
findByPrograma(programa: string, validarNoExiste = true) {
return this.repository.findOne({ programa }).then((programa) => {
if (validarNoExiste && !programa)
throw new NotFoundException('No existe este usuario.');
return programa;
});
}
}