-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#42 [feat] : 유저 업비트 잔고 조회 API 구현 (GET)
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/org/dgu/backend/controller/DashBoardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/org/dgu/backend/service/DashBoardService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
71 changes: 71 additions & 0 deletions
71
backend/src/main/java/org/dgu/backend/service/DashBoardServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |