filter with page

This commit is contained in:
IO
2024-06-20 19:02:11 -06:00
parent 61558b9564
commit c5e900915c
2 changed files with 27 additions and 11 deletions
+3 -3
View File
@@ -17,11 +17,11 @@ export class ProfesorController {
}
@Get('page')
findAllPaginated(@Query('page') page: number = 1, @Query('limit') limit: number = 10) {
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; // Limitar el límite máximo a 10 por página
limit = limit > 10 || limit < 1 ? 10 : limit;
return this.profesorService.findAllPaginated(page, limit);
return this.profesorService.findAllPaginated(page, limit, filters);
}
@Put(':id_profesor')
+24 -8
View File
@@ -100,14 +100,30 @@ 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',
},
});
async findAllPaginated(page: number, limit: number, filters: any): Promise<{ profesores: Profesor[], total: number, totalPages: number }> {
const query = this.profesorRepository.createQueryBuilder('profesor');
if (filters.nombre) {
query.andWhere('profesor.nombre LIKE :nombre', { nombre: `%${filters.nombre}%` });
}
if (filters.adscripcion) {
query.andWhere('profesor.adscripcion = :adscripcion', { adscripcion: filters.adscripcion });
}
if (filters.categoria) {
query.andWhere('profesor.categoria = :categoria', { categoria: filters.categoria });
}
if (filters.edificio) {
query.andWhere('profesor.edificio = :edificio', { edificio: filters.edificio });
}
const [profesores, total] = await query
.skip((page - 1) * limit)
.take(limit)
.orderBy('profesor.nombre', 'ASC')
.getManyAndCount();
const totalPages = Math.ceil(total / limit);