routes added
This commit is contained in:
parent
ced5837f71
commit
ca864f33b7
18
src/routes/auth/auth.controller.spec.ts
Normal file
18
src/routes/auth/auth.controller.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
22
src/routes/auth/auth.controller.ts
Normal file
22
src/routes/auth/auth.controller.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
9
src/routes/auth/auth.module.ts
Normal file
9
src/routes/auth/auth.module.ts
Normal 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 {}
|
||||
18
src/routes/auth/auth.service.spec.ts
Normal file
18
src/routes/auth/auth.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
21
src/routes/auth/auth.service.ts
Normal file
21
src/routes/auth/auth.service.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
9
src/routes/auth/dto/login.dto.ts
Normal file
9
src/routes/auth/dto/login.dto.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { IsEmail, MinLength } from 'class-validator';
|
||||
|
||||
export class LoginDto {
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
@MinLength(8)
|
||||
password: string;
|
||||
}
|
||||
15
src/routes/auth/dto/register.dto.ts
Normal file
15
src/routes/auth/dto/register.dto.ts
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
3
src/routes/user/dto/createUser.dto.ts
Normal file
3
src/routes/user/dto/createUser.dto.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export class CreateUserDto {
|
||||
email: string;
|
||||
}
|
||||
20
src/routes/user/user.controller.spec.ts
Normal file
20
src/routes/user/user.controller.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
26
src/routes/user/user.controller.ts
Normal file
26
src/routes/user/user.controller.ts
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
9
src/routes/user/user.module.ts
Normal file
9
src/routes/user/user.module.ts
Normal 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 {}
|
||||
18
src/routes/user/user.service.spec.ts
Normal file
18
src/routes/user/user.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
34
src/routes/user/user.service.ts
Normal file
34
src/routes/user/user.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user