Skip to content

Commit

Permalink
Merge pull request #78 from CSID-DGU/feature/#33/trading
Browse files Browse the repository at this point in the history
[feat] : 자동매매 거래 로그 조회 API 구현 (GET)
  • Loading branch information
bbbang105 authored Jun 17, 2024
2 parents 73b10d9 + f068763 commit cb75b5a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public enum SuccessStatus implements BaseCode {
SUCCESS_GET_ALL_CHART_OPTIONS(HttpStatus.OK, "200", "차트 선택 지표 목록 조회에 성공했습니다."),
// Trading
SUCCESS_START_TRADING(HttpStatus.CREATED, "201", "자동매매 등록에 성공했습니다."),
SUCCESS_DELETE_TRADING(HttpStatus.CREATED, "201", "자동매매 삭제에 성공했습니다.");
SUCCESS_DELETE_TRADING(HttpStatus.OK, "200", "자동매매 삭제에 성공했습니다."),
SUCCESS_GET_TRADING_LOGS(HttpStatus.OK, "200", "자동매매 거래 로그 조회에 성공했습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/trading")
@RequiredArgsConstructor
public class TradingController {
private final TradingService tradingService;
private final UpbitAutoTrader upbitAutoTrader;

// 자동매매 등록 API
@PostMapping
Expand All @@ -37,8 +38,11 @@ public ResponseEntity<ApiResponse<Object>> removeAutoTrading(
}

// 자동매매 수동 테스트 API
@GetMapping("/test")
public void test() {
upbitAutoTrader.performAutoTrading();
@GetMapping("/logs")
public ResponseEntity<ApiResponse<List<TradingDto.TradingLog>>> getUserTradingLogs(
@RequestHeader("Authorization") String authorizationHeader) {

List<TradingDto.TradingLog> tradingLogs = tradingService.getUserTradingLogs(authorizationHeader);
return ApiResponse.onSuccess(SuccessStatus.SUCCESS_GET_TRADING_LOGS, tradingLogs);
}
}
39 changes: 35 additions & 4 deletions backend/src/main/java/org/dgu/backend/dto/TradingDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.dgu.backend.domain.Portfolio;
import org.dgu.backend.domain.PortfolioOption;
import org.dgu.backend.domain.TradingOption;
import org.dgu.backend.domain.User;
import org.dgu.backend.domain.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class TradingDto {
Expand Down Expand Up @@ -39,4 +39,35 @@ public TradingOption to(User user, Portfolio portfolio, PortfolioOption portfoli
.build();
}
}

@Builder
@Getter
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class TradingLog {
private String type;
private LocalDateTime date;
private Long capital;
private BigDecimal coin;
private Long coinPrice;
private Double rate;

public static List<TradingDto.TradingLog> ofTradingLogs(List<UserTradingLog> tradingLogs) {
List<TradingDto.TradingLog> tradingLogList = new ArrayList<>();
for (UserTradingLog tradingLog : tradingLogs) {
tradingLogList.add(TradingDto.TradingLog.builder()
.type(!tradingLog.getType().equals("BUY") ? "매도" : "매수")
.date(tradingLog.getCreatedAt().minusHours(9))
.capital(tradingLog.getCapital())
.coinPrice(tradingLog.getCoinPrice().longValue())
.coin(tradingLog.getCoin())
.rate(tradingLog.getRate())
.build());
}

return tradingLogList;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface UserTradingLogRepository extends JpaRepository<UserTradingLog,L
@Query("SELECT utl FROM UserTradingLog utl WHERE utl.user = :user " +
"AND utl.createdAt > (SELECT MAX(utl2.createdAt) FROM UserTradingLog utl2 WHERE utl2.user = :user AND utl2.type = 'SELL')")
List<UserTradingLog> findRecentLogsAfterLastSell(@Param("user") User user);

List<UserTradingLog> findAllByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import org.dgu.backend.domain.User;
import org.dgu.backend.dto.TradingDto;

import java.util.List;

public interface TradingService {
void registerAutoTrading(String authorizationHeader, TradingDto.AutoTradingRequest autoTradingRequest);
void removeAutoTrading(String authorizationHeader, String portfolioId);
void executeTrade(User user, PortfolioOption portfolioOption, TradingOption tradingOption, Double curPrice);
List<TradingDto.TradingLog> getUserTradingLogs(String authorizationHeader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public void removeAutoTrading(String authorizationHeader, String portfolioId) {
cancelAutoTrading(portfolio);
tradingOptionRepository.deleteTradingOptionById(existingTradingOption.getId());
}

// 자동매매 거래 로그 조회 메서드
@Override
public List<TradingDto.TradingLog> getUserTradingLogs(String authorizationHeader) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
validateUser(user);

List<UserTradingLog> userTradingLogs = userTradingLogRepository.findAllByUser(user);

return TradingDto.TradingLog.ofTradingLogs(userTradingLogs);
}

// 거래 실행 메서드
@Override
public void executeTrade(User user, PortfolioOption portfolioOption, TradingOption tradingOption, Double curPrice) throws UpbitException, UserException {
Expand Down

0 comments on commit cb75b5a

Please sign in to comment.