From 42d93af73b899d9ce6d10578b0413c9d011465bb Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 10 Jun 2024 01:31:28 +0900 Subject: [PATCH] =?UTF-8?q?#53=20[feat]=20:=20Market=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 --- .../exception/GlobalExceptionHandler.java | 7 ++++ .../backend/exception/MarketErrorResult.java | 37 +++++++++++++++++++ .../backend/exception/MarketException.java | 15 ++++++++ .../dgu/backend/service/ChartServiceImpl.java | 9 +++++ 4 files changed, 68 insertions(+) create mode 100644 backend/src/main/java/org/dgu/backend/exception/MarketErrorResult.java create mode 100644 backend/src/main/java/org/dgu/backend/exception/MarketException.java 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 c63f72a..bd0b918 100644 --- a/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java @@ -3,6 +3,7 @@ import lombok.extern.slf4j.Slf4j; import org.dgu.backend.common.ApiResponse; import org.dgu.backend.common.code.BaseErrorCode; +import org.dgu.backend.domain.Market; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MissingRequestHeaderException; @@ -68,4 +69,10 @@ public ResponseEntity> handleChartException(ChartExce ChartErrorResult errorResult = e.getChartErrorResult(); return ApiResponse.onFailure(errorResult); } + // Market + @ExceptionHandler(MarketException.class) + public ResponseEntity> handleMarketException(MarketException e) { + MarketErrorResult errorResult = e.getMarketErrorResult(); + return ApiResponse.onFailure(errorResult); + } } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/exception/MarketErrorResult.java b/backend/src/main/java/org/dgu/backend/exception/MarketErrorResult.java new file mode 100644 index 0000000..23aba20 --- /dev/null +++ b/backend/src/main/java/org/dgu/backend/exception/MarketErrorResult.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 MarketErrorResult implements BaseErrorCode { + NOT_FOUND_MARKET(HttpStatus.NOT_FOUND, "404", "존재하지 않는 가상화폐입니다."), + NOT_FOUND_MARKETS(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/MarketException.java b/backend/src/main/java/org/dgu/backend/exception/MarketException.java new file mode 100644 index 0000000..a20d7b5 --- /dev/null +++ b/backend/src/main/java/org/dgu/backend/exception/MarketException.java @@ -0,0 +1,15 @@ +package org.dgu.backend.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class MarketException extends RuntimeException { + private final MarketErrorResult marketErrorResult; + + @Override + public String getMessage() { + return marketErrorResult.getMessage(); + } +} \ 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 8eb816f..7de8bfb 100644 --- a/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java +++ b/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java @@ -8,6 +8,8 @@ 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.repository.CandleInfoRepository; import org.dgu.backend.repository.CandleRepository; import org.dgu.backend.repository.MarketRepository; @@ -16,6 +18,7 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; @Service @@ -39,6 +42,9 @@ public List getOHLCVCharts(String koreanName, String can @Override public List getAllChartOptions() { List markets = marketRepository.findAll(); + if (markets.isEmpty()) { + throw new MarketException(MarketErrorResult.NOT_FOUND_MARKETS); + } List candles = candleRepository.findAll(); return markets.stream() @@ -57,6 +63,9 @@ protected void updateCandleInfo(String koreanName, String candleName) { @Transactional protected List fetchUpdatedCandleInfo(String koreanName, String candleName) { Market market = marketRepository.findByKoreanName(koreanName); + if (Objects.isNull(market)) { + throw new MarketException(MarketErrorResult.NOT_FOUND_MARKET); + } Candle candle = candleRepository.findByCandleName(candleName); LocalDateTime startDate = candleUtil.getStartDateByCandleName(candleName);