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 { EntityManager, Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; @Injectable() export class StudentService { 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(): Promise { return this.studentRepository.find({ skip: 5000, take: 50 }); } 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; } async GetCredit(id_cuenta: number): Promise { const student = await this.findOne(id_cuenta); return student.credito; } async UpdateCredit( id_cuenta: number, credit: number, manager: EntityManager, ) { const repo = manager.getRepository(Student); return await repo .createQueryBuilder() .update() .set({ credito: () => `credito - ${credit}` }) .where({ id_cuenta }) .execute(); } }