😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[Nest JS 둜 CRUD κ²Œμ‹œνŒ λ§Œλ“€κΈ°] (22) νšŒμ› κ°€μž… κΈ°λŠ₯ κ΅¬ν˜„ λ³Έλ¬Έ

πŸ‘©‍πŸ’» λ°±μ—”λ“œ(Back-End)/Nest js

[Nest JS 둜 CRUD κ²Œμ‹œνŒ λ§Œλ“€κΈ°] (22) νšŒμ› κ°€μž… κΈ°λŠ₯ κ΅¬ν˜„

μ§•μ§•μ•ŒνŒŒμΉ΄ 2023. 6. 7. 20:17
728x90
λ°˜μ‘ν˜•

<λ³Έ λΈ”λ‘œκ·ΈλŠ” 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
λ°˜μ‘ν˜•
Comments