initial commit
This commit is contained in:
19
back/src/users/entities/user.entity.ts
Normal file
19
back/src/users/entities/user.entity.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
// TODO: username should be unique and, maybe can act as ID?
|
||||
// NOTE: CREATE TABLE user (id bigint primary key DEFAULT UUID_SHORT(), username char(20) not null, password char(20) not null, roles char(50) not null);
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
username: string;
|
||||
|
||||
@Column()
|
||||
password: string;
|
||||
|
||||
@Column()
|
||||
roles: string;
|
||||
}
|
||||
5
back/src/users/roles/role.enum.ts
Normal file
5
back/src/users/roles/role.enum.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export enum Role {
|
||||
Public = 'public',
|
||||
User = 'user',
|
||||
Admin = 'admin',
|
||||
}
|
||||
11
back/src/users/users.module.ts
Normal file
11
back/src/users/users.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User])],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
18
back/src/users/users.service.spec.ts
Normal file
18
back/src/users/users.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
describe('UsersService', () => {
|
||||
let service: UsersService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [UsersService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<UsersService>(UsersService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
51
back/src/users/users.service.ts
Normal file
51
back/src/users/users.service.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Role } from './roles/role.enum';
|
||||
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
|
||||
export type UserType = {
|
||||
id: number;
|
||||
username: string;
|
||||
password: string;
|
||||
roles: Role[];
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private usersRepository: Repository<User>,
|
||||
@InjectDataSource()
|
||||
private dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async findOne(username: string): Promise<UserType | undefined> {
|
||||
const db_user = await this.usersRepository.findOneBy({ username });
|
||||
if (!db_user) return null;
|
||||
//TODO: change this shabby mapping for a more adequate database structure
|
||||
const user: UserType = {
|
||||
id: db_user.id,
|
||||
username: db_user.username,
|
||||
password: db_user.password,
|
||||
roles: db_user.roles.split(';') as Role[],
|
||||
};
|
||||
console.log(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
async doSOmething() {
|
||||
this.dataSource.query('SELECT * from users');
|
||||
}
|
||||
|
||||
// async create(
|
||||
// username: string,
|
||||
// password: string,
|
||||
// roles: Role[],
|
||||
// ): Promise<User | undefined> {
|
||||
// const roles_string = roles.join(';');
|
||||
// const create_result = this.usersRepository.create(
|
||||
// new User(username, password, roles_string),
|
||||
// );
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user