diff --git a/backend/src/main/java/org/dgu/backend/common/constant/SuccessStatus.java b/backend/src/main/java/org/dgu/backend/common/constant/SuccessStatus.java index 5bac814..5bd9690 100644 --- a/backend/src/main/java/org/dgu/backend/common/constant/SuccessStatus.java +++ b/backend/src/main/java/org/dgu/backend/common/constant/SuccessStatus.java @@ -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; diff --git a/backend/src/main/java/org/dgu/backend/controller/TradingController.java b/backend/src/main/java/org/dgu/backend/controller/TradingController.java index 7913e99..de43dde 100644 --- a/backend/src/main/java/org/dgu/backend/controller/TradingController.java +++ b/backend/src/main/java/org/dgu/backend/controller/TradingController.java @@ -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 @@ -37,8 +38,11 @@ public ResponseEntity> removeAutoTrading( } // 자동매매 수동 테스트 API - @GetMapping("/test") - public void test() { - upbitAutoTrader.performAutoTrading(); + @GetMapping("/logs") + public ResponseEntity>> getUserTradingLogs( + @RequestHeader("Authorization") String authorizationHeader) { + + List tradingLogs = tradingService.getUserTradingLogs(authorizationHeader); + return ApiResponse.onSuccess(SuccessStatus.SUCCESS_GET_TRADING_LOGS, tradingLogs); } } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/dto/TradingDto.java b/backend/src/main/java/org/dgu/backend/dto/TradingDto.java index ce17916..8eed2c3 100644 --- a/backend/src/main/java/org/dgu/backend/dto/TradingDto.java +++ b/backend/src/main/java/org/dgu/backend/dto/TradingDto.java @@ -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 { @@ -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 ofTradingLogs(List tradingLogs) { + List 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; + } + } + } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/repository/UserTradingLogRepository.java b/backend/src/main/java/org/dgu/backend/repository/UserTradingLogRepository.java index 74986f6..941a0b5 100644 --- a/backend/src/main/java/org/dgu/backend/repository/UserTradingLogRepository.java +++ b/backend/src/main/java/org/dgu/backend/repository/UserTradingLogRepository.java @@ -12,4 +12,6 @@ public interface UserTradingLogRepository extends JpaRepository (SELECT MAX(utl2.createdAt) FROM UserTradingLog utl2 WHERE utl2.user = :user AND utl2.type = 'SELL')") List findRecentLogsAfterLastSell(@Param("user") User user); + + List findAllByUser(User user); } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/service/TradingService.java b/backend/src/main/java/org/dgu/backend/service/TradingService.java index fd5e979..5cdc40d 100644 --- a/backend/src/main/java/org/dgu/backend/service/TradingService.java +++ b/backend/src/main/java/org/dgu/backend/service/TradingService.java @@ -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 getUserTradingLogs(String authorizationHeader); } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/service/TradingServiceImpl.java b/backend/src/main/java/org/dgu/backend/service/TradingServiceImpl.java index e476d7c..911241e 100644 --- a/backend/src/main/java/org/dgu/backend/service/TradingServiceImpl.java +++ b/backend/src/main/java/org/dgu/backend/service/TradingServiceImpl.java @@ -71,6 +71,18 @@ public void removeAutoTrading(String authorizationHeader, String portfolioId) { cancelAutoTrading(portfolio); tradingOptionRepository.deleteTradingOptionById(existingTradingOption.getId()); } + + // 자동매매 거래 로그 조회 메서드 + @Override + public List getUserTradingLogs(String authorizationHeader) { + User user = jwtUtil.getUserFromHeader(authorizationHeader); + validateUser(user); + + List userTradingLogs = userTradingLogRepository.findAllByUser(user); + + return TradingDto.TradingLog.ofTradingLogs(userTradingLogs); + } + // 거래 실행 메서드 @Override public void executeTrade(User user, PortfolioOption portfolioOption, TradingOption tradingOption, Double curPrice) throws UpbitException, UserException {