From ecbb1cd9046ddd31518ac2c2108e493398fbcff2 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 10 Jun 2024 01:34:23 +0900 Subject: [PATCH] =?UTF-8?q?#53=20[feat]=20:=20Candle=20=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/exception/CandleErrorResult.java | 37 +++++++++++++++++++ .../backend/exception/CandleException.java | 15 ++++++++ .../exception/GlobalExceptionHandler.java | 6 +++ .../dgu/backend/service/ChartServiceImpl.java | 11 ++++-- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/org/dgu/backend/exception/CandleErrorResult.java create mode 100644 backend/src/main/java/org/dgu/backend/exception/CandleException.java diff --git a/backend/src/main/java/org/dgu/backend/exception/CandleErrorResult.java b/backend/src/main/java/org/dgu/backend/exception/CandleErrorResult.java new file mode 100644 index 0000000..229398e --- /dev/null +++ b/backend/src/main/java/org/dgu/backend/exception/CandleErrorResult.java @@ -0,0 +1,37 @@ +package org.dgu.backend.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.dgu.backend.common.code.BaseErrorCode; +import org.dgu.backend.common.dto.ErrorReasonDto; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum CandleErrorResult implements BaseErrorCode { + NOT_FOUND_CANDLE(HttpStatus.NOT_FOUND, "404", "존재하지 않는 캔들입니다."), + NOT_FOUND_CANDLES(HttpStatus.NOT_FOUND, "404", "캔들 목록이 존재하지 않습니다."); + + private final HttpStatus httpStatus; + private final String code; + private final String message; + + @Override + public ErrorReasonDto getReason() { + return ErrorReasonDto.builder() + .isSuccess(false) + .code(code) + .message(message) + .build(); + } + + @Override + public ErrorReasonDto getReasonHttpStatus() { + return ErrorReasonDto.builder() + .isSuccess(false) + .httpStatus(httpStatus) + .code(code) + .message(message) + .build(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/exception/CandleException.java b/backend/src/main/java/org/dgu/backend/exception/CandleException.java new file mode 100644 index 0000000..d58a4df --- /dev/null +++ b/backend/src/main/java/org/dgu/backend/exception/CandleException.java @@ -0,0 +1,15 @@ +package org.dgu.backend.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class CandleException extends RuntimeException { + private final CandleErrorResult candleErrorResult; + + @Override + public String getMessage() { + return candleErrorResult.getMessage(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java b/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java index bd0b918..9cca2c0 100644 --- a/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java @@ -75,4 +75,10 @@ public ResponseEntity> handleMarketException(MarketEx MarketErrorResult errorResult = e.getMarketErrorResult(); return ApiResponse.onFailure(errorResult); } + // Candle + @ExceptionHandler(CandleException.class) + public ResponseEntity> handleCandleException(CandleException e) { + CandleErrorResult errorResult = e.getCandleErrorResult(); + return ApiResponse.onFailure(errorResult); + } } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java b/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java index 7de8bfb..972b802 100644 --- a/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java +++ b/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java @@ -6,10 +6,7 @@ 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.exception.MarketErrorResult; -import org.dgu.backend.exception.MarketException; +import org.dgu.backend.exception.*; import org.dgu.backend.repository.CandleInfoRepository; import org.dgu.backend.repository.CandleRepository; import org.dgu.backend.repository.MarketRepository; @@ -46,6 +43,9 @@ public List getAllChartOptions() { throw new MarketException(MarketErrorResult.NOT_FOUND_MARKETS); } List candles = candleRepository.findAll(); + if (candles.isEmpty()) { + throw new CandleException(CandleErrorResult.NOT_FOUND_CANDLES); + } return markets.stream() .flatMap(market -> candles.stream() @@ -67,6 +67,9 @@ protected List fetchUpdatedCandleInfo(String koreanName, throw new MarketException(MarketErrorResult.NOT_FOUND_MARKET); } Candle candle = candleRepository.findByCandleName(candleName); + if (Objects.isNull(candle)) { + throw new CandleException(CandleErrorResult.NOT_FOUND_CANDLE); + } LocalDateTime startDate = candleUtil.getStartDateByCandleName(candleName); List candleInfos = candleInfoRepository.findByMarketAndCandleAndDateTimeAfter(market, candle, startDate);