42 lines
1.3 KiB
TypeScript
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;
|
|
});
|
|
}
|
|
}
|