2022-04-20 23:22:34 -05:00
|
|
|
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
2022-04-04 20:53:32 -05:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2022-04-17 23:05:18 -05:00
|
|
|
import { Repository } from 'typeorm';
|
|
|
|
|
import { Programa } from './entity/programa.entity';
|
2022-04-04 20:02:54 -05:00
|
|
|
|
|
|
|
|
@Injectable()
|
2022-04-04 20:53:32 -05:00
|
|
|
export class ProgramaService {
|
|
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(Programa) private repository: Repository<Programa>,
|
|
|
|
|
) {}
|
2022-04-20 09:48:21 -05:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2022-04-20 23:22:34 -05:00
|
|
|
|
|
|
|
|
findById(id_programa: number) {
|
|
|
|
|
return this.repository.findOne({ id_programa }).then((programa) => {
|
|
|
|
|
if(!programa) throw new NotFoundException('No existe este programa')
|
|
|
|
|
return programa
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-05-13 18:07:51 -05:00
|
|
|
|
|
|
|
|
findByPrograma(programa: string, validarNoExiste = true) {
|
|
|
|
|
return this.repository.findOne({ programa }).then((programa) => {
|
|
|
|
|
if (validarNoExiste && !programa)
|
|
|
|
|
throw new NotFoundException('No existe este usuario.');
|
|
|
|
|
return programa;
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-04-04 20:53:32 -05:00
|
|
|
}
|