Skip to content

Commit

Permalink
[BE] ♻️ refactor : bank Enum 으로 변경 merge
Browse files Browse the repository at this point in the history
[BE] ♻️ refactor : bank Enum 으로 변경 #494
  • Loading branch information
hobeen-kim authored Oct 4, 2023
2 parents d2eb16a + 94bd021 commit 86f0c0e
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ public class Account extends BaseEntity {

private String account;

private String bank;
@Enumerated(EnumType.STRING)
private Bank bank;

private Account(String name, String account, String bank) {
private Account(String name, String account, Bank bank) {
this.name = name;
this.account = account;
this.bank = bank;
}

public static Account createAccount(String name, String accountNumber, String bank, Member member) {
public static Account createAccount(String name, String accountNumber, Bank bank, Member member) {
Account account = new Account(name, accountNumber, bank);

member.updateAccount(account);

return account;
}

public void updateAccount(String name, String account, String bank) {
public void updateAccount(String name, String account, Bank bank) {
this.name = name;
this.account = account;
this.bank = bank;
Expand Down
149 changes: 149 additions & 0 deletions Server/src/main/java/com/server/domain/account/domain/Bank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.server.domain.account.domain;

import com.server.global.entity.BaseEnum;
import lombok.RequiredArgsConstructor;

import java.util.function.Function;

@RequiredArgsConstructor
public enum Bank implements BaseEnum {
KB("KB 국민은행", (attribute) -> {
String[] chunk = attribute.split("-");
if(chunk.length == 3) {
return checkLen(chunk[0], 6) && checkLen(chunk[1], 2) && checkLen(chunk[2], 6);
}else if(chunk.length == 4) {
return checkLen(chunk[0], 3) &&
checkLen(chunk[1], 2) &&
checkLen(chunk[2], 4) &&
checkLen(chunk[3], 3);
}
return false;
}),
IBK("IBK 기업은행", (attribute) -> {
String[] chunk = attribute.split("-");
if(chunk.length == 4) {
return checkLen(chunk[0], 3) &&
checkLen(chunk[1], 6) &&
checkLen(chunk[2], 2) &&
checkLen(chunk[3], 3);
}
return false;
}),
NH("농협은행", (attribute) -> {
String[] chunk = attribute.split("-");
if(chunk.length == 4) {
return checkLen(chunk[0], 3) &&
checkLen(chunk[1], 4) &&
checkLen(chunk[2], 4) &&
checkLen(chunk[3], 2);
}
return false;
}),
SH("신한은행", (attribute) -> {
String[] chunk = attribute.split("-");
if(chunk.length == 3) {
return (checkLen(chunk[0], 3) &&
checkLen(chunk[1], 2) &&
checkLen(chunk[2], 6)) ||
(checkLen(chunk[0], 3) &&
checkLen(chunk[1], 3) &&
checkLen(chunk[2], 6));
}
return false;
}),
WR("우리은행", (attribute) -> {
String[] chunk = attribute.split("-");
if (chunk.length == 3) {
return checkLen(chunk[0], 4) && checkLen(chunk[1], 3) && checkLen(chunk[2], 6);
}
return false;
}),
HN("KEB 하나은행(구 외환은행)", (attribute) -> {
String[] chunk = attribute.split("-");
if(chunk.length == 3) {
return (checkLen(chunk[0], 3) &&
checkLen(chunk[1], 6) &&
checkLen(chunk[2], 5)) ||
(checkLen(chunk[0], 3) &&
checkLen(chunk[1], 6) &&
checkLen(chunk[2], 3));
}
return false;
}),
CITY("씨티은행", (attribute) -> {
String[] chunk = attribute.split("-");
if (chunk.length == 3) {
return checkLen(chunk[0], 3) && checkLen(chunk[1], 6) && checkLen(chunk[2], 3);
}
return false;
}),
DGB("DGB 대구은행", (attribute) -> {
String[] chunk = attribute.split("-");
if(chunk.length == 4) {
return checkLen(chunk[0], 3) &&
checkLen(chunk[1], 2) &&
checkLen(chunk[2], 6) &&
checkLen(chunk[3], 1);
}
return false;
}),
BNK("BNK 부산은행", (attribute) -> {
String[] chunk = attribute.split("-");
if(chunk.length == 4) {
return checkLen(chunk[0], 3) &&
checkLen(chunk[1], 4) &&
checkLen(chunk[2], 4) &&
checkLen(chunk[3], 2);
}
return false;
}),
SC("SC 제일은행", (attribute) -> {
String[] chunk = attribute.split("-");
if (chunk.length == 3) {
return checkLen(chunk[0], 3) && checkLen(chunk[1], 2) && checkLen(chunk[2], 6);
}
return false;
}),
KBANK("케이뱅크", (attribute) -> {
String[] chunk = attribute.split("-");
if (chunk.length == 3) {
return checkLen(chunk[0], 3) && checkLen(chunk[1], 3) && checkLen(chunk[2], 6);
}
return false;
}),
KAKAO("카카오뱅크", (attribute) -> {
String[] chunk = attribute.split("-");
if (chunk.length == 3) {
return checkLen(chunk[0], 4) && checkLen(chunk[1], 2) && checkLen(chunk[2], 7);
}
return false;
})
;

private final String description;

private final Function<String, Boolean> checkAccount;

public boolean checkAccount(String account) {
return this.checkAccount.apply(account);
}

@Override
public String getName() {
return name();
}

@Override
public String getDescription() {
return this.description;
}

private static boolean checkLen(String chunk, int len) {
try {
Long.parseLong(chunk);
return chunk.length() == len;
} catch (NumberFormatException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.server.domain.adjustment.controller.dto.request;

import com.server.domain.account.domain.Bank;
import com.server.domain.adjustment.service.dto.request.AccountUpdateServiceRequest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@AllArgsConstructor
@Getter
@Builder
public class AccountUpdateApiRequest {

@NotBlank(message = "{validation.account.name.notBlank}")
@Size(min = 2, max = 10, message = "{validation.size}")
private String name;
@NotBlank(message = "{validation.account.account.notBlank}")
@Size(min = 10, message = "{validation.size}")
private String account;
@NotBlank(message = "{validation.account.bank.notBlank}")
private String bank;
@NotNull(message = "{validation.account.bank.notBlank}")
private Bank bank;

public AccountUpdateServiceRequest toServiceRequest() {
return AccountUpdateServiceRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.server.domain.adjustment.domain;

import com.server.domain.account.domain.Bank;
import com.server.domain.member.entity.Member;
import com.server.global.entity.BaseEntity;
import lombok.AccessLevel;
Expand All @@ -25,7 +26,7 @@ public class Adjustment extends BaseEntity {

private String account;

private String bank;
private Bank bank;

private Integer amount;

Expand All @@ -38,7 +39,7 @@ public class Adjustment extends BaseEntity {
@JoinColumn(name = "member_id")
private Member member;

private Adjustment(Integer year, Integer month, String name, String account, String bank, Integer amount, AdjustmentStatus adjustmentStatus, String reason, Member member) {
private Adjustment(Integer year, Integer month, String name, String account, Bank bank, Integer amount, AdjustmentStatus adjustmentStatus, String reason, Member member) {
this.adjustmentYear = year;
this.adjustmentMonth = month;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.server.domain.adjustment.service;

import com.server.domain.account.domain.Account;
import com.server.domain.account.domain.Bank;
import com.server.domain.account.repository.AccountRepository;
import com.server.domain.adjustment.domain.Adjustment;
import com.server.domain.adjustment.domain.AdjustmentStatus;
Expand All @@ -11,6 +12,7 @@
import com.server.domain.adjustment.service.dto.response.*;
import com.server.domain.member.entity.Member;
import com.server.domain.member.repository.MemberRepository;
import com.server.global.exception.businessexception.accountexception.AccountNotValidException;
import com.server.global.exception.businessexception.memberexception.MemberNotFoundException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -72,6 +74,10 @@ public void updateAccount(Long loginMemberId, AccountUpdateServiceRequest reques

Account account = getAccountOrNull(loginMemberId);

if(!request.getBank().checkAccount(request.getAccount())) {
throw new AccountNotValidException();
}

if(account == null) {
Member member = verifiedMember(loginMemberId);
Account createdAccount = Account.createAccount(request.getName(), request.getAccount(), request.getBank(), member);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.server.domain.adjustment.service.dto.request;

import com.server.domain.account.domain.Bank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -11,5 +12,5 @@ public class AccountUpdateServiceRequest {

private String name;
private String account;
private String bank;
private Bank bank;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.server.domain.adjustment.service.dto.response;

import com.server.domain.account.domain.Account;
import com.server.domain.account.domain.Bank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -12,15 +13,14 @@ public class AccountResponse {

private String name;
private String account;
private String bank;
private Bank bank;

public static AccountResponse of(Account account) {

if(account == null) {
return AccountResponse.builder()
.name("계좌 정보가 없습니다.")
.account("계좌 정보가 없습니다.")
.bank("계좌 정보가 없습니다.")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.server.global.exception.businessexception.accountexception;

import com.server.global.exception.businessexception.BusinessException;
import org.springframework.http.HttpStatus;

public abstract class AccountException extends BusinessException {

protected AccountException(String errorCode, HttpStatus httpStatus, String message) {
super(errorCode, httpStatus, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.server.global.exception.businessexception.accountexception;

import com.server.global.exception.businessexception.answerexception.AnswerException;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class AccountNotValidException extends AccountException {

public static final String MESSAGE = "계좌 번호가 유효하지 않습니다.";
public static final String CODE = "ANSWER-400";

public AccountNotValidException() {
super(CODE, HttpStatus.BAD_REQUEST, MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.server.module.firmbank;

import com.server.domain.account.domain.Bank;
import com.server.module.firmbank.response.AdjustmentResult;

public interface FirmBankService {

AdjustmentResult adjustment(String name, String account, String bank, int amount);
AdjustmentResult adjustment(String name, String account, Bank bank, int amount);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.server.module.firmbank;

import com.server.domain.account.domain.Bank;
import com.server.domain.adjustment.domain.AdjustmentStatus;
import com.server.module.firmbank.response.AdjustmentResult;
import org.springframework.stereotype.Service;
Expand All @@ -8,7 +9,7 @@
public class MockFirmBankService implements FirmBankService {

@Override
public AdjustmentResult adjustment(String name, String account, String bank, int amount) {
public AdjustmentResult adjustment(String name, String account, Bank bank, int amount) {
return AdjustmentResult.builder()
.status(AdjustmentStatus.ADJUSTED)
.reason(AdjustmentStatus.ADJUSTED.getDescription())
Expand Down
Loading

0 comments on commit 86f0c0e

Please sign in to comment.