π 곡λΆνλ μ§μ§μνμΉ΄λ μ²μμ΄μ§?
[Nest JS λ‘ CRUD κ²μν λ§λ€κΈ°] (22) νμ κ°μ κΈ°λ₯ ꡬν λ³Έλ¬Έ
π©π» λ°±μλ(Back-End)/Nest js
[Nest JS λ‘ CRUD κ²μν λ§λ€κΈ°] (22) νμ κ°μ κΈ°λ₯ ꡬν
μ§μ§μνμΉ΄ 2023. 6. 7. 20:17728x90
λ°μν
<λ³Έ λΈλ‘κ·Έλ John Ahn μ μ νλΈλ₯Ό μ°Έκ³ ν΄μ 곡λΆνλ©° μμ±νμμ΅λλ€ :-)>
=> λ°λΌνλ©΄μ λ°°μ°λ NestJS
π§Έ νμ κ°μ κΈ°λ₯ ꡬν
π user.repository.ts
import { EntityRepository, Repository } from "typeorm";
import { User } from "./user.entity";
import { AuthCredentialsDto } from "./dto/auth-credential.dto";
@EntityRepository(User)
export class UserRepository extends Repository<User> {
async createUser(authCredentialsDto: AuthCredentialsDto): Promise<void> {
const { username, password } = authCredentialsDto;
const user = this.create({ username, password });
await this.save(user);
}
}
π auth/dto/auth-credential.dto.ts
export class AuthCredentialsDto {
username: string;
password: string;
}
π auth.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { UserRepository } from './user.repository';
import { AuthCredentialsDto } from './dto/auth-credential.dto';
@Injectable()
export class AuthService {
constructor(
@InjectRepository(UserRepository)
private userRepository: UserRepository,
) {}
async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
return this.userRepository.createUser(authCredentialsDto);
}
}
π auth.controller.ts
import { Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthCredentialsDto } from './dto/auth-credential.dto';
@Controller('auth')
export class AuthController {
constructor( private authService: AuthService) {}
// localhost:3000/auth/signUp
@Post("/signup")
signUp(@Body() authCredentialsDto: AuthCredentialsDto): Promise<void> {
return this.authService.signUp(authCredentialsDto);
}
}
728x90
λ°μν
'π©βπ» λ°±μλ(Back-End) > Nest js' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Comments