falta update
This commit is contained in:
@@ -16,7 +16,9 @@ export class InstitucionPrograma {
|
||||
@Column({ type: Boolean, nullable: false, default: false })
|
||||
mostrar: boolean;
|
||||
|
||||
@ManyToOne(() => Programa, (programa) => programa.instituciones)
|
||||
@ManyToOne(() => Programa, (programa) => programa.instituciones, {
|
||||
eager: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_programa' })
|
||||
programa: Programa;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common';
|
||||
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
|
||||
import { ApiBody, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
|
||||
import { InstitucionProgramaService } from './institucion-programa.service';
|
||||
import { IdInstitucionDto } from 'src/dto/id-institucion.dto';
|
||||
import { ProgramaCreateDto } from '../institucion-programa/dto/programa-create.dto';
|
||||
|
||||
@Controller('institucion-programa')
|
||||
@@ -21,6 +22,37 @@ export class InstitucionProgramaController {
|
||||
@Get()
|
||||
@ApiOperation({ description: 'Endpoint que retorna todos los programas.' })
|
||||
get() {
|
||||
return this.institucionProgramaService.findAll();
|
||||
return this.institucionProgramaService.findAllProgramas();
|
||||
}
|
||||
|
||||
@Get('programas')
|
||||
@ApiOperation({
|
||||
description: 'Endpoint que retorna todos los programas de una institución.',
|
||||
})
|
||||
@ApiQuery({
|
||||
description: 'Id de la institución.',
|
||||
name: 'id_institucion',
|
||||
type: 'string',
|
||||
})
|
||||
programas(@Query() query: IdInstitucionDto) {
|
||||
return this.institucionProgramaService.findAllByIdInstitucion(
|
||||
parseInt(query.id_institucion),
|
||||
);
|
||||
}
|
||||
|
||||
@Get('programas-mostrar')
|
||||
@ApiOperation({
|
||||
description:
|
||||
'Endpoint que retorna todos los programas de una institución que tengan el campo mostrar en true.',
|
||||
})
|
||||
@ApiQuery({
|
||||
description: 'Id de la institución.',
|
||||
name: 'id_institucion',
|
||||
type: 'string',
|
||||
})
|
||||
programasMostrar(@Query() query: IdInstitucionDto) {
|
||||
return this.institucionProgramaService.findAllByIdInstitucionMostrar(
|
||||
parseInt(query.id_institucion),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,13 @@ import { InstitucionProgramaController } from './institucion-programa.controller
|
||||
import { InstitucionProgramaService } from './institucion-programa.service';
|
||||
import { InstitucionPrograma } from './entity/institucion-programa.entity';
|
||||
import { Programa } from './entity/programa.entity';
|
||||
import { InstitucionModule } from '../institucion/institucion.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([InstitucionPrograma, Programa])],
|
||||
imports: [
|
||||
InstitucionModule,
|
||||
TypeOrmModule.forFeature([InstitucionPrograma, Programa]),
|
||||
],
|
||||
controllers: [InstitucionProgramaController],
|
||||
providers: [InstitucionProgramaService],
|
||||
exports: [InstitucionProgramaService],
|
||||
|
||||
@@ -7,6 +7,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { InstitucionPrograma } from './entity/institucion-programa.entity';
|
||||
import { Programa } from './entity/programa.entity';
|
||||
import { InstitucionService } from '../institucion/institucion.service';
|
||||
|
||||
@Injectable()
|
||||
export class InstitucionProgramaService {
|
||||
@@ -15,6 +16,7 @@ export class InstitucionProgramaService {
|
||||
private institucionProgramaRepository: Repository<InstitucionPrograma>,
|
||||
@InjectRepository(Programa)
|
||||
private programaRepository: Repository<Programa>,
|
||||
private institucionService: InstitucionService,
|
||||
) {}
|
||||
|
||||
create(programa: string) {
|
||||
@@ -27,25 +29,52 @@ export class InstitucionProgramaService {
|
||||
this.programaRepository.create({ programa }),
|
||||
);
|
||||
})
|
||||
.then((_) => ({ message: 'Se creo correctamente el programa.' }));
|
||||
.then(async (programa) => {
|
||||
const instituciones = await this.institucionService.findAll();
|
||||
|
||||
for (let i = 0; i < instituciones.length; i++)
|
||||
await this.institucionProgramaRepository.save(
|
||||
this.institucionProgramaRepository.create({
|
||||
programa,
|
||||
institucion: instituciones[i],
|
||||
}),
|
||||
);
|
||||
return { message: 'Se creó correctamente el programa.' };
|
||||
});
|
||||
}
|
||||
|
||||
findAll() {
|
||||
findAllProgramas() {
|
||||
return this.programaRepository.find();
|
||||
}
|
||||
|
||||
findById(id_programa: number) {
|
||||
findProgramaById(id_programa: number) {
|
||||
return this.programaRepository.findOne({ id_programa }).then((programa) => {
|
||||
if (!programa) throw new NotFoundException('No existe este programa.');
|
||||
return programa;
|
||||
});
|
||||
}
|
||||
|
||||
findByPrograma(programa: string, validarNoExiste = true) {
|
||||
findProgramaByPrograma(programa: string, validarNoExiste = true) {
|
||||
return this.programaRepository.findOne({ programa }).then((programa) => {
|
||||
if (validarNoExiste && !programa)
|
||||
throw new NotFoundException('No existe este programa.');
|
||||
return programa;
|
||||
});
|
||||
}
|
||||
|
||||
findAllByIdInstitucion(id_institucion: number) {
|
||||
return this.institucionService
|
||||
.findById(id_institucion)
|
||||
.then((institucion) =>
|
||||
this.institucionProgramaRepository.find({ institucion }),
|
||||
);
|
||||
}
|
||||
|
||||
findAllByIdInstitucionMostrar(id_institucion: number) {
|
||||
return this.institucionService
|
||||
.findById(id_institucion)
|
||||
.then((institucion) =>
|
||||
this.institucionProgramaRepository.find({ institucion, mostrar: true }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user