carrera programa documentación
This commit is contained in:
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<CarreraPrograma>,
|
||||
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.',
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import { IsInt } from 'class-validator';
|
||||
|
||||
export class CarreraProgramaDeleteDto {
|
||||
@IsInt()
|
||||
id_carrera_programa: number;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user