Skip to content

Commit

Permalink
refactor: 컨트롤러 계층 코드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
Gseungmin committed Dec 19, 2023
1 parent a177ffe commit 8b52b8b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 127 deletions.
135 changes: 8 additions & 127 deletions src/main/java/com/umc/refit/web/controller/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.umc.refit.web.service.EmailService;
import com.umc.refit.web.service.MemberService;

import com.umc.refit.web.service.ValidateService;
import jakarta.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -30,60 +31,34 @@ public class MemberController {

private final EmailService emailService;
private final MemberService memberService;
private final ValidateService validateService;

@Value("${member.image}")
private String imageUrl;

/*이메일 인증 API*/
@PostMapping("/email")
public ResEmailDto email(@RequestBody EmailDto emailDto) throws MessagingException {

String email = emailDto.getEmail();
emailCheck(email);

validateService.emailCheck(email);
String auth = emailService.sendEmail(emailDto.getEmail());

return new ResEmailDto(auth);
}

@PostMapping("/join/name")
public void checkName(@RequestBody NameDto nameDto) {
String name = nameDto.getName();
nameCheck(name);
validateService.nameCheck(name);
}

/*회원 가입 API*/
@PostMapping("/join")
public void join(@RequestBody JoinDto joinDto) {

String loginId = joinDto.getLoginId();
String password = joinDto.getPassword();
String email = joinDto.getEmail();
String name = joinDto.getName();
String birth = joinDto.getBirth();
Integer gender = joinDto.getGender();

/*예외 체크*/
loginIdCheck(loginId);
passwordCheck(password);
emailCheck(email);
nameCheck(name);
birthCheck(birth);
genderCheck(gender);

/*예외 처리가 끝나면 회원 저장*/
validateService.validateJoin(joinDto);
memberService.save(new Member(joinDto, imageUrl));
}

/*이메일 인증 API*/
@PostMapping("/reset/password")
public void reset(@RequestBody PasswordResetDto passwordResetDto) throws MessagingException {

String email = passwordResetDto.getEmail();
String name = passwordResetDto.getName();
String loginId = passwordResetDto.getLoginId();

Optional<Member> member = memberService.findMemberForPasswordRest(name, email, loginId);
public void reset(@RequestBody PasswordResetDto dto) throws MessagingException {
Optional<Member> member = memberService.findMemberForPasswordRest(dto.getName(), dto.getEmail(), dto.getLoginId());

if (member.isEmpty()) {
throw new MemberException(PASSWORD_RESET_FAIL, PASSWORD_RESET_FAIL.getCode(), PASSWORD_RESET_FAIL.getErrorMessage());
Expand All @@ -93,48 +68,40 @@ public void reset(@RequestBody PasswordResetDto passwordResetDto) throws Messagi
throw new MemberException(PASSWORD_RESET_FAIL, PASSWORD_RESET_FAIL.getCode(), PASSWORD_RESET_FAIL.getErrorMessage());
}

String password = emailService.resetEmail(email, name);
String password = emailService.resetEmail(dto.getEmail(), dto.getName());

Member getMember = member.get();
getMember.setPassword(password);
memberService.save(getMember);
}

/*아이디 찾기 API*/
@PostMapping("/find/id")
public ResIdFindDto findId(@RequestBody IdFindDto idFindDto) {

String email = idFindDto.getEmail();
String name = idFindDto.getName();

/*예외 처리가 끝나면 회원 조회*/
Optional<Member> member = memberService.findMemberForFindId(name, email);

/*회원이 조회되지 않으면 예외*/
if (member.isEmpty()) {
throw new MemberException(MEMBER_IS_NOT_EXIST, MEMBER_IS_NOT_EXIST.getCode(), MEMBER_IS_NOT_EXIST.getErrorMessage());
}

/*뒤 세글자 ***로 변환*/
String lastThreeReplaced = "***";
String exceptLastThree = member.get().getLoginId().substring(0, member.get().getLoginId().length() - 3);

return new ResIdFindDto(exceptLastThree + lastThreeReplaced);
}

/*일반 로그인 API*/
@PostMapping("/login")
public ResponseEntity<Void> login() {
return ResponseEntity.ok().build();
}

/*카카오 로그인 API*/
@PostMapping("/kakao")
public ResponseEntity<Void> kakao_login() {
return ResponseEntity.ok().build();
}

/*로그아웃 API*/
@PostMapping("/logout")
public ResponseEntity<Void> logout(Authentication authentication, HttpServletRequest request) {

Expand All @@ -149,13 +116,11 @@ public ResponseEntity<Void> logout(Authentication authentication, HttpServletReq
return ResponseEntity.ok().build();
}

/*엑세스 토큰 체크 API*/
@GetMapping("/token/check")
public ResponseEntity<Void> token_check() {
return ResponseEntity.ok().build();
}

/*토큰 재발급 API*/
@PostMapping("/token/refresh")
public ResponseEntity<Void> token_reissue(Authentication authentication, HttpServletRequest request
, HttpServletResponse response) throws IOException {
Expand All @@ -174,90 +139,6 @@ public ResponseEntity<Void> token_reissue(Authentication authentication, HttpSer
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(responseDto);
response.getWriter().write(jsonString);

return ResponseEntity.ok().build();
}

/*로그인 아이디 체크 메서드*/
private void loginIdCheck(String loginId) {
//예외 코드 10011: 아이디가 비어있을 경우
if (loginId.strip().equals("")) {
throw new MemberException(ID_EMPTY, ID_EMPTY.getCode(), ID_EMPTY.getErrorMessage());
}

//예외 코드 10012: 아이디가 형식에 맞지 않은 경우
if (!MemberValidator.isLoginValid(loginId)) {
throw new MemberException(ID_INVALID, ID_INVALID.getCode(), ID_INVALID.getErrorMessage());
}

//예외 코드 10013: 아이디가 이미 존재하는 경우
if (memberService.findMemberByLoginId(loginId).isPresent()) {
throw new MemberException(ID_ALREADY_EXIST, ID_ALREADY_EXIST.getCode(), ID_ALREADY_EXIST.getErrorMessage());
}
}

/*비밀번호 체크 메서드*/
private void passwordCheck(String password) {
//예외 코드 10014: 비밀번호는 필수 정보입니다.
if (password.strip().equals("")) {
throw new MemberException(PASSWORD_EMPTY, PASSWORD_EMPTY.getCode(), PASSWORD_EMPTY.getErrorMessage());
}

//예외 코드 10015: "8-16자의 영문 대소문자, 숫자, 특수문자 ((!), (_) , (-))를 포합해야합니다.
if (!MemberValidator.isPasswordValid(password)) {
throw new MemberException(PASSWORD_INVALID, PASSWORD_INVALID.getCode(), PASSWORD_INVALID.getErrorMessage());
}
}

/*이메일 체크 메서드*/
private void emailCheck(String email) {
//예외 코드 10016: 이메일이 비어있을 경우
if (email.strip().equals("")) {
throw new MemberException(EMAIL_EMPTY, EMAIL_EMPTY.getCode(), EMAIL_EMPTY.getErrorMessage());
}

//예외 코드 10017: 이미 존재하는 회원일 경우
if (memberService.findMemberByEmail(email).isPresent()) {
throw new MemberException(EMAIL_ALREADY_EXIST, EMAIL_ALREADY_EXIST.getCode(), EMAIL_ALREADY_EXIST.getErrorMessage());
}

//예외 코드 10018: 이메일 형식에 맞지 않을 경우
if (!MemberValidator.isEmailValid(email)) {
throw new MemberException(EMAIL_INVALID, EMAIL_INVALID.getCode(), EMAIL_INVALID.getErrorMessage());
}
}

/*닉네임 체크 메서드*/
private void nameCheck(String name) {
//예외 코드 10019: 이름이 비어있을 경우
if (name.strip().equals("")) {
throw new MemberException(NAME_EMPTY, NAME_EMPTY.getCode(), NAME_EMPTY.getErrorMessage());
}

//예외 코드 10020: 이미 존재하는 이름일 경우
if (memberService.findMemberByName(name).isPresent()) {
throw new MemberException(NAME_ALREADY_EXIST, NAME_ALREADY_EXIST.getCode(), NAME_ALREADY_EXIST.getErrorMessage());
}
}

/*생일 체크 메서드*/
private void birthCheck(String birth) {
//예외 코드 10021: 생일이 비어있을 경우
if (birth.strip().equals("")) {
throw new MemberException(BIRTH_EMPTY, BIRTH_EMPTY.getCode(), BIRTH_EMPTY.getErrorMessage());
}

//예외 코드 10022: 생일이 형식에 맞지 않는 경우
if (!MemberValidator.isBirthValid(birth)) {
throw new MemberException(BIRTH_ALREADY_EXIST, BIRTH_ALREADY_EXIST.getCode(), BIRTH_ALREADY_EXIST.getErrorMessage());
}
}

/*성별 체크 메서드*/
private void genderCheck(Integer gender) {
//예외 코드 10021: 생일이 비어있을 경우
if (gender == null) {
throw new MemberException(GENDER_EMPTY, GENDER_EMPTY.getCode(), GENDER_EMPTY.getErrorMessage());
}
}
}
90 changes: 90 additions & 0 deletions src/main/java/com/umc/refit/web/service/ValidateService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.umc.refit.web.service;

import com.umc.refit.domain.dto.member.JoinDto;
import com.umc.refit.exception.member.MemberException;
import com.umc.refit.exception.validator.MemberValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.umc.refit.exception.ExceptionType.*;
import static com.umc.refit.exception.ExceptionType.GENDER_EMPTY;

@Service
@RequiredArgsConstructor
public class ValidateService {

private final MemberService memberService;

public void validateJoin(JoinDto joinDto) {
loginIdCheck(joinDto.getLoginId());
passwordCheck(joinDto.getPassword());
emailCheck(joinDto.getEmail());
nameCheck(joinDto.getName());
birthCheck(joinDto.getBirth());
genderCheck(joinDto.getGender());
}

public void loginIdCheck(String loginId) {
if (loginId.strip().equals("")) {
throw new MemberException(ID_EMPTY, ID_EMPTY.getCode(), ID_EMPTY.getErrorMessage());
}

if (!MemberValidator.isLoginValid(loginId)) {
throw new MemberException(ID_INVALID, ID_INVALID.getCode(), ID_INVALID.getErrorMessage());
}

if (memberService.findMemberByLoginId(loginId).isPresent()) {
throw new MemberException(ID_ALREADY_EXIST, ID_ALREADY_EXIST.getCode(), ID_ALREADY_EXIST.getErrorMessage());
}
}

public void passwordCheck(String password) {
if (password.strip().equals("")) {
throw new MemberException(PASSWORD_EMPTY, PASSWORD_EMPTY.getCode(), PASSWORD_EMPTY.getErrorMessage());
}

if (!MemberValidator.isPasswordValid(password)) {
throw new MemberException(PASSWORD_INVALID, PASSWORD_INVALID.getCode(), PASSWORD_INVALID.getErrorMessage());
}
}

public void emailCheck(String email) {
if (email.strip().equals("")) {
throw new MemberException(EMAIL_EMPTY, EMAIL_EMPTY.getCode(), EMAIL_EMPTY.getErrorMessage());
}

if (memberService.findMemberByEmail(email).isPresent()) {
throw new MemberException(EMAIL_ALREADY_EXIST, EMAIL_ALREADY_EXIST.getCode(), EMAIL_ALREADY_EXIST.getErrorMessage());
}

if (!MemberValidator.isEmailValid(email)) {
throw new MemberException(EMAIL_INVALID, EMAIL_INVALID.getCode(), EMAIL_INVALID.getErrorMessage());
}
}

public void nameCheck(String name) {
if (name.strip().equals("")) {
throw new MemberException(NAME_EMPTY, NAME_EMPTY.getCode(), NAME_EMPTY.getErrorMessage());
}

if (memberService.findMemberByName(name).isPresent()) {
throw new MemberException(NAME_ALREADY_EXIST, NAME_ALREADY_EXIST.getCode(), NAME_ALREADY_EXIST.getErrorMessage());
}
}

public void birthCheck(String birth) {
if (birth.strip().equals("")) {
throw new MemberException(BIRTH_EMPTY, BIRTH_EMPTY.getCode(), BIRTH_EMPTY.getErrorMessage());
}

if (!MemberValidator.isBirthValid(birth)) {
throw new MemberException(BIRTH_ALREADY_EXIST, BIRTH_ALREADY_EXIST.getCode(), BIRTH_ALREADY_EXIST.getErrorMessage());
}
}

public void genderCheck(Integer gender) {
if (gender == null) {
throw new MemberException(GENDER_EMPTY, GENDER_EMPTY.getCode(), GENDER_EMPTY.getErrorMessage());
}
}
}

0 comments on commit 8b52b8b

Please sign in to comment.