Files
api-AT/src/alumno/student.service.ts
T

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-09-14 17:51:17 -06:00
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateStudentDto } from './dto/create-student.dto';
2025-09-26 17:43:19 -06:00
import { Alumno } from './entities/student.entity';
import { EntityManager, Repository } from 'typeorm';
2025-09-14 17:51:17 -06:00
import { InjectRepository } from '@nestjs/typeorm';
2025-09-26 17:43:19 -06:00
import { Carrera } from 'src/carrera/entities/carrera.entity';
@Injectable()
2025-09-17 16:35:52 -06:00
export class AlumnoService {
2025-09-14 17:51:17 -06:00
constructor(
2025-09-18 16:50:10 -06:00
@InjectRepository(Alumno)
private readonly studentRepository: Repository<Alumno>,
2025-09-24 11:04:08 -04:00
@InjectRepository(Carrera)
private readonly carreraRepository: Repository<Carrera>,
2025-09-14 17:51:17 -06:00
) {}
2025-09-24 11:04:08 -04:00
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() };
2025-09-24 11:04:08 -04:00
const student = this.studentRepository.create(createStudent);
2025-09-14 17:51:17 -06:00
return await this.studentRepository.save(student);
}
2025-09-18 16:50:10 -06:00
async findOne(id_cuenta: number): Promise<Alumno> {
2025-09-14 17:51:17 -06:00
const student = await this.studentRepository.findOne({
where: { id_cuenta },
select: { id_cuenta: true, nombre: true, credito: true },
2025-09-14 17:51:17 -06:00
});
2025-09-24 11:00:56 -06:00
2025-09-14 17:51:17 -06:00
if (!student) {
2025-09-18 15:51:30 -06:00
throw new NotFoundException(`Student not found`);
2025-09-14 17:51:17 -06:00
}
2025-09-24 11:00:56 -06:00
2025-09-14 17:51:17 -06:00
return student;
}
2025-09-18 16:50:10 -06:00
async GetCredit(id_cuenta: number): Promise<Alumno['credito']> {
const student = await this.findOne(id_cuenta);
return student.credito;
}
2025-09-19 18:30:44 -06:00
async collectCredit(
id_cuenta: number,
credit: number,
manager: EntityManager,
) {
2025-09-18 16:50:10 -06:00
const repo = manager.getRepository(Alumno);
return await repo
.createQueryBuilder()
.update()
.set({ credito: () => `credito - ${credit}` })
.where({ id_cuenta })
.execute();
}
2025-09-19 18:30:44 -06:00
2025-09-24 11:00:56 -06:00
async addCredit(id_cuenta: number, credit: number, manager: EntityManager) {
2025-09-19 18:30:44 -06:00
const repo = manager.getRepository(Alumno);
return await repo
.createQueryBuilder()
.update()
.set({ credito: () => `credito + ${credit}` })
.where({ id_cuenta })
.execute();
}
}
2025-09-24 11:00:56 -06:00
//IO