added folder student and create new Ep Login
This commit is contained in:
@@ -1 +1,10 @@
|
||||
export class CreateUserDto {}
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
export class CreateUserDto {
|
||||
|
||||
@IsString()
|
||||
username:string
|
||||
|
||||
@IsString()
|
||||
password:string
|
||||
}
|
||||
|
||||
@@ -1,6 +1,35 @@
|
||||
import { Entity } from "typeorm";
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
@Entity({name:'usuario'})
|
||||
export class User {
|
||||
|
||||
@PrimaryGeneratedColumn({name:'id_usuario',type: 'int'})
|
||||
id_usuario:number;
|
||||
|
||||
@Column({name:'nombre',type:'varchar'})
|
||||
nombre:string
|
||||
|
||||
@Column({name:'apellido_paterno',type:'varchar'})
|
||||
apellido_paterno:string
|
||||
|
||||
@Column({name:'apellido_materno',type:'varchar'})
|
||||
apellido_materno:string
|
||||
|
||||
@Column({name:'usuario',type:'varchar'})
|
||||
usuario:string
|
||||
|
||||
@Column({name:'password',type:'varchar',length:45, nullable:false})
|
||||
password:string
|
||||
|
||||
@Column({name:'activo',type:'tinyint',nullable:false, default:1})
|
||||
activo: number
|
||||
|
||||
@Column({name:'descripcion',type:'varchar',length:50,default:null})
|
||||
description:boolean
|
||||
|
||||
@Column({name:'fecha_registro',type:'varchar'})
|
||||
fecha_registro:string
|
||||
|
||||
@Column({name:'id_perfil',})
|
||||
id_perfil:number
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UserController } from './user.controller';
|
||||
import { UserService } from './user.service';
|
||||
|
||||
describe('UserController', () => {
|
||||
let controller: UserController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<UserController>(UserController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -7,28 +7,14 @@ import { UpdateUserDto } from './dto/update-user.dto';
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createUserDto: CreateUserDto) {
|
||||
return this.userService.create(createUserDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.userService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.userService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
|
||||
return this.userService.update(+id, updateUserDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.userService.remove(+id);
|
||||
@Post()
|
||||
Login(@Body() user: CreateUserDto) {
|
||||
return this.userService.Login(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { UserController } from './user.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
|
||||
@Module({
|
||||
imports:[
|
||||
TypeOrmModule.forFeature([User])
|
||||
],
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
exports: [UserService],
|
||||
})
|
||||
export class UserModule {}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UserService } from './user.service';
|
||||
|
||||
describe('UserService', () => {
|
||||
let service: UserService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [UserService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<UserService>(UserService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, Logger, NotFoundException, UnauthorizedException } from '@nestjs/common';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private userRepository: Repository<User>,
|
||||
) { }
|
||||
|
||||
create(createUserDto: CreateUserDto) {
|
||||
return 'This action adds a new user';
|
||||
}
|
||||
@@ -12,8 +20,19 @@ export class UserService {
|
||||
return `This action returns all user`;
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} user`;
|
||||
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;
|
||||
}
|
||||
|
||||
update(id: number, updateUserDto: UpdateUserDto) {
|
||||
@@ -23,4 +42,16 @@ export class UserService {
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} user`;
|
||||
}
|
||||
|
||||
async Login(data: CreateUserDto) {
|
||||
const { username } = data
|
||||
|
||||
if (!username) {
|
||||
throw new UnauthorizedException('Credenciales inválidas.');
|
||||
}
|
||||
|
||||
const user = await this.findOneByName(username)
|
||||
return user
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user