From 893d60a56f3dbb6cc8d714900f8591f550886873 Mon Sep 17 00:00:00 2001 From: seungh1024 Date: Sun, 25 Feb 2024 00:32:27 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20code=20smell=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bankaccount/exception/AccountErrorCode.java | 4 ++++ .../bankaccount/exception/AccountException.java | 8 ++++++++ .../bankaccount/limit/ChargeLimitManager.java | 6 +++--- .../bankaccount/product/ProductManager.java | 2 +- .../bankaccount/service/MainAccountService.java | 11 ++++------- .../member/interceptor/LoginInterceptor.java | 2 +- .../org/c4marathon/assignment/SetContainerTest.java | 2 -- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountErrorCode.java b/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountErrorCode.java index d35bc78c0..48f6d2460 100644 --- a/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountErrorCode.java +++ b/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountErrorCode.java @@ -16,6 +16,10 @@ public enum AccountErrorCode implements ErrorCode { private final HttpStatus httpStatus; private final String message; + public AccountException accountException() { + return new AccountException(getHttpStatus(), name(), getMessage()); + } + public AccountException accountException(String debugMessage) { return new AccountException(getHttpStatus(), name(), getMessage(), debugMessage); } diff --git a/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountException.java b/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountException.java index 83f2113f1..7b16ab506 100644 --- a/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountException.java +++ b/src/main/java/org/c4marathon/assignment/bankaccount/exception/AccountException.java @@ -11,6 +11,14 @@ public class AccountException extends RuntimeException { private final String errorMessage; private final String debugMessage; + public AccountException(HttpStatus httpStatus, String errorName, String errorMessage) { + super(errorMessage); + this.httpStatus = httpStatus; + this.errorName = errorName; + this.errorMessage = errorMessage; + this.debugMessage = null; + } + public AccountException(HttpStatus httpStatus, String errorName, String errorMessage, String debugMessage) { super(errorMessage); diff --git a/src/main/java/org/c4marathon/assignment/bankaccount/limit/ChargeLimitManager.java b/src/main/java/org/c4marathon/assignment/bankaccount/limit/ChargeLimitManager.java index 17ec606aa..3295b3956 100644 --- a/src/main/java/org/c4marathon/assignment/bankaccount/limit/ChargeLimitManager.java +++ b/src/main/java/org/c4marathon/assignment/bankaccount/limit/ChargeLimitManager.java @@ -13,7 +13,7 @@ @Component @RequiredArgsConstructor public class ChargeLimitManager { - private final RedisTemplate redisTemplate; + private final RedisTemplate redisTemplate; private final MainAccountRepository mainAccountRepository; public long get(long pk) { @@ -22,11 +22,11 @@ public long get(long pk) { public boolean charge(long pk, long money) { Long limit = getChargeLimit(pk); - System.out.println("limit: " + limit); + if (limit >= money) { limit -= money; redisTemplate.opsForValue().set(pk, limit); - System.out.println("db limit: " + limit); + return true; } return false; diff --git a/src/main/java/org/c4marathon/assignment/bankaccount/product/ProductManager.java b/src/main/java/org/c4marathon/assignment/bankaccount/product/ProductManager.java index aba07bbcc..41a60d96d 100644 --- a/src/main/java/org/c4marathon/assignment/bankaccount/product/ProductManager.java +++ b/src/main/java/org/c4marathon/assignment/bankaccount/product/ProductManager.java @@ -21,7 +21,7 @@ public class ProductManager { public List getProductInfo() { List productList = savingProductRepository.findAll(); return productList.stream() - .map(product -> new SavingProductResponseDto(product)) + .map(SavingProductResponseDto::new) .toList(); } diff --git a/src/main/java/org/c4marathon/assignment/bankaccount/service/MainAccountService.java b/src/main/java/org/c4marathon/assignment/bankaccount/service/MainAccountService.java index e708d1087..7a5081d5e 100644 --- a/src/main/java/org/c4marathon/assignment/bankaccount/service/MainAccountService.java +++ b/src/main/java/org/c4marathon/assignment/bankaccount/service/MainAccountService.java @@ -37,8 +37,7 @@ public long chargeMoney(long mainAccountPk, long money) { } MainAccount mainAccount = mainAccountRepository.findByPkForUpdate(mainAccountPk) - .orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException( - "존재하지 않는 계좌, mainAccountPk = " + mainAccountPk)); + .orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException()); mainAccount.chargeMoney(money); mainAccountRepository.save(mainAccount); @@ -49,8 +48,7 @@ public long chargeMoney(long mainAccountPk, long money) { @Transactional(isolation = Isolation.READ_COMMITTED) public void sendToSavingAccount(long mainAccountPk, long savingAccountPk, long money) { MainAccount mainAccount = mainAccountRepository.findByPkForUpdate(mainAccountPk) - .orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException( - "존재하지 않는 계좌, mainAccountPk = " + mainAccountPk)); + .orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException()); if (!isSendValid(mainAccount.getMoney(), money)) { throw AccountErrorCode.INVALID_MONEY_SEND.accountException( @@ -58,8 +56,7 @@ public void sendToSavingAccount(long mainAccountPk, long savingAccountPk, long m } SavingAccount savingAccount = savingAccountRepository.findByPkForUpdate(savingAccountPk) - .orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException( - "존재하지 않는 계좌, savingAccountPk = " + savingAccountPk)); + .orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException()); savingAccount.addMoney(money); savingAccountRepository.save(savingAccount); @@ -71,7 +68,7 @@ public MainAccountResponseDto getMainAccountInfo(long mainAccountPk) { MainAccount mainAccount = mainAccountRepository.findById(mainAccountPk) .orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException( "존재하지 않는 계좌, mainAccountPk = " + mainAccountPk)); - System.out.println(new MainAccountResponseDto(mainAccount)); + return new MainAccountResponseDto(mainAccount); } diff --git a/src/main/java/org/c4marathon/assignment/member/interceptor/LoginInterceptor.java b/src/main/java/org/c4marathon/assignment/member/interceptor/LoginInterceptor.java index 3f44bcf41..6ed779563 100644 --- a/src/main/java/org/c4marathon/assignment/member/interceptor/LoginInterceptor.java +++ b/src/main/java/org/c4marathon/assignment/member/interceptor/LoginInterceptor.java @@ -21,7 +21,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons HttpSession session = request.getSession(); SessionMemberInfo memberInfo = (SessionMemberInfo)session.getAttribute(SessionConst.MEMBER_INFO); - if (session == null || memberInfo == null) { + if (memberInfo == null) { HttpStatus httpStatus = CommonErrorCode.UNAUTHORIZED_USER.getHttpStatus(); String message = CommonErrorCode.UNAUTHORIZED_USER.getMessage(); ErrorResponse errorResponse = ErrorResponse.of(httpStatus, message); diff --git a/src/test/java/org/c4marathon/assignment/SetContainerTest.java b/src/test/java/org/c4marathon/assignment/SetContainerTest.java index f15f665f7..cc87e9852 100644 --- a/src/test/java/org/c4marathon/assignment/SetContainerTest.java +++ b/src/test/java/org/c4marathon/assignment/SetContainerTest.java @@ -3,12 +3,10 @@ import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; import org.testcontainers.utility.DockerImageName; import com.redis.testcontainers.RedisContainer; -@Testcontainers public class SetContainerTest { private static final String REDIS_IMAGE = "redis:latest";