|
6 | 6 | import java.util.HashMap; |
7 | 7 | import java.util.List; |
8 | 8 | import java.util.Map; |
| 9 | +import java.util.NoSuchElementException; |
9 | 10 | import java.util.Optional; |
10 | 11 |
|
11 | 12 | import org.springframework.stereotype.Service; |
|
26 | 27 | import sevenstar.marineleisure.forecast.repository.ScubaRepository; |
27 | 28 | import sevenstar.marineleisure.forecast.repository.SurfingRepository; |
28 | 29 | import sevenstar.marineleisure.global.enums.ActivityCategory; |
| 30 | +import sevenstar.marineleisure.global.enums.TimePeriod; |
29 | 31 | import sevenstar.marineleisure.spot.domain.OutdoorSpot; |
30 | 32 | import sevenstar.marineleisure.spot.repository.OutdoorSpotRepository; |
31 | 33 |
|
@@ -205,34 +207,22 @@ public ActivityDetailResponse getActivityDetail(ActivityCategory activity, BigDe |
205 | 207 | } |
206 | 208 |
|
207 | 209 | @Transactional(readOnly = true) |
208 | | - public ActivityWeatherResponse getWeatherBySpot(BigDecimal latitude, BigDecimal longitude) { |
209 | | - OutdoorSpot nearSpot = outdoorSpotRepository.findByCoordinates(latitude, longitude, 1).getFirst(); |
210 | | - |
211 | | - Optional<Fishing> fishingSpot = fishingRepository.findBySpotIdOrderByCreatedAt(nearSpot.getId()); |
212 | | - |
213 | | - if (fishingSpot.isPresent()) { |
214 | | - Fishing fishingSpotGet = fishingSpot.get(); |
215 | | - |
216 | | - return new ActivityWeatherResponse( |
217 | | - nearSpot.getName(), |
218 | | - fishingSpotGet.getWindSpeedMax().toString(), |
219 | | - fishingSpotGet.getWaveHeightMax().toString(), |
220 | | - fishingSpotGet.getSeaTempMax().toString() |
221 | | - ); |
222 | | - } |
223 | | - |
224 | | - Optional<Surfing> surfingSpot = surfingRepository.findBySpotIdOrderByCreatedAt(nearSpot.getId()); |
225 | | - |
226 | | - if (surfingSpot.isPresent()) { |
227 | | - Surfing surfingSpotGet = surfingSpot.get(); |
228 | | - return new ActivityWeatherResponse( |
229 | | - nearSpot.getName(), |
230 | | - surfingSpotGet.getWindSpeed().toString(), |
231 | | - surfingSpotGet.getWaveHeight().toString(), |
232 | | - surfingSpotGet.getSeaTemp().toString() |
233 | | - ); |
234 | | - } else { |
235 | | - throw new RuntimeException("Spot not found"); |
236 | | - } |
| 210 | + public ActivityWeatherResponse getWeatherBySpot(Float latitude, Float longitude) { |
| 211 | + // 1. 가까운 낚시 지점 조회 |
| 212 | + OutdoorSpot nearSpot = outdoorSpotRepository.findNearFishingSpot(latitude.doubleValue(),longitude.doubleValue()) |
| 213 | + .orElseThrow(() -> new NoSuchElementException("가까운 낚시 지점을 찾을 수 없습니다.")); |
| 214 | + |
| 215 | + // 2. 해당 지점의 예보 데이터 조회 |
| 216 | + Fishing fishing = fishingRepository.findFishingBySpotIdAndForecastDateAndTimePeriod(nearSpot.getId(), LocalDate.now(), |
| 217 | + TimePeriod.AM) |
| 218 | + .orElseThrow(() -> new NoSuchElementException("해당 지점에 대한 예보 정보를 찾을 수 없습니다.")); |
| 219 | + |
| 220 | + // 3. 결과 조합 |
| 221 | + return new ActivityWeatherResponse( |
| 222 | + nearSpot.getName(), |
| 223 | + fishing.getWindSpeedMax().toString(), |
| 224 | + fishing.getWaveHeightMax().toString(), |
| 225 | + fishing.getSeaTempMax().toString() |
| 226 | + ); |
237 | 227 | } |
238 | 228 | } |
0 commit comments