70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { HttpException, Injectable } from '@nestjs/common';
|
|
import { Profesor } from './entities/profesor.entity';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { profesorDto } from './dto/profesorDto.dto';
|
|
|
|
@Injectable()
|
|
export class ProfesorService {
|
|
constructor(
|
|
@InjectRepository(Profesor) private profesorRepository: Repository<Profesor>
|
|
) {}
|
|
|
|
async register(data: profesorDto) {
|
|
const { nombre } = data;
|
|
|
|
const existingProfesor = await this.profesorRepository.findOne({ where: { nombre } });
|
|
if (existingProfesor) {
|
|
throw new HttpException('User already exist', 403);
|
|
}
|
|
|
|
const newProfesor = this.profesorRepository.create(data);
|
|
return this.profesorRepository.save(newProfesor);
|
|
}
|
|
|
|
async modify(id_profesor: number, data: profesorDto) {
|
|
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
|
|
if (!profesor) {
|
|
throw new HttpException('Profesor not found', 404);
|
|
}
|
|
|
|
Object.assign(profesor, data);
|
|
return this.profesorRepository.save(profesor);
|
|
}
|
|
|
|
async remove(id_profesor: number) {
|
|
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
|
|
if (!profesor) {
|
|
throw new HttpException('Profesor not found', 404);
|
|
}
|
|
|
|
await this.profesorRepository.remove(profesor);
|
|
return { message: 'Profesor removed successfully' };
|
|
}
|
|
|
|
async profile(id_profesor: number) {
|
|
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
|
|
if (!profesor) {
|
|
throw new HttpException('Profesor not found', 404);
|
|
}
|
|
|
|
return profesor;
|
|
}
|
|
|
|
async filter(data: profesorDto) {
|
|
const query = this.profesorRepository.createQueryBuilder('profesor');
|
|
|
|
if (data.nombre) {
|
|
query.andWhere('profesor.nombre = :nombre', { nombre: data.nombre });
|
|
}
|
|
if (data.carrera) {
|
|
query.andWhere('profesor.carrera = :carrera', { carrera: data.carrera });
|
|
}
|
|
if (data.id_categoria) {
|
|
query.andWhere('profesor.id_categoria = :id_categoria', { id_categoria: data.id_categoria });
|
|
}
|
|
|
|
return query.getMany();
|
|
}
|
|
}
|