diff --git a/src/carrera-programa/carrera-programa.controller.ts b/src/carrera-programa/carrera-programa.controller.ts index c07e305..8c26f8a 100644 --- a/src/carrera-programa/carrera-programa.controller.ts +++ b/src/carrera-programa/carrera-programa.controller.ts @@ -1,19 +1,27 @@ -import { Controller, Delete, Get, Post, Put } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Post, Put } from '@nestjs/common'; import { CarreraProgramaService } from './carrera-programa.service'; +import { CarreraProgramaCreateDto } from './dto/carrera-programa-create.dto' +import { CarreraProgramaUpdateDto } from './dto/carrera-programa-update.dto' @Controller('carrera-programa') export class CarreraProgramaController { constructor(private carreraProgramaService: CarreraProgramaService) {} @Post() - create() {} + create(@Body() body: CarreraProgramaCreateDto) { + return this.carreraProgramaService.create(body.id_carrera, body.id_programa) + } @Delete() delete() {} @Get() - get() {} + get() { + return this.carreraProgramaService.findAll() + } @Put() - update() {} + update(@Body() body: CarreraProgramaUpdateDto) { + return this.carreraProgramaService.update(body) + } } diff --git a/src/carrera-programa/carrera-programa.service.ts b/src/carrera-programa/carrera-programa.service.ts index f1d7ed9..8e044f8 100644 --- a/src/carrera-programa/carrera-programa.service.ts +++ b/src/carrera-programa/carrera-programa.service.ts @@ -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, - ) {} + 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) { + 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" + })) + } } diff --git a/src/carrera-programa/dto/carrera-programa-create.dto.ts b/src/carrera-programa/dto/carrera-programa-create.dto.ts new file mode 100644 index 0000000..f10a54f --- /dev/null +++ b/src/carrera-programa/dto/carrera-programa-create.dto.ts @@ -0,0 +1,9 @@ +import {IsNumber} from 'class-validator'; + +export class CarreraProgramaCreateDto { + @IsNumber() + id_carrera: number; + + @IsNumber() + id_programa: number; +} diff --git a/src/carrera-programa/dto/carrera-programa-update.dto.ts b/src/carrera-programa/dto/carrera-programa-update.dto.ts new file mode 100644 index 0000000..05b1891 --- /dev/null +++ b/src/carrera-programa/dto/carrera-programa-update.dto.ts @@ -0,0 +1,12 @@ +import {IsNumber} from 'class-validator'; + +export class CarreraProgramaUpdateDto { + @IsNumber() + id_carrera_programa: number; + + @IsNumber() + id_carrera: number; + + @IsNumber() + id_programa: number; +} diff --git a/src/programa/dto/programa.dto.ts b/src/programa/dto/programa.dto.ts new file mode 100644 index 0000000..f0de1de --- /dev/null +++ b/src/programa/dto/programa.dto.ts @@ -0,0 +1,6 @@ +import { IsOptional, IsString } from 'class-validator'; + +export class ProgramaDto { + @IsString() + id_programa: string; +} diff --git a/src/programa/programa.controller.ts b/src/programa/programa.controller.ts index 9e56a9a..346e428 100644 --- a/src/programa/programa.controller.ts +++ b/src/programa/programa.controller.ts @@ -1,6 +1,7 @@ -import { Body, Controller, Get, Post } from '@nestjs/common'; +import { Body, Controller, Get, Post, Query } from '@nestjs/common'; import { ProgramaService } from './programa.service'; import { ProgramaCreateDto } from './dto/programa-create.dto'; +import { ProgramaDto } from './dto/programa.dto' @Controller('programa') export class ProgramaController { @@ -15,4 +16,9 @@ export class ProgramaController { get() { return this.programaService.findAll(); } + + @Get('programa') + programa(@Query() query: ProgramaDto) { + return this.programaService.findById(Number(query.id_programa)) + } } diff --git a/src/programa/programa.service.ts b/src/programa/programa.service.ts index a08de13..8bfb62c 100644 --- a/src/programa/programa.service.ts +++ b/src/programa/programa.service.ts @@ -1,4 +1,4 @@ -import { ConflictException, Injectable } from '@nestjs/common'; +import { ConflictException, Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Programa } from './entity/programa.entity'; @@ -23,4 +23,11 @@ export class ProgramaService { 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 + }) + } }