ajout d'une bdd postgresql aec le type orm, d'une table user avec quelques champs dedans. TODO : faire une relations basique entre deux entités, par exemple la question de la relation entre deux personnes, amis / bloqués.

This commit is contained in:
batche
2022-10-24 18:52:46 +02:00
parent 5396cce993
commit 5e53a2df78
9863 changed files with 554763 additions and 287 deletions

View File

@@ -1,11 +1,27 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [UsersModule],
imports: [UsersModule,
ConfigModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: parseInt(process.env.POSTGRES_PORT),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DATABASE,
autoLoadEntities: true,
//ne pas synchroniser quand on est en prod. Trouver un moyen de set ça, sûrement
//avec une classe pour le module
synchronize: true,
}),
],
controllers: [AppController],
providers: [AppService],
})

View File

@@ -1,9 +0,0 @@
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { UsersModule } from 'src/users/users.module';
@Module({
imports: [UsersModule],
providers: [AuthService],
})
export class AuthModule {}

View File

@@ -1,18 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile();
service = module.get<AuthService>(AuthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -1,11 +0,0 @@
import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
@Injectable()
export class AuthService {
constructor (private usersService: UsersService) {}
async validateUser(username: string, pass: string): Promise<any> {
return null;
}
}

View File

@@ -1,6 +1,19 @@
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity('users')
export class User {
userId: string;
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
username: string;
//Dans le cadre d'un tableau on peut faire :
// @Column('json', { nullable: true })
@Column()
password: string;
@Column('json', { nullable: true })
status: [string];
}

View File

@@ -1,10 +1,13 @@
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { User } from './entities/user.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
providers: [UsersService],
exports: [UsersService],
controllers: [UsersController],
imports: [TypeOrmModule.forFeature([User])],
providers: [UsersService],
exports: [UsersService],
controllers: [UsersController],
})
export class UsersModule {}

View File

@@ -1,54 +1,52 @@
import { HttpCode, HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { NotFoundError } from 'rxjs';
import { User } from './entities/user.entity';
import { Repository } from 'typeorm';
import { CreateUsersDto } from './dto/create-users.dto';
import { UpdateUsersDto } from './dto/update-users.dto';
@Injectable()
export class UsersService {
private users:User[] = [
{
userId: '1',
name: 'John',
username: 'TheBoss',
password: 'changeme',
},
{
userId: '2',
name: 'Jane',
username: 'Jane2000',
password: 'guess',
},
];
findOne(id: string) {
const user = this.users.find(user => user.userId === id);
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
async findOne(id: string) {
const user = await this.userRepository.findOneBy({id: +id});
if (!user)
throw new NotFoundException(`The requested user not found.`);
return user;
}
findAll() {
if (!this.users.length)
throw new HttpException(`No user exists.`,HttpStatus.NOT_FOUND);
return this.users;
return this.userRepository.find();
}
create(createUserDto: any) {
const user = this.users.push(createUserDto);
create(createUserDto: CreateUsersDto) {
const user = this.userRepository.create(createUserDto);
if (!user)
throw new HttpException(`The user could not be created.`,HttpStatus.NOT_FOUND);
return createUserDto.username + ' has been created';
return this.userRepository.save(user);
}
update(id: string, updateUserDto: any) {
const index = this.users.findIndex(user => user.userId === id);
this.users[index] = updateUserDto;
return this.users[index].username + ' has been updated';
async update(id: string, updateUserDto: UpdateUsersDto) {
const user = await this.userRepository.preload(
{id: +id,
...updateUserDto});
if (!user)
throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND);
return this.userRepository.save(user);
}
remove(id: string) {
const index = this.users.findIndex(user => user.userId === id);
this.users.splice(index, 1);
return 'User has been deleted';
async remove(id: string) {
const user = await this.findOne(id);
if (!user)
throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND);
return this.userRepository.remove(user);
}
}