Skip to content

Commit

Permalink
♻️ refactor: 예외처리 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn authored Aug 14, 2024
2 parents 85968d9 + 8655e89 commit 72f984f
Showing 1 changed file with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.waither.weatherservice.entity.Region;
import com.waither.weatherservice.entity.WeatherAdvisory;
import com.waither.weatherservice.exception.WeatherExceptionHandler;
import com.waither.weatherservice.utills.GpsTransferUtils;
import com.waither.weatherservice.kafka.KafkaMessage;
import com.waither.weatherservice.kafka.Producer;
import com.waither.weatherservice.openapi.ForeCastOpenApiResponse;
Expand All @@ -29,6 +28,7 @@
import com.waither.weatherservice.repository.RegionRepository;
import com.waither.weatherservice.repository.WeatherAdvisoryRepository;
import com.waither.weatherservice.response.WeatherErrorCode;
import com.waither.weatherservice.utills.GpsTransferUtils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -64,8 +64,8 @@ public void createExpectedWeather(

ForeCastOpenApiResponse.Item item = items.get(0);

List<Region> region = regionRepository.findRegionByXAndY(item.getNx(), item.getNy());
String regionName = region.get(0).getRegionName();
String regionName = convertGridToRegionName(item.getNx(), item.getNy());

String key = regionName + "_" + item.getFcstDate() + "_" + item.getFcstTime();

ExpectedWeather expectedWeather = ExpectedWeather.builder()
Expand Down Expand Up @@ -105,8 +105,8 @@ public void createDailyWeather(

ForeCastOpenApiResponse.Item item = items.get(0);

List<Region> region = regionRepository.findRegionByXAndY(item.getNx(), item.getNy());
String regionName = region.get(0).getRegionName();
String regionName = convertGridToRegionName(item.getNx(), item.getNy());

String key = regionName + "_" + item.getFcstDate() + "_" + item.getFcstTime();

DailyWeather dailyWeather = DailyWeather.builder()
Expand Down Expand Up @@ -162,31 +162,48 @@ public void convertLocation(double latitude, double longitude) throws URISyntaxE
public MainWeatherResponse getMainWeather(double latitude, double longitude) {
LocalDateTime now = LocalDateTime.now();

List<Region> region = regionRepository.findRegionByLatAndLong(latitude, longitude);

if (region.isEmpty())
List<Region> regionList = regionRepository.findRegionByLatAndLong(latitude, longitude);
if (regionList.isEmpty())
throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND);

String regionName = region.get(0).getRegionName();
Region region = regionList.get(0);
String regionName = region.getRegionName();

log.info("[Main - api] region : {}", regionName);

String expectedWeatherKey = regionName + "_" + convertLocalDateTimeToString(now);

LocalDateTime dailyWeatherBaseTime = convertLocalDateTimeToDailyWeatherTime(now.minusHours(1));

String dailyWeatherKey = regionName + "_" + convertLocalDateTimeToString(dailyWeatherBaseTime);

log.info("[Main - api] dailyWeatherKey : {}", dailyWeatherKey);
log.info("[Main - api] expectedWeatherKey : {}", expectedWeatherKey);

DailyWeather dailyWeather = dailyWeatherRepository.findById(dailyWeatherKey)
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.DAILY_NOT_FOUND));
.orElseGet(() -> {
try {
String[] baseTime = convertLocalDateTimeToString(now.minusHours(2)).split("_");
createDailyWeather(region.getStartX(), region.getStartY(), baseTime[0], baseTime[1]);
} catch (URISyntaxException e) {
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
}
return dailyWeatherRepository.findById(dailyWeatherKey)
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.DAILY_NOT_FOUND));
});

log.info(regionName + "[Main - api] DailyWeather : {}", dailyWeather);

ExpectedWeather expectedWeather = expectedWeatherRepository.findById(expectedWeatherKey)
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.EXPECTED_NOT_FOUND));
.orElseGet(() -> {
try {
String[] baseTime = convertLocalDateTimeToString(now.minusHours(1)).split("_");
createExpectedWeather(region.getStartX(), region.getStartY(), baseTime[0], baseTime[1]);
} catch (URISyntaxException e) {
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR);
}
return expectedWeatherRepository.findById(expectedWeatherKey)
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.EXPECTED_NOT_FOUND));
});

log.info(regionName + "[Main - api] ExpectedWeather : {}", expectedWeather);

Expand All @@ -208,7 +225,19 @@ public List<Region> getRegionList() {
}

public String convertGpsToRegionName(double latitude, double longitude) {
return regionRepository.findRegionByLatAndLong(latitude, longitude).get(0).getRegionName();
List<Region> region = regionRepository.findRegionByLatAndLong(latitude, longitude);
if (region.isEmpty())
throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND);

return region.get(0).getRegionName();
}

public String convertGridToRegionName(int x, int y) {
List<Region> region = regionRepository.findRegionByXAndY(x, y);
if (region.isEmpty())
throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND);

return region.get(0).getRegionName();
}

public double calculateWindChill(double temp, double wind) {
Expand All @@ -219,12 +248,8 @@ public double calculateWindChill(double temp, double wind) {
}

public double getWindChill(double latitude, double longitude, LocalDateTime baseTime) {
String regionName = convertGpsToRegionName(latitude, longitude);

List<Region> region = regionRepository.findRegionByLatAndLong(latitude, longitude);
if (region.isEmpty())
throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND);

String regionName = region.get(0).getRegionName();
LocalDateTime dailyWeatherBaseTime = convertLocalDateTimeToDailyWeatherTime(baseTime.minusHours(1));
String dailyWeatherKey = regionName + "_" + convertLocalDateTimeToString(dailyWeatherBaseTime);

Expand Down

0 comments on commit 72f984f

Please sign in to comment.