From 0def125cea4934ffffc63ab24e1bcddb4f04f4bb Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 10 Jun 2024 20:50:12 +0900 Subject: [PATCH 1/3] =?UTF-8?q?#48=20[refactor]=20:=20BigDecimal=EB=A1=9C?= =?UTF-8?q?=20=ED=83=80=EC=9E=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dgu/backend/dto/DashBoardDto.java | 3 +- .../java/org/dgu/backend/dto/UpbitDto.java | 8 +++-- .../backend/service/DashBoardServiceImpl.java | 36 +++++++++++-------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/backend/src/main/java/org/dgu/backend/dto/DashBoardDto.java b/backend/src/main/java/org/dgu/backend/dto/DashBoardDto.java index aaec7fe..569b40c 100644 --- a/backend/src/main/java/org/dgu/backend/dto/DashBoardDto.java +++ b/backend/src/main/java/org/dgu/backend/dto/DashBoardDto.java @@ -21,7 +21,8 @@ public class DashBoardDto { @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) @JsonInclude(JsonInclude.Include.NON_NULL) public static class UserAccountResponse { - private Long account; + @JsonSerialize(using = BigDecimalSerializer.class) + private BigDecimal account; } @Builder diff --git a/backend/src/main/java/org/dgu/backend/dto/UpbitDto.java b/backend/src/main/java/org/dgu/backend/dto/UpbitDto.java index 35b65e3..d587105 100644 --- a/backend/src/main/java/org/dgu/backend/dto/UpbitDto.java +++ b/backend/src/main/java/org/dgu/backend/dto/UpbitDto.java @@ -7,6 +7,8 @@ import lombok.*; import org.dgu.backend.domain.Market; +import java.math.BigDecimal; + public class UpbitDto { @Builder @Getter @@ -82,9 +84,9 @@ public static class CandleInfoResponse { @JsonInclude(JsonInclude.Include.NON_NULL) public static class Account { private String currency; - private Double balance; - private Double locked; - private Double avgBuyPrice; + private BigDecimal balance; + private BigDecimal locked; + private BigDecimal avgBuyPrice; private String unitCurrency; } diff --git a/backend/src/main/java/org/dgu/backend/service/DashBoardServiceImpl.java b/backend/src/main/java/org/dgu/backend/service/DashBoardServiceImpl.java index c548d81..ef5b7dc 100644 --- a/backend/src/main/java/org/dgu/backend/service/DashBoardServiceImpl.java +++ b/backend/src/main/java/org/dgu/backend/service/DashBoardServiceImpl.java @@ -20,6 +20,7 @@ import org.springframework.web.client.RestTemplate; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -49,20 +50,10 @@ public DashBoardDto.UserAccountResponse getUserAccount(String authorizationHeade if (Objects.isNull(responseBody)) { throw new UpbitException(UpbitErrorResult.FAIL_ACCESS_USER_ACCOUNT); } - - long accountSum = 0L; - for (UpbitDto.Account account : responseBody) { - if (account.getCurrency().equals("KRW")) { - accountSum += BigDecimal.valueOf(account.getBalance()).longValue(); - } else { - accountSum += BigDecimal.valueOf(account.getBalance()) - .multiply(BigDecimal.valueOf(account.getAvgBuyPrice())) - .longValue(); - } - } + BigDecimal accountSum = getAccountSum(responseBody); return DashBoardDto.UserAccountResponse.builder() - .account(accountSum) + .account(accountSum.setScale(3, RoundingMode.HALF_UP)) .build(); } @@ -98,8 +89,8 @@ public List getUserCoins(String authorizationHead DashBoardDto.UserCoinResponse userCoinResponse = DashBoardDto.UserCoinResponse.builder() .coinName(coinName) - .balance(BigDecimal.valueOf(account.getBalance())) - .price(BigDecimal.valueOf(account.getAvgBuyPrice())) + .balance(account.getBalance()) + .price(account.getAvgBuyPrice()) .isIncrease(isIncrease) .build(); userCoinResponses.add(userCoinResponse); @@ -126,10 +117,25 @@ public List getRepresentativeCoins() { return representativeCoinResponses; } + // 현재 업비트 잔고를 계산하는 메서드 + private BigDecimal getAccountSum(UpbitDto.Account[] responseBody) { + BigDecimal accountSum = BigDecimal.ZERO; + for (UpbitDto.Account account : responseBody) { + if (account.getCurrency().equals("KRW")) { + accountSum = accountSum.add(account.getBalance()); + } else { + BigDecimal balance = account.getBalance(); + BigDecimal avgBuyPrice = account.getAvgBuyPrice(); + accountSum = accountSum.add(balance.multiply(avgBuyPrice)); + } + } + return accountSum; + } + // 코인 가격 상승 여부를 판단하는 메서드 private boolean isBalanceIncreased(UpbitDto.Account account, UserCoin userCoin) { - return BigDecimal.valueOf(account.getBalance()).compareTo(userCoin.getBalance()) > 0; + return account.getBalance().compareTo(userCoin.getBalance()) > 0; } // 전체 계좌 조회 업비트 API와 통신하는 메서드 From e8e299014ceee55daa7a0c76809a66b20ddcc434 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 10 Jun 2024 20:50:27 +0900 Subject: [PATCH 2/3] =?UTF-8?q?#48=20[feat]=20:=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EC=BF=BC=EB=A6=AC=EB=AC=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dgu/backend/repository/UpbitKeyRepository.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/backend/src/main/java/org/dgu/backend/repository/UpbitKeyRepository.java b/backend/src/main/java/org/dgu/backend/repository/UpbitKeyRepository.java index 391ad34..38bb175 100644 --- a/backend/src/main/java/org/dgu/backend/repository/UpbitKeyRepository.java +++ b/backend/src/main/java/org/dgu/backend/repository/UpbitKeyRepository.java @@ -1,9 +1,17 @@ package org.dgu.backend.repository; +import jakarta.transaction.Transactional; import org.dgu.backend.domain.UpbitKey; import org.dgu.backend.domain.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; public interface UpbitKeyRepository extends JpaRepository { UpbitKey findByUser(User user); + + @Transactional + @Modifying(clearAutomatically = true) + @Query("delete from UpbitKey where id = :id") + void deleteUpbitKeyById(Long id); } \ No newline at end of file From 93b21590298a2cf96333825f9dbd1d4fa00d359a Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 10 Jun 2024 20:50:47 +0900 Subject: [PATCH 3/3] =?UTF-8?q?#48=20[feat]=20:=20=EC=97=85=EB=B9=84?= =?UTF-8?q?=ED=8A=B8=20=ED=82=A4=20=EB=93=B1=EB=A1=9D=20=EC=8B=9C,=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dgu/backend/service/UserServiceImpl.java | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/backend/src/main/java/org/dgu/backend/service/UserServiceImpl.java b/backend/src/main/java/org/dgu/backend/service/UserServiceImpl.java index f8248d4..006943d 100644 --- a/backend/src/main/java/org/dgu/backend/service/UserServiceImpl.java +++ b/backend/src/main/java/org/dgu/backend/service/UserServiceImpl.java @@ -13,11 +13,13 @@ import org.springframework.stereotype.Service; import java.security.KeyPair; +import java.util.Objects; @Service @Transactional @RequiredArgsConstructor public class UserServiceImpl implements UserService { + private final DashBoardService dashBoardService; private final UpbitKeyRepository upbitKeyRepository; private final JwtUtil jwtUtil; private final EncryptionUtil encryptionUtil; @@ -34,6 +36,9 @@ public void addUserUpbitKeys(String authorizationHeader, UserDto.UserUpbitKeyReq String encryptedPrivateKey = encryptionUtil.encryptPrivateKey(keyPair); saveUpbitKey(user, encodedAccessKey, encodedSecretKey, encryptedPrivateKey, existUpbitKey); + + // 업비트 키가 정상적으로 입력되었는지 확인 + dashBoardService.getUserAccount(authorizationHeader); } // 서비스 약관 동의 여부를 등록하는 메서드 @@ -56,20 +61,17 @@ public UserDto.getUserAgreementResponse getUserAgreement(String authorizationHea // 업비트 키 저장 메서드 private void saveUpbitKey(User user, String encodedAccessKey, String encodedSecretKey, String encryptedPrivateKey, UpbitKey existUpbitKey) { - // 기존 키 있는 경우 업데이트 - if (existUpbitKey != null) { - existUpbitKey.updateAccessKey(encodedAccessKey); - existUpbitKey.updateSecretKey(encodedSecretKey); - existUpbitKey.updatePrivateKey(encryptedPrivateKey); - upbitKeyRepository.save(existUpbitKey); - } else { - UpbitKey upbitKey = UpbitKey.builder() - .user(user) - .accessKey(encodedAccessKey) - .secretKey(encodedSecretKey) - .privateKey(encryptedPrivateKey) - .build(); - upbitKeyRepository.save(upbitKey); + // 기존 키가 있으면 삭제 + if (!Objects.isNull(existUpbitKey)) { + upbitKeyRepository.deleteUpbitKeyById(existUpbitKey.getId()); } + + UpbitKey upbitKey = UpbitKey.builder() + .user(user) + .accessKey(encodedAccessKey) + .secretKey(encodedSecretKey) + .privateKey(encryptedPrivateKey) + .build(); + upbitKeyRepository.save(upbitKey); } } \ No newline at end of file