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

72 lines
2.3 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-04-22 13:36:18 -05:00
import { CarreraService } from '../carrera/carrera.service';
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-04-20 23:22:34 -05:00
private carreraService: CarreraService,
private programaService: ProgramaService,
2022-04-22 13:36:18 -05:00
) {}
2022-04-20 23:22:34 -05:00
async create(id_carrera: number, id_programa: number) {
2022-04-22 13:36:18 -05:00
const carrera = await this.carreraService.findById(id_carrera);
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)
throw new NotFoundException('No existe esta carrera programa');
return carreraPrograma;
});
2022-04-20 23:22:34 -05:00
}
2022-04-22 21:59:12 -05:00
// Eliminar update, no tiene sentido tenerlo, me equivoque xd
2022-04-20 23:22:34 -05:00
async update(attrs: Partial<CarreraPrograma>) {
2022-04-22 13:36:18 -05:00
const carreraPrograma = await this.findById(attrs.id_carrera_programa);
2022-04-20 23:22:34 -05:00
return this.repository
2022-04-22 13:36:18 -05:00
.findOne({ id_carrera_programa: attrs.id_carrera_programa })
2022-04-20 23:22:34 -05:00
.then((existeCarreraPrograma) => {
if (existeCarreraPrograma)
2022-04-22 13:36:18 -05:00
throw new ConflictException('Ya existe esta carrera programa');
Object.assign(carreraPrograma, attrs);
return this.repository.save(carreraPrograma);
2022-04-20 23:22:34 -05:00
})
.then(() => ({
2022-04-22 13:36:18 -05:00
message: 'Se actualizo correctamente la carrera programa',
}));
2022-04-20 23:22:34 -05:00
}
2022-04-04 20:53:32 -05:00
}