new get test

This commit is contained in:
2024-06-19 16:18:12 -06:00
parent 4cb0c7d737
commit 3f6bb6d00e
2 changed files with 23 additions and 1 deletions
+9 -1
View File
@@ -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);
+14
View File
@@ -99,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 };
}
}