refactor nest app to server folder
This commit is contained in:
18
server/src/user/dto/create-user.dto.ts
Normal file
18
server/src/user/dto/create-user.dto.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { IsString, IsEmail, MinLength } from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class CreateUserDto {
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@ApiProperty()
|
||||
username: string;
|
||||
|
||||
@IsEmail()
|
||||
@ApiProperty()
|
||||
email: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(6)
|
||||
@ApiProperty()
|
||||
password: string;
|
||||
}
|
||||
24
server/src/user/dto/update-user.dto.ts
Normal file
24
server/src/user/dto/update-user.dto.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { IsString, IsEmail, MinLength, IsOptional } from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class UpdateUserDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@ApiProperty()
|
||||
username?: string;
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
@ApiProperty()
|
||||
email?: string;
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(6)
|
||||
@ApiProperty()
|
||||
password?: string;
|
||||
|
||||
}
|
||||
57
server/src/user/user.controller.ts
Normal file
57
server/src/user/user.controller.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
UseGuards,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { User } from '../entity/user';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { Roles } from '../auth/roles.decorator';
|
||||
import { Role } from '../auth/role.enum';
|
||||
import { RolesGuard } from '../auth/roles.guard';
|
||||
|
||||
@Controller('users')
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles(Role.Admin)
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Post()
|
||||
create(
|
||||
@Body(new ValidationPipe()) createUserDto: CreateUserDto,
|
||||
): Promise<User> {
|
||||
return this.userService.create(createUserDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(): Promise<User[]> {
|
||||
return this.userService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string): Promise<User | null> {
|
||||
return this.userService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Body(new ValidationPipe()) updateUserDto: UpdateUserDto,
|
||||
): Promise<User | null> {
|
||||
return this.userService.update(+id, updateUserDto);
|
||||
}
|
||||
|
||||
@Roles(Role.Admin)
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string): Promise<void> {
|
||||
return this.userService.remove(+id);
|
||||
}
|
||||
}
|
||||
13
server/src/user/user.module.ts
Normal file
13
server/src/user/user.module.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserService } from './user.service';
|
||||
import { UserController } from './user.controller';
|
||||
import { User } from '../entity/user';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User])],
|
||||
providers: [UserService],
|
||||
controllers: [UserController],
|
||||
exports: [UserService],
|
||||
})
|
||||
export class UserModule {}
|
||||
57
server/src/user/user.service.ts
Normal file
57
server/src/user/user.service.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { User } from '../entity/user';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { FindOptionsRelations } from 'typeorm/find-options/FindOptionsRelations';
|
||||
import { DvbookingLoggerService } from '../logger/dvbooking-logger.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private usersRepository: Repository<User>,
|
||||
private readonly logger: DvbookingLoggerService,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<User[]> {
|
||||
this.logger.log('Finding all users', 'UserService');
|
||||
return this.usersRepository.find();
|
||||
}
|
||||
|
||||
findOne(id: number): Promise<User | null> {
|
||||
this.logger.log(`Finding user with id: ${id}`, 'UserService');
|
||||
return this.usersRepository.findOneBy({ id });
|
||||
}
|
||||
|
||||
findByUsername(
|
||||
username: string,
|
||||
relations: FindOptionsRelations<User>,
|
||||
): Promise<User | null> {
|
||||
this.logger.log(`Finding user with username: ${username}`, 'UserService');
|
||||
return this.usersRepository.findOne({ where: { username }, relations });
|
||||
}
|
||||
|
||||
async create(user: Partial<User>): Promise<User> {
|
||||
this.logger.log('Creating a new user', 'UserService');
|
||||
if (user.password) {
|
||||
user.password = await bcrypt.hash(user.password, 12);
|
||||
}
|
||||
const newUser = this.usersRepository.create(user);
|
||||
return this.usersRepository.save(newUser);
|
||||
}
|
||||
|
||||
async update(id: number, user: Partial<User>): Promise<User | null> {
|
||||
this.logger.log(`Updating user with id: ${id}`, 'UserService');
|
||||
if (user.password) {
|
||||
user.password = await bcrypt.hash(user.password, 12);
|
||||
}
|
||||
await this.usersRepository.update(id, user);
|
||||
return this.usersRepository.findOneBy({ id });
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
this.logger.log(`Removing user with id: ${id}`, 'UserService');
|
||||
await this.usersRepository.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user