From 6d3a5270eae413f67e16b2d835b3b06222091b61 Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Mon, 30 May 2022 21:28:05 -0500 Subject: [PATCH] =?UTF-8?q?carrera=20programa=20documentaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carrera-programa.controller.ts | 59 +++++++++++++------ .../carrera-programa.module.ts | 2 + .../carrera-programa.service.ts | 51 +++++++++++----- .../dto/carrera-programa-create.dto.ts | 8 +-- .../dto/carrera-programa-delete.dto.ts | 6 ++ .../dto/carrera-programa-update.dto.ts | 13 ---- .../entity/carrera-programa.entity.ts | 5 +- 7 files changed, 92 insertions(+), 52 deletions(-) create mode 100644 src/carrera-programa/dto/carrera-programa-delete.dto.ts delete mode 100644 src/carrera-programa/dto/carrera-programa-update.dto.ts diff --git a/src/carrera-programa/carrera-programa.controller.ts b/src/carrera-programa/carrera-programa.controller.ts index f6046ff..0fd88ae 100644 --- a/src/carrera-programa/carrera-programa.controller.ts +++ b/src/carrera-programa/carrera-programa.controller.ts @@ -1,38 +1,59 @@ -import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Post, Query } from '@nestjs/common'; +import { ApiBody, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'; import { CarreraProgramaService } from './carrera-programa.service'; import { CarreraProgramaCreateDto } from './dto/carrera-programa-create.dto'; -import { CarreraProgramaUpdateDto } from './dto/carrera-programa-update.dto'; -import {ApiOperation, ApiTags} from '@nestjs/swagger' +import { CarreraProgramaDeleteDto } from './dto/carrera-programa-delete.dto'; +import { IdInstitucionDto } from '../dto/id-institucion.dto'; @Controller('carrera-programa') -// @ApiTags('carrera-programa') +@ApiTags('carrera-programa') export class CarreraProgramaController { constructor(private carreraProgramaService: CarreraProgramaService) {} @Post() - // @ApiOperation({ - // description: 'Creamos una carrera programa, pasandole los parametros id_carrera y id_programa' - // }) + @ApiOperation({ + description: + 'Endpoint que crea una asociación entra una carrera y un programa.', + }) + @ApiBody({ + description: 'Ambas variables son obligatorios.', + examples: { + ejemplo: { value: { id_institucion_carrera: 36, id_programa: 1 } }, + }, + }) create(@Body() body: CarreraProgramaCreateDto) { return this.carreraProgramaService.create( - body.id_carrera, + body.id_institucion_carrera, body.id_programa, ); } - @Delete(':id') - // @ApiOperation({ - // description: 'Borramos una carrera programa a partir del id (id_carrera_programa)' - // }) - delete(@Param('id') id_carrera_programa: number) { - return this.carreraProgramaService.delete(id_carrera_programa) + @Delete() + @ApiOperation({ + description: + 'Endpoint que borra la asociación entre una carrera y un programa.', + }) + @ApiBody({ + description: '', + examples: { ejemplo: { value: { id_carrera_programa: 1 } } }, + }) + delete(@Body() body: CarreraProgramaDeleteDto) { + return this.carreraProgramaService.delete(body.id_carrera_programa); } @Get() - // @ApiOperation({ - // description: 'Nos trae todas las carrera-programa' - // }) - get() { - return this.carreraProgramaService.findAll(); + @ApiOperation({ + description: + 'Endpoint que retorna todos los programas reservados para una carrera de una institución.', + }) + @ApiQuery({ + description: 'Id de la institución.', + name: 'id_institucion', + type: 'string', + }) + get(@Query() query: IdInstitucionDto) { + return this.carreraProgramaService.findByIdInstitucion( + parseInt(query.id_institucion), + ); } } diff --git a/src/carrera-programa/carrera-programa.module.ts b/src/carrera-programa/carrera-programa.module.ts index 67c313b..4102833 100644 --- a/src/carrera-programa/carrera-programa.module.ts +++ b/src/carrera-programa/carrera-programa.module.ts @@ -3,11 +3,13 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { CarreraProgramaController } from './carrera-programa.controller'; import { CarreraProgramaService } from './carrera-programa.service'; import { CarreraPrograma } from './entity/carrera-programa.entity'; +import { InstitucionModule } from 'src/institucion/institucion.module'; import { InstitucionCarreraModule } from '../institucion-carrera/institucion-carrera.module'; import { ProgramaModule } from '../programa/programa.module'; @Module({ imports: [ + InstitucionModule, InstitucionCarreraModule, TypeOrmModule.forFeature([CarreraPrograma]), ProgramaModule, diff --git a/src/carrera-programa/carrera-programa.service.ts b/src/carrera-programa/carrera-programa.service.ts index bb8c8bb..e8a75d0 100644 --- a/src/carrera-programa/carrera-programa.service.ts +++ b/src/carrera-programa/carrera-programa.service.ts @@ -6,6 +6,7 @@ import { import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { CarreraPrograma } from './entity/carrera-programa.entity'; +import { InstitucionService } from '../institucion/institucion.service'; import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service'; import { ProgramaService } from '../programa/programa.service'; @@ -14,6 +15,7 @@ export class CarreraProgramaService { constructor( @InjectRepository(CarreraPrograma) private repository: Repository, + private institucionService: InstitucionService, private institucionCarreraService: InstitucionCarreraService, private programaService: ProgramaService, ) {} @@ -23,25 +25,22 @@ export class CarreraProgramaService { 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', + 'Ya existe una carrera programa con este nombre, intente con uno diferente.', ); - return this.repository.save(nuevoCarreraPrograma); + return this.repository.save( + this.repository.create({ + carrera, + programa, + }), + ); }) - .then(() => ({ message: 'se creo correctamente la carrera programa' })); - } - - findAll() { - return this.repository.find(); + .then(() => ({ message: 'Se creó correctamente la carrera programa.' })); } findById(id_carrera_programa: number) { @@ -49,15 +48,37 @@ export class CarreraProgramaService { .findOne({ id_carrera_programa }) .then((carreraPrograma) => { if (!carreraPrograma) - throw new NotFoundException('No existe esta carrera-programa'); + throw new NotFoundException('No existe esta carrera programa.'); return carreraPrograma; }); } + findByIdInstitucion(id_institucion: number) { + return this.institucionService + .findById(id_institucion) + .then((institucion) => + this.repository.find({ + join: { + alias: 'cp', + innerJoinAndSelect: { + ic: 'cp.carrera', + c: 'ic.carrera', + i: 'ic.institucion', + }, + }, + where: { + carrera: { institucion }, + }, + }), + ); + } + //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); - }); + return this.findById(id_carrera_programa) + .then((carreraPrograma) => this.repository.delete(carreraPrograma)) + .then((_) => ({ + message: 'Se eliminó 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 index 6905da5..e958533 100644 --- a/src/carrera-programa/dto/carrera-programa-create.dto.ts +++ b/src/carrera-programa/dto/carrera-programa-create.dto.ts @@ -1,9 +1,9 @@ -import { IsNumber } from 'class-validator'; +import { IsInt } from 'class-validator'; export class CarreraProgramaCreateDto { - @IsNumber() - id_carrera: number; + @IsInt() + id_institucion_carrera: number; - @IsNumber() + @IsInt() id_programa: number; } diff --git a/src/carrera-programa/dto/carrera-programa-delete.dto.ts b/src/carrera-programa/dto/carrera-programa-delete.dto.ts new file mode 100644 index 0000000..025d7f7 --- /dev/null +++ b/src/carrera-programa/dto/carrera-programa-delete.dto.ts @@ -0,0 +1,6 @@ +import { IsInt } from 'class-validator'; + +export class CarreraProgramaDeleteDto { + @IsInt() + id_carrera_programa: number; +} diff --git a/src/carrera-programa/dto/carrera-programa-update.dto.ts b/src/carrera-programa/dto/carrera-programa-update.dto.ts deleted file mode 100644 index ec7f798..0000000 --- a/src/carrera-programa/dto/carrera-programa-update.dto.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { IsNumber } from 'class-validator'; - -export class CarreraProgramaUpdateDto { - @IsNumber() - id_carrera_programa: number; - - // Falta IsOptional para este y el de abajo - @IsNumber() - id_carrera: number; - - @IsNumber() - id_programa: number; -} diff --git a/src/carrera-programa/entity/carrera-programa.entity.ts b/src/carrera-programa/entity/carrera-programa.entity.ts index 9654ddb..7ee31db 100644 --- a/src/carrera-programa/entity/carrera-programa.entity.ts +++ b/src/carrera-programa/entity/carrera-programa.entity.ts @@ -10,11 +10,14 @@ export class CarreraPrograma { @ManyToOne( () => InstitucionCarrera, (institucionCarrera) => institucionCarrera.programas, + { eager: true }, ) @JoinColumn({ name: 'id_institucion_carrera' }) carrera: InstitucionCarrera; - @ManyToOne(() => Programa, (programa) => programa.carrerasPrograma) + @ManyToOne(() => Programa, (programa) => programa.carrerasPrograma, { + eager: true, + }) @JoinColumn({ name: 'id_programa' }) programa: Programa; }