Skip to content

Commit

Permalink
refactor: code smell 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
seungh1024 committed Feb 24, 2024
1 parent f0a79a5 commit 893d60a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Component
@RequiredArgsConstructor
public class ChargeLimitManager {
private final RedisTemplate redisTemplate;
private final RedisTemplate<Long, Long> redisTemplate;
private final MainAccountRepository mainAccountRepository;

public long get(long pk) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ProductManager {
public List<SavingProductResponseDto> getProductInfo() {
List<SavingProduct> productList = savingProductRepository.findAll();
return productList.stream()
.map(product -> new SavingProductResponseDto(product))
.map(SavingProductResponseDto::new)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -49,17 +48,15 @@ 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(
"현재 잔고 부족, mainAccount.getMoney() = " + mainAccount.getMoney() + ", send money = " + money);
}

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);

Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/c4marathon/assignment/SetContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 893d60a

Please sign in to comment.