From 73db06d77395895784aea2e9b3e11fea88673435 Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Tue, 19 Apr 2022 23:54:31 -0500 Subject: [PATCH] listo --- catalogos.sql | 18 +++++++++ .../dto/institucion-dia-update.dto.ts | 23 ++++++++++++ .../entity/institucion-dia.entity.ts | 10 ++--- .../institucion-dia.controller.ts | 19 ++++++++-- src/institucion-dia/institucion-dia.module.ts | 3 +- .../institucion-dia.service.ts | 37 ++++++++++++++++++- 6 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 src/institucion-dia/dto/institucion-dia-update.dto.ts diff --git a/catalogos.sql b/catalogos.sql index 74899ee..d6e1d7f 100644 --- a/catalogos.sql +++ b/catalogos.sql @@ -20,3 +20,21 @@ INSERT INTO tipo_usuario (tipo_usuario) VALUES ("Operador"); INSERT INTO tipo_usuario (tipo_usuario) VALUES ("Profesor"); INSERT INTO tipo_usuario (tipo_usuario) VALUES ("Alumno"); INSERT INTO tipo_usuario (tipo_usuario) VALUES ("Posgrado"); + +INSERT INTO dia (dia) VALUES ("Lunes"); +INSERT INTO dia (dia) VALUES ("Martes"); +INSERT INTO dia (dia) VALUES ("Miercoles"); +INSERT INTO dia (dia) VALUES ("Jueves"); +INSERT INTO dia (dia) VALUES ("Viernes"); + +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (1, 1); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (1, 2); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (1, 3); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (1, 4); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (1, 5); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (2, 1); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (2, 2); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (2, 3); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (2, 4); +INSERT INTO institucion_dia (id_institucion, id_dia) VALUES (2, 5); + diff --git a/src/institucion-dia/dto/institucion-dia-update.dto.ts b/src/institucion-dia/dto/institucion-dia-update.dto.ts new file mode 100644 index 0000000..982e3c1 --- /dev/null +++ b/src/institucion-dia/dto/institucion-dia-update.dto.ts @@ -0,0 +1,23 @@ +import { + IsBoolean, + IsMilitaryTime, + IsNumber, + IsOptional, +} from 'class-validator'; + +export class InstitucionDiaUpdateDto { + @IsNumber() + id_institucion_dia: number; + + @IsBoolean() + @IsOptional() + activo: boolean; + + @IsMilitaryTime() + @IsOptional() + hora_fin: string; + + @IsMilitaryTime() + @IsOptional() + hora_inicio: string; +} diff --git a/src/institucion-dia/entity/institucion-dia.entity.ts b/src/institucion-dia/entity/institucion-dia.entity.ts index 3b0f161..92b9b8c 100644 --- a/src/institucion-dia/entity/institucion-dia.entity.ts +++ b/src/institucion-dia/entity/institucion-dia.entity.ts @@ -18,13 +18,13 @@ export class InstitucionDia { @Column({ type: Boolean, nullable: false, default: false }) activo: boolean; - @Column({ type: Date, nullable: false }) - hora_fin: Date; + @Column({ type: String, nullable: false, default: '17:45', length: 5 }) + hora_fin: string; - @Column({ type: Date, nullable: false }) - hora_inicio: Date; + @Column({ type: String, nullable: false, default: '09:00', length: 5 }) + hora_inicio: string; - @ManyToOne(() => Dia, (dia) => dia.institucionesDias) + @ManyToOne(() => Dia, (dia) => dia.institucionesDias, { eager: true }) @JoinColumn({ name: 'id_dia' }) dia: Dia; diff --git a/src/institucion-dia/institucion-dia.controller.ts b/src/institucion-dia/institucion-dia.controller.ts index 24a6110..319ec51 100644 --- a/src/institucion-dia/institucion-dia.controller.ts +++ b/src/institucion-dia/institucion-dia.controller.ts @@ -1,13 +1,26 @@ -import { Controller, Get, Put } from '@nestjs/common'; +import { Body, Controller, Get, Put, Query } from '@nestjs/common'; import { InstitucionDiaService } from './institucion-dia.service'; +import { IdInstitucionDto } from '../dto/id-institucion.dto'; +import { InstitucionDiaUpdateDto } from './dto/institucion-dia-update.dto'; @Controller('institucion-dia') export class InstitucionDiaController { constructor(private institucionDiaService: InstitucionDiaService) {} @Get() - get() {} + get() { + return this.institucionDiaService.findAll(); + } + + @Get('instituciones_dias') + institucionesDias(@Query() query: IdInstitucionDto) { + return this.institucionDiaService.findAllByIdInstitucion( + Number(query.id_institucion), + ); + } @Put() - update() {} + update(@Body() body: InstitucionDiaUpdateDto) { + return this.institucionDiaService.update(body); + } } diff --git a/src/institucion-dia/institucion-dia.module.ts b/src/institucion-dia/institucion-dia.module.ts index 95cdb42..88c0e88 100644 --- a/src/institucion-dia/institucion-dia.module.ts +++ b/src/institucion-dia/institucion-dia.module.ts @@ -4,9 +4,10 @@ import { InstitucionDiaController } from './institucion-dia.controller'; import { InstitucionDiaService } from './institucion-dia.service'; import { InstitucionDia } from './entity/institucion-dia.entity'; import { Dia } from './entity/dia.entity'; +import { InstitucionModule } from '../institucion/institucion.module'; @Module({ - imports: [TypeOrmModule.forFeature([Dia, InstitucionDia])], + imports: [TypeOrmModule.forFeature([Dia, InstitucionDia]), InstitucionModule], controllers: [InstitucionDiaController], providers: [InstitucionDiaService], }) diff --git a/src/institucion-dia/institucion-dia.service.ts b/src/institucion-dia/institucion-dia.service.ts index 1b06475..162959a 100644 --- a/src/institucion-dia/institucion-dia.service.ts +++ b/src/institucion-dia/institucion-dia.service.ts @@ -2,11 +2,44 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { InstitucionDia } from './entity/institucion-dia.entity'; - +import { InstitucionService } from '../institucion/institucion.service'; @Injectable() export class InstitucionDiaService { constructor( @InjectRepository(InstitucionDia) - private repository: Repository, + private repositoryInstitucionDia: Repository, + private institucionService: InstitucionService, ) {} + + findAll() { + return this.repositoryInstitucionDia.find(); + } + + findAllByIdInstitucion(id_institucion: number) { + return this.institucionService + .findById(id_institucion) + .then((institucion) => + this.repositoryInstitucionDia.find({ institucion }), + ); + } + + findById(id_institucion_dia: number) { + return this.repositoryInstitucionDia + .findOne({ id_institucion_dia }) + .then((institucionDia) => { + if (!institucionDia) throw new Error('No existe esta institucion dia.'); + return institucionDia; + }); + } + + update(attrs: Partial) { + return this.findById(attrs.id_institucion_dia) + .then((institucionDia) => { + Object.assign(institucionDia, attrs); + return this.repositoryInstitucionDia.save(institucionDia); + }) + .then((_) => ({ + message: 'Se actualió correctamente la institucion infracción.', + })); + } }