인생 디벨로퍼

[Bank App] 6강 회원가입 만들기 본문

Project/개인 Project - Bank App

[Bank App] 6강 회원가입 만들기

뫄뫙뫄 2023. 6. 10. 02:44
728x90
반응형

DTO 생성

package shop.mtcoding.bankapp.dto.user;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class JoinReqDto {
    private String username;
    private String password;
    private String fullname;
}

Controller

@PostMapping("join")
    public String join(JoinReqDto joinReqDto) {
        // 1. 인증
        // 2. 유효성 검사
        if (joinReqDto.getUsername() == null || joinReqDto.getUsername().isEmpty()) {
            throw new CustomException("username 을 입력해주세요", HttpStatus.BAD_REQUEST);
        }
        if (joinReqDto.getPassword() == null || joinReqDto.getPassword().isEmpty()) {
            throw new CustomException("password 을 입력해주세요", HttpStatus.BAD_REQUEST);
        }
        if (joinReqDto.getFullname() == null || joinReqDto.getFullname().isEmpty()) {
            throw new CustomException("fullname 을 입력해주세요", HttpStatus.BAD_REQUEST);
        }
        // 3. 서비스 호출
        return "redirect:/loginForm";
    }

Full-Name 을 입력하지 않았을때, 


Service 만들기

package shop.mtcoding.bankapp.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import shop.mtcoding.bankapp.dto.user.JoinReqDto;
import shop.mtcoding.bankapp.handler.ex.CustomException;
import shop.mtcoding.bankapp.model.user.UserRepository;

@Service // Ioc
public class UserService {
    
    @Autowired // DI
    private UserRepository userRepository;

    @Transactional
    public void 회원가입(JoinReqDto joinReqDto) {
        int result = userRepository.insert(joinReqDto);
        if (result != 1) {
            throw new CustomException("회원가입 실패", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}


결과 확인

 


  • DI (Dependency Injection) : 의존성 주입 객체를 직접 생성하는 게 아니라 외부에서 생성한 후 주입 시켜주는 방식

  • IoC (Inversion of Control) : "제어의 역전" 이라는 의미로, 말 그대로 메소드나 객체의 호출작업을 개발자가 결정하는 것이 아니라, 외부에서 결정되는 것을 의미

728x90
반응형