added entities and modules

This commit is contained in:
2025-10-23 10:01:00 -06:00
parent f66de9458b
commit 3bdfac1f66
21 changed files with 758 additions and 1 deletions
@@ -0,0 +1,22 @@
import { IsEmail, 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;
@IsEmail()
@IsString()
correo: string;
}
@@ -0,0 +1,18 @@
import { Controller, Get, Post, Param } from '@nestjs/common';
import { AlumnoService } from './student.service';
@Controller('student')
export class AlumnoController {
constructor(private readonly alumnoService: AlumnoService) {}
// @Post()
// async create(@Body() createStudentDto: CreateStudentDto): Promise<Alumno> {
// return this.alumnoService.create(createStudentDto);
// }
@Get(':id')
findOne(@Param('id') id: number) {
return this.alumnoService.findOne(+id);
}
}
//IO
+14
View File
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { AlumnoService } from './student.service';
import { AlumnoController } from './student.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Alumno } from 'src/database/AT/entities/student.entity';
@Module({
imports: [TypeOrmModule.forFeature([Alumno], 'dbAT')],
controllers: [AlumnoController],
providers: [AlumnoService],
exports: [AlumnoService],
})
export class AlumnoModule {}
//IO
+83
View File
@@ -0,0 +1,83 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { EntityManager, Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Alumno } from 'src/database/AT/entities/student.entity';
@Injectable()
export class AlumnoService {
constructor(
@InjectRepository(Alumno, 'dbAT')
private readonly studentRepository: Repository<Alumno>,
// @InjectRepository(Carrera)
// private readonly carreraRepository: Repository<Carrera>,
) {}
// async create(data: CreateStudentDto): Promise<Alumno> {
// const { id_carrera, ...rest } = data;
// const carrera = await this.carreraRepository.findOne({
// where: { id_carrera: data.id_carrera },
// });
// if (!carrera) {
// throw new NotFoundException(`Carrera not found`);
// }
// const createStudent = { ...rest, carrera, fecha_registro: new Date() };
// const student = this.studentRepository.create(createStudent);
// return await this.studentRepository.save(student);
// }
async findOne(id_cuenta: number): Promise<Alumno> {
const student = await this.studentRepository.findOne({
where: { id_cuenta },
select: { id_cuenta: true, nombre: true, credito: true },
});
if (!student) {
throw new NotFoundException(`Student not found`);
}
return student;
}
async findOneByName(nombre: string): Promise<Alumno> {
const student = await this.studentRepository.findOne({
where: { nombre },
});
if (!student) {
throw new NotFoundException(`Student not found`);
}
return student;
}
async GetCredit(id_cuenta: number): Promise<Alumno['credito']> {
const student = await this.findOne(id_cuenta);
return student.credito;
}
async collectCredit(
id_cuenta: number,
credit: number,
manager: EntityManager,
) {
const repo = manager.getRepository(Alumno);
return await repo
.createQueryBuilder()
.update()
.set({ credito: () => `credito - ${credit}` })
.where({ id_cuenta })
.execute();
}
async addCredit(id_cuenta: number, credit: number, manager: EntityManager) {
const repo = manager.getRepository(Alumno);
return await repo
.createQueryBuilder()
.update()
.set({ credito: () => `credito + ${credit}` })
.where({ id_cuenta })
.execute();
}
}
//IO