diff --git a/.example b/.example deleted file mode 100644 index 34af4b9..0000000 --- a/.example +++ /dev/null @@ -1,7 +0,0 @@ -PORT= -JWT_SECRET= -DB_HOST= -DB_PORT= -DB_USER= -DB_PASSWORD= -DB_NAME= \ No newline at end of file diff --git a/src/alumno/dto/create-student.dto.ts b/src/alumno/dto/create-student.dto.ts index b8fcadd..130445e 100644 --- a/src/alumno/dto/create-student.dto.ts +++ b/src/alumno/dto/create-student.dto.ts @@ -1 +1,22 @@ -export class CreateStudentDto {} +import { Type } from 'class-transformer'; +import { IsDate, IsNotEmpty, IsNumber, IsString } from 'class-validator'; + +export class CreateStudentDto { + @IsNotEmpty() + @IsNumber() + id_cuenta: number; + + @IsNotEmpty() + @IsString() + nombre: string; + + @IsString() + fecha_nacimiento: string; + + @IsNotEmpty() + @IsNumber() + id_carrera: number; + + @IsString() + correo: string; +} diff --git a/src/alumno/entities/student.entity.ts b/src/alumno/entities/student.entity.ts index a375e94..e50f2d4 100644 --- a/src/alumno/entities/student.entity.ts +++ b/src/alumno/entities/student.entity.ts @@ -14,7 +14,7 @@ export class Student { length: 8, nullable: true, }) - fecha_nacimiento: string | null; + fecha_nacimiento: Date | null; @Column({ name: 'correo', type: 'varchar', length: 100, nullable: true }) correo: string | null; @@ -47,7 +47,11 @@ export class Student { @Column({ name: 'id_periodo', type: 'int', nullable: true }) id_periodo: number | null; - @Column({ name: 'fecha_registro', type: 'datetime', nullable: false }) + @Column({ + name: 'fecha_registro', + type: 'datetime', + nullable: false, + }) fecha_registro: Date; @Column({ name: 'id_carrera', type: 'int', nullable: false }) diff --git a/src/alumno/student.controller.ts b/src/alumno/student.controller.ts index 8f8e5b6..c93bc87 100644 --- a/src/alumno/student.controller.ts +++ b/src/alumno/student.controller.ts @@ -1,19 +1,28 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { + Controller, + Get, + Post, + Body, + Patch, + Param, + Delete, +} from '@nestjs/common'; import { StudentService } from './student.service'; import { CreateStudentDto } from './dto/create-student.dto'; import { UpdateStudentDto } from './dto/update-student.dto'; +import { Student } from './entities/student.entity'; @Controller('student') export class StudentController { constructor(private readonly studentService: StudentService) {} @Post() - create(@Body() createStudentDto: CreateStudentDto) { + async create(@Body() createStudentDto: CreateStudentDto): Promise { return this.studentService.create(createStudentDto); } @Get() - findAll() { + async findAll(): Promise { return this.studentService.findAll(); } diff --git a/src/alumno/student.module.ts b/src/alumno/student.module.ts index 1c61ccb..e62ebe4 100644 --- a/src/alumno/student.module.ts +++ b/src/alumno/student.module.ts @@ -1,8 +1,11 @@ import { Module } from '@nestjs/common'; import { StudentService } from './student.service'; import { StudentController } from './student.controller'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Student } from './entities/student.entity'; @Module({ + imports: [TypeOrmModule.forFeature([Student])], controllers: [StudentController], providers: [StudentService], }) diff --git a/src/alumno/student.service.ts b/src/alumno/student.service.ts index 9e81073..d14dc03 100644 --- a/src/alumno/student.service.ts +++ b/src/alumno/student.service.ts @@ -1,19 +1,34 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { CreateStudentDto } from './dto/create-student.dto'; import { UpdateStudentDto } from './dto/update-student.dto'; +import { Student } from './entities/student.entity'; +import { Repository } from 'typeorm'; +import { InjectRepository } from '@nestjs/typeorm'; @Injectable() export class StudentService { - create(createStudentDto: CreateStudentDto) { - return 'This action adds a new student'; + constructor( + @InjectRepository(Student) + private readonly studentRepository: Repository, + ) {} + + async create(createStudentDto: CreateStudentDto): Promise { + const student = this.studentRepository.create(createStudentDto); + return await this.studentRepository.save(student); } - findAll() { - return `This action returns all student`; + findAll(): Promise { + return this.studentRepository.find({ skip: 5000, take: 50 }); } - findOne(id: number) { - return `This action returns a #${id} student`; + async findOne(id_cuenta: number): Promise { + const student = await this.studentRepository.findOne({ + where: { id_cuenta }, + }); + if (!student) { + throw new NotFoundException(`Student with ID ${id_cuenta} not found`); + } + return student; } update(id: number, updateStudentDto: UpdateStudentDto) { diff --git a/src/app.module.ts b/src/app.module.ts index 30c4c4d..198e326 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -3,17 +3,18 @@ import { ConfigModule, ConfigService } from '@nestjs/config'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { TypeOrmModule } from '@nestjs/typeorm'; -import { StudentModule } from './student/student.module'; +import { StudentModule } from './alumno/student.module'; import { UserModule } from './user/user.module'; import { User } from './user/entities/user.entity'; import { DetalleServicioModule } from './detalle_servicio/detalle_servicio.module'; import { PeriodoModule } from './periodo/periodo.module'; import { ServicioModule } from './servicio/servicio.module'; +import { Student } from './alumno/entities/student.entity'; @Module({ imports: [ ConfigModule.forRoot({ - isGlobal: true, + isGlobal: true, envFilePath: '.env', }), TypeOrmModule.forRootAsync({ @@ -25,7 +26,7 @@ import { ServicioModule } from './servicio/servicio.module'; username: configService.get('DB_USER'), password: configService.get('DB_PASSWORD'), database: configService.get('DB_NAME'), - entities: [User], + entities: [User, Student], synchronize: false, }), }), @@ -33,6 +34,7 @@ import { ServicioModule } from './servicio/servicio.module'; DetalleServicioModule, PeriodoModule, ServicioModule, + StudentModule, ], controllers: [AppController], providers: [AppService],