-
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.
#53 [feat] : OHLCV 차트 조회 API 구현 (GET)
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/org/dgu/backend/controller/ChartController.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,28 @@ | ||
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.ChartDto; | ||
import org.dgu.backend.service.ChartService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/charts") | ||
@RequiredArgsConstructor | ||
public class ChartController { | ||
private final ChartService chartService; | ||
|
||
// OHLCV 차트를 조회하는 API | ||
@GetMapping | ||
public ResponseEntity<ApiResponse<List<ChartDto.OHLCVResponse>>> getOHLCVCharts( | ||
@RequestParam("coin_name") String koreanName, | ||
@RequestParam("candle_name") String candleName) { | ||
|
||
List<ChartDto.OHLCVResponse> ohlcvResponses = chartService.getOHLCVCharts(koreanName, candleName); | ||
return ApiResponse.onSuccess(SuccessStatus.SUCCESS_GET_OHLCV_CHART, ohlcvResponses); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/org/dgu/backend/service/ChartService.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,9 @@ | ||
package org.dgu.backend.service; | ||
|
||
import org.dgu.backend.dto.ChartDto; | ||
|
||
import java.util.List; | ||
|
||
public interface ChartService { | ||
List<ChartDto.OHLCVResponse> getOHLCVCharts(String koreanName, String candleType); | ||
} |
60 changes: 60 additions & 0 deletions
60
backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.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,60 @@ | ||
package org.dgu.backend.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.dgu.backend.domain.Candle; | ||
import org.dgu.backend.domain.CandleInfo; | ||
import org.dgu.backend.domain.Market; | ||
import org.dgu.backend.dto.ChartDto; | ||
import org.dgu.backend.exception.ChartErrorResult; | ||
import org.dgu.backend.exception.ChartException; | ||
import org.dgu.backend.repository.CandleInfoRepository; | ||
import org.dgu.backend.repository.CandleRepository; | ||
import org.dgu.backend.repository.MarketRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ChartServiceImpl implements ChartService { | ||
private final MarketRepository marketRepository; | ||
private final CandleRepository candleRepository; | ||
private final CandleInfoRepository candleInfoRepository; | ||
|
||
private static final List<String> SEVEN_DAY_CANDLES = Arrays.asList("minutes1", "minutes3", "minutes5", "minutes10", "minutes15", "minutes30"); | ||
private static final List<String> SIX_MONTH_CANDLES = Arrays.asList("minutes60", "minutes240"); | ||
|
||
// OHLCV 차트를 반환하는 메서드 | ||
@Override | ||
public List<ChartDto.OHLCVResponse> getOHLCVCharts(String koreanName, String candleName) { | ||
Market market = marketRepository.findByKoreanName(koreanName); | ||
Candle candle = candleRepository.findByCandleName(candleName); | ||
|
||
LocalDateTime startDate = getStartDateByCandleName(candleName); | ||
|
||
List<CandleInfo> candleInfos = candleInfoRepository.findByMarketAndCandleAndDateTimeAfter(market, candle, startDate); | ||
if (candleInfos.isEmpty()) { | ||
throw new ChartException(ChartErrorResult.NOT_FOUND_CHARTS); | ||
} | ||
return candleInfos.stream() | ||
.map(ChartDto.OHLCVResponse::of) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// 캔들 종류에 따라 시작 기간을 계산해 반환하는 메서드 | ||
private LocalDateTime getStartDateByCandleName(String candleName) { | ||
LocalDateTime now = LocalDateTime.now(); | ||
if (SEVEN_DAY_CANDLES.contains(candleName)) { | ||
return now.minusDays(7); | ||
} else if (SIX_MONTH_CANDLES.contains(candleName)) { | ||
return now.minusMonths(6); | ||
} else { | ||
return LocalDateTime.of(2019, 1, 1, 0, 0); | ||
} | ||
} | ||
} |