Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] : 마켓명 & 한글명 & 영어명 모두 반환하도록 리팩토링 #70

Merged
merged 4 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions backend/src/main/java/org/dgu/backend/constant/Coin.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package org.dgu.backend.constant;

public enum Coin {
BITCOIN("KRW-BTC", "비트코인"),
ETHEREUM("KRW-ETH", "이더리움"),
RIPPLE("KRW-XRP", "리플"),
DOGECOIN("KRW-DOGE", "도지코인"),
WAVE("KRW-WAVES", "웨이브");
BITCOIN("KRW-BTC", "비트코인", "Bitcoin"),
ETHEREUM("KRW-ETH", "이더리움", "Ethereum"),
RIPPLE("KRW-XRP", "리플", "Ripple"),
DOGECOIN("KRW-DOGE", "도지코인", "Dogecoin"),
WAVE("KRW-WAVES", "웨이브", "Waves");

private final String marketName;
private final String koreanName;
private final String englishName;

Coin(String marketName, String koreanName) {
Coin(String marketName, String koreanName, String englishName) {
this.marketName = marketName;
this.koreanName = koreanName;
this.englishName = englishName;
}

public String getMarketName() {
Expand All @@ -22,4 +24,8 @@ public String getMarketName() {
public String getKoreanName() {
return koreanName;
}

public String getEnglishName() {
return englishName;
}
}
16 changes: 12 additions & 4 deletions backend/src/main/java/org/dgu/backend/domain/UserCoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ public class UserCoin extends BaseEntity {
@JoinColumn(name = "users_id", foreignKey = @ForeignKey(name = "users_coins_fk_users_id"))
private User user;

@Column(name = "coin_name", nullable = false, length = 50)
private String coinName;
@Column(name = "market_name", nullable = false, length = 50)
private String marketName;

@Column(name = "korean_name", nullable = false, length = 50)
private String koreanName;

@Column(name = "english_name", nullable = false, length = 50)
private String englishName;

@Column(name = "coin_count", precision = 20, scale = 10, nullable = false)
private BigDecimal coinCount;
Expand All @@ -39,9 +45,11 @@ public class UserCoin extends BaseEntity {
private BigDecimal rate;

@Builder
public UserCoin(User user, String coinName, BigDecimal coinCount, BigDecimal price, BigDecimal balance, BigDecimal rate) {
public UserCoin(User user, String marketName, String koreanName, String englishName, BigDecimal coinCount, BigDecimal price, BigDecimal balance, BigDecimal rate) {
this.user = user;
this.coinName = coinName;
this.marketName = marketName;
this.koreanName = koreanName;
this.englishName = englishName;
this.coinCount = coinCount;
this.price = price;
this.balance = balance;
Expand Down
12 changes: 9 additions & 3 deletions backend/src/main/java/org/dgu/backend/dto/DashBoardDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static class UserAccountResponse {
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class UserCoinResponse {
private String coinName;
private String marketName;
private String koreanName;
private String englishName;
@JsonSerialize(using = BigDecimalSerializer.class)
private BigDecimal coinCount;
@JsonSerialize(using = BigDecimalSerializer.class)
Expand All @@ -44,7 +46,9 @@ public static class UserCoinResponse {
public UserCoin to(User user) {
return UserCoin.builder()
.user(user)
.coinName(coinName)
.marketName(marketName)
.koreanName(koreanName)
.englishName(englishName)
.coinCount(coinCount)
.price(price)
.balance(balance)
Expand All @@ -60,6 +64,7 @@ public UserCoin to(User user) {
public static class RepresentativeCoinResponse {
private String marketName;
private String koreanName;
private String englishName;
@JsonSerialize(using = BigDecimalSerializer.class)
private BigDecimal price;
@JsonSerialize(using = BigDecimalSerializer.class)
Expand All @@ -68,10 +73,11 @@ public static class RepresentativeCoinResponse {
private BigDecimal changeRate;
private Boolean isIncrease;

public static DashBoardDto.RepresentativeCoinResponse of(UpbitDto.Ticker ticker, String koreanName) {
public static DashBoardDto.RepresentativeCoinResponse of(UpbitDto.Ticker ticker, String koreanName, String englishName) {
return RepresentativeCoinResponse.builder()
.marketName(ticker.getMarket())
.koreanName(koreanName)
.englishName(englishName)
.changePrice(BigDecimal.valueOf(ticker.getPrice()))
.changeRate(BigDecimal.valueOf(ticker.getChangeRate()).setScale(5, RoundingMode.HALF_UP))
.isIncrease(ticker.getChange().equals("RISE"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface MarketRepository extends JpaRepository<Market,Long> {
Market findByKoreanName(String marketKoreanName);
Market findByKoreanName(String koreanName);
Market findByMarketName(String marketName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@


public interface UserCoinRepository extends JpaRepository<UserCoin,Long> {
UserCoin findByCoinName(String coinName);
UserCoin findByMarketName(String marketName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dgu.backend.constant.Coin;
import org.dgu.backend.domain.Market;
import org.dgu.backend.domain.UpbitKey;
import org.dgu.backend.domain.User;
import org.dgu.backend.domain.UserCoin;
import org.dgu.backend.dto.DashBoardDto;
import org.dgu.backend.dto.UpbitDto;
import org.dgu.backend.exception.MarketErrorResult;
import org.dgu.backend.exception.MarketException;
import org.dgu.backend.exception.UpbitErrorResult;
import org.dgu.backend.exception.UpbitException;
import org.dgu.backend.repository.MarketRepository;
import org.dgu.backend.repository.UpbitKeyRepository;
import org.dgu.backend.repository.UserCoinRepository;
import org.dgu.backend.util.JwtUtil;
Expand Down Expand Up @@ -39,6 +43,7 @@ public class DashBoardServiceImpl implements DashBoardService {
private final JwtUtil jwtUtil;
private final UpbitKeyRepository upbitKeyRepository;
private final UserCoinRepository userCoinRepository;
private final MarketRepository marketRepository;

// 유저 업비트 잔고를 반환하는 메서드
@Override
Expand Down Expand Up @@ -66,20 +71,24 @@ public List<DashBoardDto.UserCoinResponse> getUserCoins(String authorizationHead
private List<DashBoardDto.UserCoinResponse> processUserCoins(UpbitDto.Account[] accounts, User user) {
List<DashBoardDto.UserCoinResponse> userCoinResponses = new ArrayList<>();
for (UpbitDto.Account account : accounts) {
String coinName = account.getCurrency();
String marketName = account.getCurrency();
// 현금은 제외
if (!coinName.equals("KRW")) {
userCoinResponses.add(processSingleCoin(account, user, coinName));
if (!marketName.equals("KRW")) {
userCoinResponses.add(processSingleCoin(account, user, marketName));
}
}
return userCoinResponses;
}

// 단일 코인 정보를 처리하는 메서드
private DashBoardDto.UserCoinResponse processSingleCoin(UpbitDto.Account account, User user, String coinName) {
coinName = "KRW-" + coinName;
UserCoin userCoin = userCoinRepository.findByCoinName(coinName);
UpbitDto.Ticker[] ticker = getTickerPriceAtUpbit(UPBIT_URL_TICKER + coinName);
private DashBoardDto.UserCoinResponse processSingleCoin(UpbitDto.Account account, User user, String marketName) {
marketName = "KRW-" + marketName;
UserCoin userCoin = userCoinRepository.findByMarketName(marketName);
Market market = marketRepository.findByMarketName(marketName);
if (Objects.isNull(market)) {
throw new MarketException(MarketErrorResult.NOT_FOUND_MARKET);
}
UpbitDto.Ticker[] ticker = getTickerPriceAtUpbit(UPBIT_URL_TICKER + marketName);
BigDecimal curPrice = BigDecimal.valueOf(ticker[0].getPrice());
BigDecimal curCoinCount = account.getCoinCount();
boolean isIncrease = false;
Expand All @@ -91,7 +100,9 @@ private DashBoardDto.UserCoinResponse processSingleCoin(UpbitDto.Account account
}

DashBoardDto.UserCoinResponse userCoinResponse = DashBoardDto.UserCoinResponse.builder()
.coinName(coinName)
.marketName(marketName)
.koreanName(market.getKoreanName())
.englishName(market.getEnglishName())
.coinCount(curCoinCount)
.price(curPrice)
.balance(curPrice.multiply(curCoinCount).setScale(4, RoundingMode.HALF_UP))
Expand All @@ -109,7 +120,7 @@ public List<DashBoardDto.RepresentativeCoinResponse> getRepresentativeCoins() {
List<DashBoardDto.RepresentativeCoinResponse> representativeCoinResponses = new ArrayList<>();
for (Coin coin : Coin.values()) {
UpbitDto.Ticker[] ticker = getTickerPriceAtUpbit(UPBIT_URL_TICKER + coin.getMarketName());
representativeCoinResponses.add(DashBoardDto.RepresentativeCoinResponse.of(ticker[0], coin.getKoreanName()));
representativeCoinResponses.add(DashBoardDto.RepresentativeCoinResponse.of(ticker[0], coin.getKoreanName(), coin.getEnglishName()));
}

return representativeCoinResponses;
Expand All @@ -123,8 +134,8 @@ private BigDecimal getAccountSum(UpbitDto.Account[] accounts) {
accountSum = accountSum.add(account.getCoinCount());
} else {
// 현재가를 가져옴
String coinName = "KRW-" + account.getCurrency();
UpbitDto.Ticker[] ticker = getTickerPriceAtUpbit(UPBIT_URL_TICKER + coinName);
String marketName = "KRW-" + account.getCurrency();
UpbitDto.Ticker[] ticker = getTickerPriceAtUpbit(UPBIT_URL_TICKER + marketName);
BigDecimal curPrice = BigDecimal.valueOf(ticker[0].getPrice());
BigDecimal userCoinCount = account.getCoinCount();
accountSum = accountSum.add(curPrice.multiply(userCoinCount));
Expand Down
Loading