Skip to content

Commit

Permalink
#42 [feat] : 대표 코인 정보 조회 API 구현 (GET)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Jun 5, 2024
1 parent 4e0d50a commit ba689db
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ public ResponseEntity<ApiResponse<List<DashBoardDto.UserCoinResponse>>> getUserC
List<DashBoardDto.UserCoinResponse> userCoins = dashBoardService.getUserCoins(authorizationHeader);
return ApiResponse.onSuccess(SuccessStatus.SUCCESS_GET_USER_COINS, userCoins);
}

// 대표 코인 5개 조회 API
@GetMapping("/coins/representative")
public ResponseEntity<ApiResponse<List<DashBoardDto.RepresentativeCoinResponse>>> getRepresentativeCoins() {

List<DashBoardDto.RepresentativeCoinResponse> representativeCoinResponses = dashBoardService.getRepresentativeCoins();
return ApiResponse.onSuccess(SuccessStatus.SUCCESS_GET_REPRESENTATIVE_COINS, representativeCoinResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public interface DashBoardService {
DashBoardDto.UserAccountResponse getUserAccount(String authorizationHeader);
List<DashBoardDto.UserCoinResponse> getUserCoins(String authorizationHeader);
List<DashBoardDto.RepresentativeCoinResponse> getRepresentativeCoins();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dgu.backend.common.constant.Coin;
import org.dgu.backend.domain.UpbitKey;
import org.dgu.backend.domain.User;
import org.dgu.backend.domain.UserCoin;
Expand Down Expand Up @@ -40,7 +41,7 @@ public DashBoardDto.UserAccountResponse getUserAccount(String authorizationHeade

String token = jwtUtil.generateUpbitToken(upbitKey.getAccessKey(), upbitKey.getSecretKey());
String url = "https://api.upbit.com/v1/accounts";
UpbitDto.Account[] responseBody = connectUpbitApi(url, token);
UpbitDto.Account[] responseBody = getUserAccountsAtUpbit(url, token);
if (Objects.isNull(responseBody)) {
throw new UpbitException(UpbitErrorResult.FAIL_ACCESS_USER_ACCOUNT);
}
Expand Down Expand Up @@ -69,7 +70,7 @@ public List<DashBoardDto.UserCoinResponse> getUserCoins(String authorizationHead

String token = jwtUtil.generateUpbitToken(upbitKey.getAccessKey(), upbitKey.getSecretKey());
String url = "https://api.upbit.com/v1/accounts";
UpbitDto.Account[] responseBody = connectUpbitApi(url, token);
UpbitDto.Account[] responseBody = getUserAccountsAtUpbit(url, token);
if (Objects.isNull(responseBody)) {
throw new UpbitException(UpbitErrorResult.FAIL_ACCESS_USER_ACCOUNT);
}
Expand Down Expand Up @@ -102,6 +103,22 @@ public List<DashBoardDto.UserCoinResponse> getUserCoins(String authorizationHead
return userCoinResponses;
}

// 대표 코인 5개 정보를 반환하는 메서드
@Override
public List<DashBoardDto.RepresentativeCoinResponse> getRepresentativeCoins() {
String url = "https://api.upbit.com/v1/ticker?markets=";
List<DashBoardDto.RepresentativeCoinResponse> representativeCoinResponses = new ArrayList<>();
for (Coin coin : Coin.values()) {
UpbitDto.Ticker[] responseBody = getTickerPriceAtUpbit(url + coin.getMarketName());
if (Objects.isNull(responseBody[0])) {
throw new UpbitException(UpbitErrorResult.FAIL_ACCESS_COIN_INFO);
}
representativeCoinResponses.add(DashBoardDto.RepresentativeCoinResponse.of(responseBody[0], coin.getKoreanName()));
}

return representativeCoinResponses;
}

// 코인 가격 상승 여부를 판단하는 메서드
private boolean isBalanceIncreased(UpbitDto.Account account, UserCoin userCoin) {
BigDecimal curBalance = BigDecimal.valueOf(account.getBalance())
Expand All @@ -110,8 +127,8 @@ private boolean isBalanceIncreased(UpbitDto.Account account, UserCoin userCoin)
return curBalance.compareTo(userCoin.getBalance()) > 0;
}

// 업비트 API와 통신하는 메서드
private UpbitDto.Account[] connectUpbitApi(String url, String token) {
// 전체 계좌 조회 업비트 API와 통신하는 메서드
private UpbitDto.Account[] getUserAccountsAtUpbit(String url, String token) {
String authenticationToken = "Bearer " + token;
HttpHeaders headers = new HttpHeaders();
headers.set("accept", MediaType.APPLICATION_JSON_VALUE);
Expand All @@ -125,4 +142,18 @@ private UpbitDto.Account[] connectUpbitApi(String url, String token) {

return responseEntity.getBody();
}

// 시세 현재가 조회 업비트 API와 통신하는 메서드
private UpbitDto.Ticker[] getTickerPriceAtUpbit(String url) {
HttpHeaders headers = new HttpHeaders();
headers.set("accept", MediaType.APPLICATION_JSON_VALUE);
ResponseEntity<UpbitDto.Ticker[]> responseEntity = restTemplate.exchange(
url,
HttpMethod.GET,
new HttpEntity<>(headers),
UpbitDto.Ticker[].class
);

return responseEntity.getBody();
}
}

0 comments on commit ba689db

Please sign in to comment.