This commit is contained in:
TioSam77
2024-06-19 16:23:24 -06:00
4 changed files with 35 additions and 8 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);
+19 -1
View File
@@ -30,7 +30,11 @@ export class ProfesorService {
//brings all teachers
async findAll(): Promise<Profesor[]> {
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 };
}
}
+2 -2
View File
@@ -39,8 +39,8 @@ export class AuthController {
//@UseGuards(AuthGuard)
@Put('Modificacion/:id')
async update(@Param('id') id: number, @Body() updateUserDto: Record<string, any>) {
return this.authService.update(id, updateUserDto);
async update(@Param('id') userId: number, @Body() data: registerDto) {
return this.authService.update(userId, data);
}
//@UseGuards(AuthGuard)
+5 -4
View File
@@ -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<void> {