import { ConflictException, Injectable, NotFoundException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { CarreraPrograma } from './entity/carrera-programa.entity'; import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service'; import { ProgramaService } from '../programa/programa.service'; @Injectable() export class CarreraProgramaService { constructor( @InjectRepository(CarreraPrograma) private repository: Repository, private institucionCarreraService: InstitucionCarreraService, private programaService: ProgramaService, ) {} async create(id_institucion_carrera: number, id_programa: number) { const carrera = await this.institucionCarreraService.findById( id_institucion_carrera, ); const programa = await this.programaService.findById(id_programa); const nuevoCarreraPrograma = this.repository.create({ carrera, programa, }); return this.repository .findOne({ carrera, programa }) .then((existeCarretaPrograma) => { if (existeCarretaPrograma) throw new ConflictException( 'Ya existe una carrera programa con este nombre, intente con uno diferente', ); return this.repository.save(nuevoCarreraPrograma); }) .then(() => ({ message: 'se creo correctamente la carrera programa' })); } findAll() { return this.repository.find(); } findById(id_carrera_programa: number) { return this.repository .findOne({ id_carrera_programa }) .then((carreraPrograma) => { if (!carreraPrograma) throw new NotFoundException('No existe esta carrera-programa'); return carreraPrograma; }); } //No estoy seguro de que esto este bien :) delete(id_carrera_programa: number) { this.findById(id_carrera_programa).then(() => { return this.repository.delete(id_carrera_programa); }); } }