diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 29d381c..dc0cde3 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -10,7 +10,8 @@ import { Request, UseGuards, Delete, - ParseIntPipe + ParseIntPipe, + Query } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { AuthService } from './auth.service'; @@ -44,9 +45,21 @@ export class AuthController { return this.authService.findAll(); } - @Get(':id_user') - async profile(@Param('id_user', ParseIntPipe) id_user: number) { - return this.authService.profile(id_user); + @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; + + return this.authService.findAllPaginated(page, limit,filters); + } + + @Get(':id_usuario') + async profile(@Param('id_usuario', ParseIntPipe) id_usuario: number) { + return this.authService.profile(id_usuario); } //@UseGuards(AuthGuard) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index f3d0e2d..3c05301 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -113,6 +113,32 @@ export class AuthService { return this.userRepository.find(); } + async findAllPaginated( + page: number, + limit: number, + filters: any + ): Promise<{ usuarios: User[], total: number, totalPages: number }> { + const query = this.userRepository.createQueryBuilder('usuario'); + + if (filters.nombre) { + query.andWhere('usuario.nombre LIKE :nombre', { nombre: `%${filters.nombre}%` }); + } + + if (filters.id_tipo_usuario) { + query.andWhere('usuario.id_tipo_usuario = :id_tipo_usuario', { id_tipo_usuario: filters.id_tipo_usuario }); + } + + const [usuarios, total] = await query + .skip((page - 1) * limit) + .take(limit) + .orderBy('usuario.nombre', 'ASC') + .getManyAndCount(); + + const totalPages = Math.ceil(total / limit); + + return { usuarios, total, totalPages }; + } + async profile(id_usuario: number) { const user = await this.userRepository.findOne({ where: { id_usuario },