log into professors
This commit is contained in:
@@ -1,48 +1,75 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query,UseGuards } from '@nestjs/common';
|
||||
import { ProfesorService } from './profesor.service';
|
||||
import { profesorDto } from './dto/profesorDto.dto';
|
||||
import {Roles} from '../permissions/roles.decorator'
|
||||
import {RolesGuard} from '../permissions/roles.guard'
|
||||
import {Role} from '../permissions/role.enum'
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Logger,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards
|
||||
} from "@nestjs/common";
|
||||
import { ProfesorService } from "./profesor.service";
|
||||
import { profesorDto } from "./dto/profesorDto.dto";
|
||||
import { Roles } from "../permissions/roles.decorator";
|
||||
import { RolesGuard } from "../permissions/roles.guard";
|
||||
import { Role } from "../permissions/role.enum";
|
||||
|
||||
@Controller('profesor')
|
||||
@Controller("profesor")
|
||||
@UseGuards(RolesGuard)
|
||||
export class ProfesorController {
|
||||
constructor(private profesorService: ProfesorService) {}
|
||||
constructor(private profesorService: ProfesorService) {
|
||||
}
|
||||
|
||||
@Post()
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
@Roles(Role.Responsable, Role.Admin)
|
||||
async register(@Body() data: profesorDto) {
|
||||
Logger.debug("register profesor");
|
||||
return this.profesorService.register(data);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
Logger.debug("findAllProfesors");
|
||||
return this.profesorService.findAll();
|
||||
}
|
||||
|
||||
@Get('page')
|
||||
findAllPaginated(@Query('page') page: number = 1, @Query('limit') limit: number = 10, @Query() filters: any) {
|
||||
page = page < 1 ? 1 : page;
|
||||
limit = limit > 10 || limit < 1 ? 10 : limit;
|
||||
@Get("page")
|
||||
findAllPaginated(@Query("page") page: number = 1, @Query("limit") limit: number = 10, @Query() filters: any) {
|
||||
try {
|
||||
Logger.debug("Page Profesors");
|
||||
page = page < 1 ? 1 : page;
|
||||
limit = limit > 10 || limit < 1 ? 10 : limit;
|
||||
return this.profesorService.findAllPaginated(page, limit, filters);
|
||||
} catch (err) {
|
||||
Logger.error("profesor controller ", err.error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.profesorService.findAllPaginated(page, limit, filters);
|
||||
}
|
||||
|
||||
@Put(':id_profesor')
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
async modify(@Param('id_profesor', ParseIntPipe) id_profesor: number, @Body() data: profesorDto) {
|
||||
@Put(":id_profesor")
|
||||
@Roles(Role.Responsable, Role.Admin)
|
||||
async modify(@Param("id_profesor", ParseIntPipe) id_profesor: number, @Body() data: profesorDto) {
|
||||
Logger.debug("put Profesors by id");
|
||||
|
||||
return this.profesorService.modify(id_profesor, data);
|
||||
}
|
||||
|
||||
@Delete(':id_profesor')
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
async remove(@Param('id_profesor', ParseIntPipe) id_profesor: number) {
|
||||
@Delete(":id_profesor")
|
||||
@Roles(Role.Responsable, Role.Admin)
|
||||
async remove(@Param("id_profesor", ParseIntPipe) id_profesor: number) {
|
||||
Logger.debug("delete Profesors by id");
|
||||
|
||||
return this.profesorService.remove(id_profesor);
|
||||
}
|
||||
|
||||
@Get(':id_profesor')
|
||||
async profile(@Param('id_profesor', ParseIntPipe) id_profesor: number) {
|
||||
@Get(":id_profesor")
|
||||
async profile(@Param("id_profesor", ParseIntPipe) id_profesor: number) {
|
||||
Logger.debug("get Profesors by id");
|
||||
|
||||
return this.profesorService.profile(id_profesor);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { HttpException, Injectable } from '@nestjs/common';
|
||||
import { Profesor } from './entities/profesor.entity';
|
||||
import { DatosAcademicosService } from '../datos_academicos/datos_academicos.service';
|
||||
import { LineasInvestigacionService } from '../lineas_investigacion/lineas_investigacion.service';
|
||||
import { ProyectosAcademicosService } from '../proyectos_academicos/proyectos_academicos.service';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { profesorDto } from './dto/profesorDto.dto';
|
||||
import { ProyectosAcademicos } from '../proyectos_academicos/entities/proyectos_academicos.entity';
|
||||
import { DatosAcademicos } from '../datos_academicos/entities/datos_academicos.entity'
|
||||
import { LineasInvestigacion } from '../lineas_investigacion/entities/lineas_investigacion.entity'
|
||||
import { HttpException, Injectable, Logger } from "@nestjs/common";
|
||||
import { Profesor } from "./entities/profesor.entity";
|
||||
import { DatosAcademicosService } from "../datos_academicos/datos_academicos.service";
|
||||
import { LineasInvestigacionService } from "../lineas_investigacion/lineas_investigacion.service";
|
||||
import { ProyectosAcademicosService } from "../proyectos_academicos/proyectos_academicos.service";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { profesorDto } from "./dto/profesorDto.dto";
|
||||
import { ProyectosAcademicos } from "../proyectos_academicos/entities/proyectos_academicos.entity";
|
||||
import { DatosAcademicos } from "../datos_academicos/entities/datos_academicos.entity";
|
||||
import { LineasInvestigacion } from "../lineas_investigacion/entities/lineas_investigacion.entity";
|
||||
|
||||
@Injectable()
|
||||
export class ProfesorService {
|
||||
constructor(
|
||||
@InjectRepository(Profesor)
|
||||
@InjectRepository(Profesor)
|
||||
private readonly profesorRepository: Repository<Profesor>,
|
||||
private readonly datosAcademicosService: DatosAcademicosService,
|
||||
private readonly lineasInvestigacionService: LineasInvestigacionService,
|
||||
@@ -23,13 +23,14 @@ export class ProfesorService {
|
||||
@InjectRepository(LineasInvestigacion)
|
||||
private readonly lineasInvestigacionRepository: Repository<LineasInvestigacion>,
|
||||
@InjectRepository(ProyectosAcademicos)
|
||||
private readonly proyectosAcademicosRepository: Repository<ProyectosAcademicos>,
|
||||
) {}
|
||||
private readonly proyectosAcademicosRepository: Repository<ProyectosAcademicos>
|
||||
) {
|
||||
}
|
||||
|
||||
private async findAvailableId(): Promise<number> {
|
||||
let id = 1;
|
||||
let exists = true;
|
||||
|
||||
|
||||
while (exists) {
|
||||
if (!await this.profesorRepository.findOne({ where: { id_profesor: id } })) {
|
||||
exists = false;
|
||||
@@ -41,14 +42,14 @@ export class ProfesorService {
|
||||
|
||||
//register teacher
|
||||
async register(data: profesorDto): Promise<Profesor | undefined> {
|
||||
const { num_trabajador, rfc, proyectosAcademicos, datosAcademicos, lineasInvestigacion} = data;
|
||||
const { num_trabajador, rfc, proyectosAcademicos, datosAcademicos, lineasInvestigacion } = data;
|
||||
|
||||
if (await this.profesorRepository.findOne({ where: { rfc } })) {
|
||||
throw new HttpException('rfc already exist', 403);
|
||||
throw new HttpException("rfc already exist", 403);
|
||||
}
|
||||
|
||||
if (await this.profesorRepository.findOne({ where: { num_trabajador } })) {
|
||||
throw new HttpException('number of worker already exist', 403);
|
||||
throw new HttpException("number of worker already exist", 403);
|
||||
}
|
||||
|
||||
const availableId = await this.findAvailableId();
|
||||
@@ -61,7 +62,7 @@ export class ProfesorService {
|
||||
const newProyecto = this.proyectosAcademicosRepository.create({
|
||||
...proyecto,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newProyecto;
|
||||
});
|
||||
@@ -73,7 +74,7 @@ export class ProfesorService {
|
||||
const newLinea = this.lineasInvestigacionRepository.create({
|
||||
...linea,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newLinea;
|
||||
});
|
||||
@@ -85,7 +86,7 @@ export class ProfesorService {
|
||||
const newDato = this.datosAcademicosRepository.create({
|
||||
...datosAcademicos,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newDato;
|
||||
});
|
||||
@@ -99,8 +100,8 @@ export class ProfesorService {
|
||||
async findAll(): Promise<Profesor[]> {
|
||||
return this.profesorRepository.find({
|
||||
order: {
|
||||
nombre: 'ASC',
|
||||
},
|
||||
nombre: "ASC"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -108,7 +109,7 @@ export class ProfesorService {
|
||||
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);
|
||||
throw new HttpException("Profesor not found", 404);
|
||||
}
|
||||
|
||||
const { proyectosAcademicos, datosAcademicos, lineasInvestigacion, ...profesorData } = data;
|
||||
@@ -122,7 +123,7 @@ export class ProfesorService {
|
||||
const newProyecto = this.proyectosAcademicosRepository.create({
|
||||
...proyecto,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newProyecto;
|
||||
});
|
||||
@@ -130,12 +131,12 @@ export class ProfesorService {
|
||||
}
|
||||
|
||||
if (lineasInvestigacion) {
|
||||
await this.lineasInvestigacionRepository.delete({ profesor: {id_profesor}});
|
||||
await this.lineasInvestigacionRepository.delete({ profesor: { id_profesor } });
|
||||
const lineas = lineasInvestigacion.map(linea => {
|
||||
const newLinea = this.lineasInvestigacionRepository.create({
|
||||
...linea,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newLinea;
|
||||
});
|
||||
@@ -148,7 +149,7 @@ export class ProfesorService {
|
||||
const newDato = this.datosAcademicosRepository.create({
|
||||
...datosAcademicos,
|
||||
profesor: savedProfesor,
|
||||
id_profesor: savedProfesor.id_profesor,
|
||||
id_profesor: savedProfesor.id_profesor
|
||||
});
|
||||
return newDato;
|
||||
});
|
||||
@@ -160,28 +161,28 @@ export class ProfesorService {
|
||||
|
||||
//remove teacher
|
||||
async remove(id_profesor: number) {
|
||||
const profesor = await this.profesorRepository.findOne({ where: { id_profesor }, relations: ['datosAcademicos'] });
|
||||
const profesor = await this.profesorRepository.findOne({ where: { id_profesor }, relations: ["datosAcademicos"] });
|
||||
if (!profesor) {
|
||||
throw new HttpException('Profesor not found', 404);
|
||||
throw new HttpException("Profesor not found", 404);
|
||||
}
|
||||
|
||||
await this.datosAcademicosService.removeByProfesorId(id_profesor);
|
||||
await this.lineasInvestigacionService.removeByProfesorId(id_profesor);
|
||||
await this.proyectosAcademicosService.removeByProfesorId(id_profesor);
|
||||
await this.datosAcademicosService.removeByProfesorId(id_profesor);
|
||||
await this.lineasInvestigacionService.removeByProfesorId(id_profesor);
|
||||
await this.proyectosAcademicosService.removeByProfesorId(id_profesor);
|
||||
|
||||
await this.profesorRepository.remove(profesor);
|
||||
return { message: 'Profesor removed successfully' };
|
||||
return { message: "Profesor removed successfully" };
|
||||
}
|
||||
|
||||
//brings teachers by id
|
||||
async profile(id_profesor: number) {
|
||||
const profesor = await this.profesorRepository.findOne({
|
||||
where: { id_profesor },
|
||||
relations: ['proyectosAcademicos', 'datosAcademicos', 'lineasInvestigacion'],
|
||||
relations: ["proyectosAcademicos", "datosAcademicos", "lineasInvestigacion"]
|
||||
});
|
||||
|
||||
|
||||
if (!profesor) {
|
||||
throw new HttpException('Profesor not found', 404);
|
||||
throw new HttpException("Profesor not found", 404);
|
||||
}
|
||||
|
||||
return profesor;
|
||||
@@ -192,43 +193,53 @@ export class ProfesorService {
|
||||
limit: number,
|
||||
filters: any
|
||||
): Promise<{ profesores: Profesor[], total: number, totalPages: number }> {
|
||||
const query = this.profesorRepository.createQueryBuilder('profesor');
|
||||
|
||||
const query = this.profesorRepository.createQueryBuilder("profesor");
|
||||
|
||||
if (filters.nombre) {
|
||||
query.andWhere('profesor.nombre LIKE :nombre', { nombre: `%${filters.nombre}%` });
|
||||
Logger.debug('fiter by name');
|
||||
|
||||
query.andWhere("profesor.nombre LIKE :nombre", { nombre: `%${filters.nombre}%` });
|
||||
}
|
||||
|
||||
|
||||
if (filters.adscripcion) {
|
||||
query.andWhere('profesor.adscripcion = :adscripcion', { adscripcion: filters.adscripcion });
|
||||
Logger.debug("fiter by adscription");
|
||||
|
||||
query.andWhere("profesor.adscripcion = :adscripcion", { adscripcion: filters.adscripcion });
|
||||
}
|
||||
|
||||
|
||||
if (filters.categoria) {
|
||||
query.andWhere('profesor.categoria = :categoria', { categoria: filters.categoria });
|
||||
Logger.debug("fiter by category");
|
||||
|
||||
query.andWhere("profesor.categoria = :categoria", { categoria: filters.categoria });
|
||||
}
|
||||
|
||||
|
||||
if (filters.edificio) {
|
||||
query.andWhere('profesor.edificio = :edificio', { edificio: filters.edificio });
|
||||
Logger.debug("fiter by building");
|
||||
|
||||
query.andWhere("profesor.edificio = :edificio", { edificio: filters.edificio });
|
||||
}
|
||||
|
||||
if (typeof filters.activo !== 'undefined') {
|
||||
query.andWhere('profesor.activo = :activo', { activo: filters.activo });
|
||||
|
||||
if (typeof filters.activo !== "undefined") {
|
||||
Logger.debug("fiter by is active");
|
||||
|
||||
query.andWhere("profesor.activo = :activo", { activo: filters.activo });
|
||||
} else {
|
||||
query.orderBy('profesor.activo', 'DESC');
|
||||
query.orderBy("profesor.activo", "DESC");
|
||||
}
|
||||
|
||||
query.addOrderBy('profesor.nombre', 'ASC');
|
||||
|
||||
|
||||
query.addOrderBy("profesor.nombre", "ASC");
|
||||
|
||||
const [profesores, total] = await query
|
||||
.skip((page - 1) * limit)
|
||||
.take(limit)
|
||||
.getManyAndCount();
|
||||
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
|
||||
return { profesores, total, totalPages };
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
async updateImageURL(id_profesor: number, imageUrl: string): Promise<void> {
|
||||
await this.profesorRepository.update(id_profesor, { fotografia: imageUrl });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user