initial commit

This commit is contained in:
2024-07-19 19:55:57 +02:00
commit 049e063d48
53 changed files with 15239 additions and 0 deletions

View 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;
}

View File

@@ -0,0 +1,5 @@
export enum Role {
Public = 'public',
User = 'user',
Admin = 'admin',
}

View 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 {}

View 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();
});
});

View 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),
// );
// }
}