Skip to content

Commit

Permalink
#53 [feat] : OHLCV 차트 조회 API 구현 (GET)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Jun 8, 2024
1 parent 97149b7 commit 2db3c17
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
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);
}
}
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);
}
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);
}
}
}

0 comments on commit 2db3c17

Please sign in to comment.