2025-09-09 21:19:17 -04:00
|
|
|
import { Injectable, Logger, NotFoundException, UnauthorizedException } from '@nestjs/common';
|
2025-09-04 21:04:15 -04:00
|
|
|
import { CreateUserDto } from './dto/create-user.dto';
|
|
|
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
2025-09-09 21:19:17 -04:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
import { User } from './entities/user.entity';
|
|
|
|
|
import { Repository } from 'typeorm';
|
2025-09-04 21:04:15 -04:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class UserService {
|
2025-09-09 21:19:17 -04:00
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(User)
|
|
|
|
|
private userRepository: Repository<User>,
|
|
|
|
|
) { }
|
|
|
|
|
|
2025-09-04 21:04:15 -04:00
|
|
|
create(createUserDto: CreateUserDto) {
|
|
|
|
|
return 'This action adds a new user';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findAll() {
|
|
|
|
|
return `This action returns all user`;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 21:19:17 -04:00
|
|
|
async findOneByName(nombre: string): Promise<User> {
|
|
|
|
|
|
|
|
|
|
const user = await this.userRepository.findOne({
|
|
|
|
|
where: { nombre }
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException(
|
|
|
|
|
`El usuario ${nombre} no fue encontrado`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return user;
|
2025-09-04 21:04:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update(id: number, updateUserDto: UpdateUserDto) {
|
|
|
|
|
return `This action updates a #${id} user`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove(id: number) {
|
|
|
|
|
return `This action removes a #${id} user`;
|
|
|
|
|
}
|
2025-09-09 21:19:17 -04:00
|
|
|
|
|
|
|
|
async Login(data: CreateUserDto) {
|
|
|
|
|
const { username } = data
|
|
|
|
|
|
|
|
|
|
if (!username) {
|
|
|
|
|
throw new UnauthorizedException('Credenciales inválidas.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const user = await this.findOneByName(username)
|
|
|
|
|
return user
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 21:04:15 -04:00
|
|
|
}
|