diff --git a/src/Profesor/profesor.controller.ts b/src/Profesor/profesor.controller.ts index 85aa4fd..68e1215 100644 --- a/src/Profesor/profesor.controller.ts +++ b/src/Profesor/profesor.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query } from '@nestjs/common'; import { ProfesorService } from './profesor.service'; import { profesorDto } from './dto/profesorDto.dto'; @@ -16,6 +16,14 @@ export class ProfesorController { return this.profesorService.findAll(); } + @Get('page') + findAllPaginated(@Query('page') page: number = 1, @Query('limit') limit: number = 10) { + page = page < 1 ? 1 : page; + limit = limit > 10 || limit < 1 ? 10 : limit; // Limitar el límite máximo a 10 por página + + return this.profesorService.findAllPaginated(page, limit); + } + @Put(':id_profesor') async postModify(@Param('id_profesor', ParseIntPipe) id_profesor: number, @Body() data: profesorDto) { return this.profesorService.modify(id_profesor, data); diff --git a/src/Profesor/profesor.service.ts b/src/Profesor/profesor.service.ts index fbd2599..3dba275 100644 --- a/src/Profesor/profesor.service.ts +++ b/src/Profesor/profesor.service.ts @@ -30,7 +30,11 @@ export class ProfesorService { //brings all teachers async findAll(): Promise { - return this.profesorRepository.find(); + return this.profesorRepository.find({ + order: { + nombre: 'ASC', + }, + }); } //modify teacher @@ -95,4 +99,18 @@ export class ProfesorService { return query.getMany(); } + + async findAllPaginated(page: number, limit: number): Promise<{ profesores: Profesor[], total: number, totalPages: number }> { + const [profesores, total] = await this.profesorRepository.findAndCount({ + skip: (page - 1) * limit, + take: limit, + order: { + nombre: 'ASC', + }, + }); + + const totalPages = Math.ceil(total / limit); + + return { profesores, total, totalPages }; + } } diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 4fe9041..ba3d900 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -39,8 +39,8 @@ export class AuthController { //@UseGuards(AuthGuard) @Put('Modificacion/:id') - async update(@Param('id') id: number, @Body() updateUserDto: Record) { - return this.authService.update(id, updateUserDto); + async update(@Param('id') userId: number, @Body() data: registerDto) { + return this.authService.update(userId, data); } //@UseGuards(AuthGuard) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 6a2e42c..5b88f77 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -65,16 +65,17 @@ export class AuthService { return { token: token }; } - async update(userId, updateUserDto) { + //update user + async update(userId, data:registerDto) { - const{contraseña}=updateUserDto; + const{contraseña}=data; if(contraseña){ const hashedpassword = await hash(contraseña, 10); - updateUserDto = {...updateUserDto,contraseña:hashedpassword}; + data = {...data,contraseña:hashedpassword}; } - return await this.userRepository.update(userId, updateUserDto); + return await this.userRepository.update(userId, data); } async remove(userId: number): Promise {