revisar update y falta delete

This commit is contained in:
Andres2908
2022-04-20 23:22:34 -05:00
parent 8f480f1317
commit b386bd61e8
7 changed files with 106 additions and 8 deletions
@@ -1,12 +1,62 @@
import { Injectable } from '@nestjs/common';
import {ConflictException,
Injectable,
NotFoundException, } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CarreraPrograma } from './entity/carrera-programa.entity';
import { CarreraService } from '../carrera/carrera.service'
import { ProgramaService } from '../programa/programa.service'
@Injectable()
export class CarreraProgramaService {
constructor(
@InjectRepository(CarreraPrograma)
private repository: Repository<CarreraPrograma>,
) {}
private carreraService: CarreraService,
private programaService: ProgramaService,
) { }
async create(id_carrera: number, id_programa: number) {
const carrera = await this.carreraService.findById(id_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
})
}
async update(attrs: Partial<CarreraPrograma>) {
const carreraPrograma = await this.findById(attrs.id_carrera_programa)
return this.repository
.findOne({ id_carrera_programa: attrs.id_carrera_programa})
.then((existeCarreraPrograma) => {
if (existeCarreraPrograma)
throw new ConflictException('Ya existe esta carrera programa')
Object.assign(carreraPrograma, attrs)
return this.repository.save(carreraPrograma)
})
.then(() => ({
message: "Se actualizo correctamente la carrera programa"
}))
}
}