add user crud
This commit is contained in:
@@ -3,20 +3,21 @@ import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
|
||||
import { UserModule } from './user/user.module';
|
||||
import { User } from './entity/user';
|
||||
|
||||
const moduleTypeOrm = TypeOrmModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => {
|
||||
return {
|
||||
type: 'postgres',
|
||||
type: 'postgres',
|
||||
host: configService.get<string>('DATABASE_HOST'),
|
||||
port: parseInt(configService.get<string>('DATABASE_PORT') as string,10),
|
||||
username: configService.get<string>('DATABASE_USER'),
|
||||
password: configService.get<string>('DATABASE_PASS'),
|
||||
database: configService.get<string>('DATABASE_NAME'),
|
||||
entities: [],
|
||||
entities: [User],
|
||||
// synchronize: true,
|
||||
};
|
||||
},
|
||||
@@ -26,10 +27,10 @@ const moduleTypeOrm = TypeOrmModule.forRootAsync({
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
moduleTypeOrm
|
||||
moduleTypeOrm,
|
||||
UserModule
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
||||
export class AppModule {}
|
||||
|
||||
14
src/user/dto/create-user.dto.ts
Normal file
14
src/user/dto/create-user.dto.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { IsString, IsEmail, MinLength } from 'class-validator';
|
||||
|
||||
export class CreateUserDto {
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
username: string;
|
||||
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(6)
|
||||
password: string;
|
||||
}
|
||||
17
src/user/dto/update-user.dto.ts
Normal file
17
src/user/dto/update-user.dto.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { IsString, IsEmail, MinLength, IsOptional } from 'class-validator';
|
||||
|
||||
export class UpdateUserDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
username?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
email?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(6)
|
||||
password?: string;
|
||||
}
|
||||
47
src/user/user.controller.ts
Normal file
47
src/user/user.controller.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
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';
|
||||
|
||||
@Controller('users')
|
||||
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);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string): Promise<void> {
|
||||
return this.userService.remove(+id);
|
||||
}
|
||||
}
|
||||
12
src/user/user.module.ts
Normal file
12
src/user/user.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
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],
|
||||
})
|
||||
export class UserModule {}
|
||||
34
src/user/user.service.ts
Normal file
34
src/user/user.service.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { User } from '../entity/user';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private usersRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<User[]> {
|
||||
return this.usersRepository.find();
|
||||
}
|
||||
|
||||
findOne(id: number): Promise<User | null> {
|
||||
return this.usersRepository.findOneBy({ id });
|
||||
}
|
||||
|
||||
async create(user: Partial<User>): Promise<User> {
|
||||
const newUser = this.usersRepository.create(user);
|
||||
return this.usersRepository.save(newUser);
|
||||
}
|
||||
|
||||
async update(id: number, user: Partial<User>): Promise<User | null> {
|
||||
await this.usersRepository.update(id, user);
|
||||
return this.usersRepository.findOneBy({ id });
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
await this.usersRepository.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user