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

64 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-04-22 13:36:18 -05:00
import {
ConflictException,
2022-04-20 23:22:34 -05:00
Injectable,
2022-04-22 13:36:18 -05:00
NotFoundException,
} from '@nestjs/common';
2022-04-04 20:53:32 -05:00
import { InjectRepository } from '@nestjs/typeorm';
2022-04-17 20:47:35 -05:00
import { Repository } from 'typeorm';
2022-04-17 20:56:02 -05:00
import { CarreraPrograma } from './entity/carrera-programa.entity';
2022-05-01 17:03:37 -05:00
import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service';
2022-04-22 13:36:18 -05:00
import { ProgramaService } from '../programa/programa.service';
2022-04-04 20:02:54 -05:00
@Injectable()
2022-04-04 20:53:32 -05:00
export class CarreraProgramaService {
constructor(
@InjectRepository(CarreraPrograma)
private repository: Repository<CarreraPrograma>,
2022-05-01 17:03:37 -05:00
private institucionCarreraService: InstitucionCarreraService,
2022-04-20 23:22:34 -05:00
private programaService: ProgramaService,
2022-04-22 13:36:18 -05:00
) {}
2022-05-01 17:03:37 -05:00
async create(id_institucion_carrera: number, id_programa: number) {
const carrera = await this.institucionCarreraService.findById(
id_institucion_carrera,
);
2022-04-22 13:36:18 -05:00
const programa = await this.programaService.findById(id_programa);
2022-04-20 23:22:34 -05:00
const nuevoCarreraPrograma = this.repository.create({
2022-04-22 13:36:18 -05:00
carrera,
programa,
2022-04-20 23:22:34 -05:00
});
return this.repository
.findOne({ carrera, programa })
.then((existeCarretaPrograma) => {
if (existeCarretaPrograma)
2022-04-22 13:36:18 -05:00
throw new ConflictException(
'Ya existe una carrera programa con este nombre, intente con uno diferente',
);
return this.repository.save(nuevoCarreraPrograma);
2022-04-20 23:22:34 -05:00
})
2022-04-22 13:36:18 -05:00
.then(() => ({ message: 'se creo correctamente la carrera programa' }));
2022-04-20 23:22:34 -05:00
}
findAll() {
2022-04-22 13:36:18 -05:00
return this.repository.find();
2022-04-20 23:22:34 -05:00
}
findById(id_carrera_programa: number) {
2022-04-22 13:36:18 -05:00
return this.repository
.findOne({ id_carrera_programa })
.then((carreraPrograma) => {
if (!carreraPrograma)
2022-04-24 19:09:22 -05:00
throw new NotFoundException('No existe esta carrera-programa');
2022-04-22 13:36:18 -05:00
return carreraPrograma;
});
2022-04-20 23:22:34 -05:00
}
2022-04-24 19:09:22 -05:00
//No estoy seguro de que esto este bien :)
delete(id_carrera_programa: number) {
2022-05-01 17:03:37 -05:00
this.findById(id_carrera_programa).then(() => {
return this.repository.delete(id_carrera_programa);
});
2022-04-20 23:22:34 -05:00
}
2022-04-04 20:53:32 -05:00
}