routes added

This commit is contained in:
sdraris 2025-08-20 10:29:44 +02:00
parent ced5837f71
commit ca864f33b7
13 changed files with 222 additions and 0 deletions

View File

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

View File

@ -0,0 +1,22 @@
import { Body, Controller, Get, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { RegisterDto } from './dto/register.dto';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
//Route pour se connecter
@Post('login')
login(@Body() loginDto: LoginDto) {
return this.authService.login(loginDto);
}
//Route pour s'inscrire
@Post('register')
register(@Body() registerDto: RegisterDto) {
return this.authService.register(registerDto);
}
}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
@Module({
controllers: [AuthController],
providers: [AuthService]
})
export class AuthModule {}

View File

@ -0,0 +1,18 @@
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

@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
import { LoginDto } from './dto/login.dto';
import { RegisterDto } from './dto/register.dto';
@Injectable()
export class AuthService {
register(registerDto: RegisterDto) {
return {
message: `User registered successfully ${registerDto}`,
user: registerDto,
};
}
login(loginDto: LoginDto) {
return {
message: `Login successful ${loginDto.email}`,
user: loginDto,
};
}
}

View File

@ -0,0 +1,9 @@
import { IsEmail, MinLength } from 'class-validator';
export class LoginDto {
@IsEmail()
email: string;
@MinLength(8)
password: string;
}

View File

@ -0,0 +1,15 @@
import { IsEmail, IsNotEmpty, MaxLength, MinLength } from "class-validator";
export class RegisterDto {
@IsEmail()
email: string;
@IsNotEmpty()
username: string;
@MinLength(8)
password: string;
}

View File

@ -0,0 +1,3 @@
export class CreateUserDto {
email: string;
}

View File

@ -0,0 +1,20 @@
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();
});
});

View File

@ -0,0 +1,26 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { UserService } from './user.service';
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
//Route pour retrouver tous les utilisateurs
@Get()
findAll() {
return this.userService.findAll();
}
//Route pour retrouver un utilisateur par ID
@Get(':id')
findOneById(@Param('id') id: string) {
return this.userService.findOneById(Number(id));
}
//Route pour créer un utilisateur
@Post()
createUser(@Body() createUserDto: { email: string }) {
return this.userService.createUser(createUserDto);
}
}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { UserController } from './user.controller';
import { UserService } from './user.service';
@Module({
controllers: [UserController],
providers: [UserService]
})
export class UserModule {}

View File

@ -0,0 +1,18 @@
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();
});
});

View File

@ -0,0 +1,34 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class UserService {
private users = [
{ id: 1, email: 'alice@example.com' },
{ id: 2, email: 'bob@example.com' },
];
//Methode pour trouver tous les utilisateurs
findAll() {
return this.users;
}
//Methode pour trouver un utilisateur par ID
findOneById(id: number) {
return this.users.find(user => user.id === id);
}
//Methode pour trouver un utilisateur par email
findOneByEmail(email: string) {
return this.users.find(user => user.email === email);
}
//Methode pour faire un utilisateur
createUser(createUserDto: {email: string}) {
const newUser = {
id: this.users.length + 1,
...createUserDto,
};
this.users.push(newUser);
return newUser;
}
}