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 2006f1f commit 025cafb
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.dgu.backend.controller;

import lombok.RequiredArgsConstructor;
import org.dgu.backend.common.ApiResponse;
import org.dgu.backend.common.constant.SuccessStatus;
import org.dgu.backend.dto.DashBoardDto;
import org.dgu.backend.service.DashBoardService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/dashboard")
@RequiredArgsConstructor
public class DashBoardController {
private final DashBoardService dashBoardService;

// 유저 업비트 잔고 조회 API
@GetMapping("/accounts")
public ResponseEntity<ApiResponse<DashBoardDto.UserAccountResponse>> getUserBalance(
@RequestHeader("Authorization") String authorizationHeader) {

DashBoardDto.UserAccountResponse userBalanceResponse = dashBoardService.getUserAccount(authorizationHeader);
return ApiResponse.onSuccess(SuccessStatus.SUCCESS_GET_USER_BALANCE, userBalanceResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.dgu.backend.service;

import org.dgu.backend.dto.DashBoardDto;

public interface DashBoardService {
DashBoardDto.UserAccountResponse getUserAccount(String authorizationHeader);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.dgu.backend.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dgu.backend.domain.UpbitKey;
import org.dgu.backend.domain.User;
import org.dgu.backend.dto.DashBoardDto;
import org.dgu.backend.dto.UpbitDto;
import org.dgu.backend.exception.UpbitErrorResult;
import org.dgu.backend.exception.UpbitException;
import org.dgu.backend.repository.UpbitKeyRepository;
import org.dgu.backend.util.JwtUtil;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

@Service
@Transactional
@RequiredArgsConstructor
@Slf4j
public class DashBoardServiceImpl implements DashBoardService {
private final RestTemplate restTemplate;
private final JwtUtil jwtUtil;
private final UpbitKeyRepository upbitKeyRepository;

// 유저 업비트 잔고를 반환하는 메서드
@Override
public DashBoardDto.UserAccountResponse getUserAccount(String authorizationHeader) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
UpbitKey upbitKey = upbitKeyRepository.findByUser(user);

String token = jwtUtil.generateUpbitToken(upbitKey.getAccessKey(), upbitKey.getSecretKey());
String url = "https://api.upbit.com/v1/accounts";
UpbitDto.Account[] responseBody = connectUpbitApi(url, token);
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 += account.getBalance();
} else {
accountSum += (long) (account.getBalance() * account.getAvgBuyPrice());
}
}

return DashBoardDto.UserAccountResponse.builder()
.account(accountSum)
.build();
}

// 업비트 API와 통신하는 메서드
private UpbitDto.Account[] connectUpbitApi(String url, String token) {
String authenticationToken = "Bearer " + token;
HttpHeaders headers = new HttpHeaders();
headers.set("accept", MediaType.APPLICATION_JSON_VALUE);
headers.add("Authorization", authenticationToken);
ResponseEntity<UpbitDto.Account[]> responseEntity = restTemplate.exchange(
url,
HttpMethod.GET,
new HttpEntity<>(headers),
UpbitDto.Account[].class
);

return responseEntity.getBody();
}
}

0 comments on commit 025cafb

Please sign in to comment.