Merge pull request 'endpoint alumnos' (#15) from eithan into develop
Reviewed-on: #15
This commit was merged in pull request #15.
This commit is contained in:
@@ -11,6 +11,7 @@ services:
|
||||
MARIADB_DATABASE: formularios_test
|
||||
volumes:
|
||||
- mariadb_data:/var/lib/mysql
|
||||
- ./init-scripts:/docker-entrypoint-initdb.d
|
||||
restart: unless-stopped
|
||||
|
||||
api:
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
-- Crear la base de datos para el registro de alumnos
|
||||
CREATE DATABASE IF NOT EXISTS datos_feria_registro;
|
||||
USE datos_feria_registro;
|
||||
|
||||
-- Crear la tabla registro_alumno
|
||||
CREATE TABLE IF NOT EXISTS registro_alumno (
|
||||
id_ncuenta int(9) unsigned zerofill NOT NULL,
|
||||
nombre varchar(100) NOT NULL,
|
||||
apellidos varchar(120) NOT NULL,
|
||||
carrera varchar(100) NOT NULL,
|
||||
genero char(1) NOT NULL,
|
||||
PRIMARY KEY (id_ncuenta)
|
||||
);
|
||||
|
||||
-- Insertar algunos datos de ejemplo
|
||||
INSERT INTO registro_alumno (id_ncuenta, nombre, apellidos, carrera, genero) VALUES
|
||||
(000123456, 'Juan', 'Pérez López', 'Ingeniería en Computación', 'M'),
|
||||
(000789012, 'María', 'González Ramírez', 'Licenciatura en Administración', 'F'),
|
||||
(000456789, 'Carlos', 'Rodríguez Martínez', 'Ingeniería Civil', 'M'),
|
||||
(000345678, 'Ana', 'Sánchez Jiménez', 'Medicina', 'F'),
|
||||
(000901234, 'Pedro', 'Fernández Gómez', 'Arquitectura', 'M'),
|
||||
(316092940, 'Eithan', 'Fernández Gómez', 'Arquitectura', 'M');
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Controller, Get, Param, NotFoundException } from '@nestjs/common';
|
||||
import { AlumnosService } from './alumnos.service';
|
||||
import { ApiTags, ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger';
|
||||
import { AlumnoDto, ALUMNO_RESPONSES } from './alumnos.documentation';
|
||||
|
||||
@ApiTags('alumnos')
|
||||
@Controller('alumnos')
|
||||
export class AlumnosController {
|
||||
constructor(private readonly alumnosService: AlumnosService) {}
|
||||
|
||||
@Get(':cuenta')
|
||||
@ApiOperation({ summary: 'Obtener datos de un alumno por su número de cuenta' })
|
||||
@ApiParam({ name: 'cuenta', description: 'Número de cuenta del alumno' })
|
||||
@ApiResponse(ALUMNO_RESPONSES[200])
|
||||
@ApiResponse(ALUMNO_RESPONSES[404])
|
||||
@ApiResponse(ALUMNO_RESPONSES[500])
|
||||
async getAlumnoByCuenta(@Param('cuenta') cuenta: string): Promise<AlumnoDto | null> {
|
||||
const alumno = await this.alumnosService.findByCuenta(cuenta);
|
||||
if (!alumno) {
|
||||
throw new NotFoundException(`No se encontró alumno con número de cuenta ${cuenta}`);
|
||||
}
|
||||
return alumno;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class AlumnoDto {
|
||||
@ApiProperty({
|
||||
example: 123456789,
|
||||
description: 'Número de cuenta del alumno (9 dígitos)',
|
||||
})
|
||||
id_ncuenta: number;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'Juan',
|
||||
description: 'Nombre del alumno',
|
||||
})
|
||||
nombre: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'Pérez López',
|
||||
description: 'Apellidos del alumno',
|
||||
})
|
||||
apellidos: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'Ingeniería en Computación',
|
||||
description: 'Carrera que cursa el alumno',
|
||||
})
|
||||
carrera: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'M',
|
||||
description: 'Género del alumno (M: Masculino, F: Femenino)',
|
||||
enum: ['M', 'F'],
|
||||
})
|
||||
genero: string;
|
||||
}
|
||||
|
||||
export const ALUMNO_RESPONSES = {
|
||||
200: {
|
||||
description: 'Datos del alumno obtenidos correctamente',
|
||||
type: AlumnoDto,
|
||||
},
|
||||
404: {
|
||||
description: 'Alumno no encontrado',
|
||||
},
|
||||
500: {
|
||||
description: 'Error interno del servidor',
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AlumnosController } from './alumnos.controller';
|
||||
import { AlumnosService } from './alumnos.service';
|
||||
import { RegistroAlumno } from './entities/registro-alumno.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([RegistroAlumno], 'alumnosConnection'),
|
||||
TypeOrmModule.forRoot({
|
||||
name: 'alumnosConnection',
|
||||
type: 'mysql',
|
||||
host: process.env.ALUMNOS_DB_HOST || 'mariadb',
|
||||
port: parseInt(process.env.ALUMNOS_DB_PORT || '3306'),
|
||||
username: process.env.ALUMNOS_DB_USERNAME || 'alumnosuser',
|
||||
password: process.env.ALUMNOS_DB_PASSWORD || 'alumnospass',
|
||||
database: process.env.ALUMNOS_DB_DATABASE || 'datos_feria_registro',
|
||||
entities: [RegistroAlumno],
|
||||
synchronize: false,
|
||||
}),
|
||||
],
|
||||
controllers: [AlumnosController],
|
||||
providers: [AlumnosService],
|
||||
exports: [AlumnosService],
|
||||
})
|
||||
export class AlumnosModule {}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { RegistroAlumno } from './entities/registro-alumno.entity';
|
||||
|
||||
@Injectable()
|
||||
export class AlumnosService {
|
||||
constructor(
|
||||
@InjectRepository(RegistroAlumno, 'alumnosConnection')
|
||||
private registroAlumnoRepository: Repository<RegistroAlumno>,
|
||||
) {}
|
||||
|
||||
async findByCuenta(cuenta: string): Promise<RegistroAlumno | null> {
|
||||
return this.registroAlumnoRepository.findOne({
|
||||
where: { id_ncuenta: parseInt(cuenta, 10) },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Entity, Column, PrimaryColumn } from 'typeorm';
|
||||
|
||||
@Entity('registro_alumno')
|
||||
export class RegistroAlumno {
|
||||
@PrimaryColumn({ type: 'int', unsigned: true, zerofill: true, width: 9 })
|
||||
id_ncuenta: number;
|
||||
|
||||
@Column({ type: 'varchar', length: 100 })
|
||||
nombre: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 120 })
|
||||
apellidos: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 100 })
|
||||
carrera: string;
|
||||
|
||||
@Column({ type: 'char', length: 1 })
|
||||
genero: string;
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import { AdministradorModule } from './administrador/administrador.module';
|
||||
import { AsistenciaModule } from './asistencia/asistencia.module';
|
||||
import { ParticipanteEventoModule } from './participante_evento/participante_evento.module';
|
||||
import { ValidacionesModule } from './validaciones/validaciones.module';
|
||||
import { AlumnosModule } from './alumnos/alumnos.module';
|
||||
|
||||
|
||||
@Module({
|
||||
@@ -79,6 +80,7 @@ import { ValidacionesModule } from './validaciones/validaciones.module';
|
||||
AsistenciaModule,
|
||||
ParticipanteEventoModule,
|
||||
ValidacionesModule,
|
||||
AlumnosModule,
|
||||
],
|
||||
|
||||
controllers: [AppController],
|
||||
|
||||
Reference in New Issue
Block a user