Skip to content

Commit fa399ec

Browse files
authored
Hotfix/activity index gunwoong (#125)
* hotfix: weather * hotfix: activity index
1 parent 0c4e207 commit fa399ec

File tree

5 files changed

+123
-76
lines changed

5 files changed

+123
-76
lines changed

src/main/java/sevenstar/marineleisure/activity/service/ActivityService.java

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import sevenstar.marineleisure.global.enums.ActivityCategory;
3030
import sevenstar.marineleisure.global.enums.TimePeriod;
3131
import sevenstar.marineleisure.spot.domain.OutdoorSpot;
32+
import sevenstar.marineleisure.spot.dto.SpotPreviewReadResponse;
3233
import sevenstar.marineleisure.spot.repository.OutdoorSpotRepository;
34+
import sevenstar.marineleisure.spot.service.SpotService;
3335

3436
@Service
3537
@RequiredArgsConstructor
@@ -42,6 +44,8 @@ public class ActivityService {
4244
private final ScubaRepository scubaRepository;
4345
private final SurfingRepository surfingRepository;
4446

47+
private final SpotService spotService;
48+
4549
@Transactional(readOnly = true)
4650
public Map<String, ActivitySummaryResponse> getActivitySummary(BigDecimal latitude, BigDecimal longitude,
4751
boolean global) {
@@ -55,90 +59,92 @@ public Map<String, ActivitySummaryResponse> getActivitySummary(BigDecimal latitu
5559
private Map<String, ActivitySummaryResponse> getLocalActivitySummary(BigDecimal latitude, BigDecimal longitude) {
5660
Map<String, ActivitySummaryResponse> responses = new HashMap<>();
5761

58-
Fishing fishingBySpot = null;
59-
Mudflat mudflatBySpot = null;
60-
Surfing surfingBySpot = null;
61-
Scuba scubaBySpot = null;
62-
63-
LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
64-
LocalDateTime endOfDay = startOfDay.plusDays(1);
65-
66-
List<OutdoorSpot> outdoorSpotList = outdoorSpotRepository.findByCoordinates(latitude, longitude, 10);
67-
68-
while (fishingBySpot == null || mudflatBySpot == null || surfingBySpot == null || scubaBySpot == null) {
69-
70-
OutdoorSpot currentSpot;
71-
Long currentSpotId;
72-
73-
try {
74-
currentSpot = outdoorSpotList.removeFirst();
75-
currentSpotId = currentSpot.getId();
76-
} catch (Exception e) {
77-
break;
78-
}
79-
80-
if (fishingBySpot == null) {
81-
Optional<Fishing> fishingResult = fishingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
82-
currentSpotId, startOfDay, endOfDay);
83-
84-
if (fishingResult.isPresent()) {
85-
fishingBySpot = fishingResult.get();
86-
responses.put("Fishing",
87-
new ActivitySummaryResponse(currentSpot.getName(), fishingResult.get().getTotalIndex()));
88-
}
89-
}
90-
91-
if (mudflatBySpot == null) {
92-
Optional<Mudflat> mudflatResult = mudflatRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
93-
currentSpotId, startOfDay, endOfDay);
94-
95-
if (mudflatResult.isPresent()) {
96-
mudflatBySpot = mudflatResult.get();
97-
responses.put("Mudflat",
98-
new ActivitySummaryResponse(currentSpot.getName(), mudflatResult.get().getTotalIndex()));
99-
}
100-
}
101-
102-
if (surfingBySpot == null) {
103-
Optional<Surfing> surfingResult = surfingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
104-
currentSpotId, startOfDay, endOfDay);
105-
106-
if (surfingResult.isPresent()) {
107-
surfingBySpot = surfingResult.get();
108-
responses.put("Surfing",
109-
new ActivitySummaryResponse(currentSpot.getName(), surfingResult.get().getTotalIndex()));
110-
}
111-
}
112-
113-
if (scubaBySpot == null) {
114-
Optional<Scuba> scubaResult = scubaRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
115-
currentSpotId, startOfDay, endOfDay);
116-
117-
if (scubaResult.isPresent()) {
118-
scubaBySpot = scubaResult.get();
119-
responses.put("Scuba",
120-
new ActivitySummaryResponse(currentSpot.getName(), scubaResult.get().getTotalIndex()));
121-
}
122-
}
123-
}
62+
SpotPreviewReadResponse preview = spotService.preview(latitude.floatValue(), longitude.floatValue());
63+
responses.put("Fishing",
64+
new ActivitySummaryResponse(preview.fishing().getName(), preview.fishing().getTotalIndex()));
65+
responses.put("Mudflat",new ActivitySummaryResponse(preview.mudflat().getName(), preview.mudflat().getTotalIndex()));
66+
responses.put("Surfing", new ActivitySummaryResponse(preview.surfing().getName(), preview.surfing().getTotalIndex()));
67+
responses.put("Scuba", new ActivitySummaryResponse(preview.scuba().getName(), preview.scuba().getTotalIndex()));
68+
69+
// Fishing fishingBySpot = null;
70+
// Mudflat mudflatBySpot = null;
71+
// Surfing surfingBySpot = null;
72+
// Scuba scubaBySpot = null;
73+
//
74+
// LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
75+
// LocalDateTime endOfDay = startOfDay.plusDays(1);
76+
//
77+
// List<OutdoorSpot> outdoorSpotList = outdoorSpotRepository.findByCoordinates(latitude, longitude, 10);
78+
//
79+
// while (fishingBySpot == null || mudflatBySpot == null || surfingBySpot == null || scubaBySpot == null) {
80+
//
81+
// OutdoorSpot currentSpot;
82+
// Long currentSpotId;
83+
//
84+
// try {
85+
// currentSpot = outdoorSpotList.removeFirst();
86+
// currentSpotId = currentSpot.getId();
87+
// } catch (Exception e) {
88+
// break;
89+
// }
90+
//
91+
// if (fishingBySpot == null) {
92+
// Optional<Fishing> fishingResult = fishingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
93+
// currentSpotId, startOfDay, endOfDay);
94+
//
95+
// if (fishingResult.isPresent()) {
96+
// fishingBySpot = fishingResult.get();
97+
// responses.put("Fishing",
98+
// new ActivitySummaryResponse(currentSpot.getName(), fishingResult.get().getTotalIndex()));
99+
// }
100+
// }
101+
//
102+
// if (mudflatBySpot == null) {
103+
// Optional<Mudflat> mudflatResult = mudflatRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
104+
// currentSpotId, startOfDay, endOfDay);
105+
//
106+
// if (mudflatResult.isPresent()) {
107+
// mudflatBySpot = mudflatResult.get();
108+
// responses.put("Mudflat",
109+
// new ActivitySummaryResponse(currentSpot.getName(), mudflatResult.get().getTotalIndex()));
110+
// }
111+
// }
112+
//
113+
// if (surfingBySpot == null) {
114+
// Optional<Surfing> surfingResult = surfingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
115+
// currentSpotId, startOfDay, endOfDay);
116+
//
117+
// if (surfingResult.isPresent()) {
118+
// surfingBySpot = surfingResult.get();
119+
// responses.put("Surfing",
120+
// new ActivitySummaryResponse(currentSpot.getName(), surfingResult.get().getTotalIndex()));
121+
// }
122+
// }
123+
//
124+
// if (scubaBySpot == null) {
125+
// Optional<Scuba> scubaResult = scubaRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
126+
// currentSpotId, startOfDay, endOfDay);
127+
//
128+
// if (scubaResult.isPresent()) {
129+
// scubaBySpot = scubaResult.get();
130+
// responses.put("Scuba",
131+
// new ActivitySummaryResponse(currentSpot.getName(), scubaResult.get().getTotalIndex()));
132+
// }
133+
// }
134+
// }
124135

125136
return responses;
126137
}
127138

128139
private Map<String, ActivitySummaryResponse> getGlobalActivitySummary() {
129140
Map<String, ActivitySummaryResponse> responses = new HashMap<>();
130141

131-
LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
132-
LocalDateTime endOfDay = startOfDay.plusDays(1);
142+
LocalDate now = LocalDate.now();
133143

134-
Optional<Fishing> fishingResult = fishingRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(
135-
startOfDay, endOfDay);
136-
Optional<Mudflat> mudflatResult = mudflatRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(
137-
startOfDay, endOfDay);
138-
Optional<Surfing> surfingResult = surfingRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(
139-
startOfDay, endOfDay);
140-
Optional<Scuba> scubaResult = scubaRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(
141-
startOfDay, endOfDay);
144+
Optional<Fishing> fishingResult = fishingRepository.findBestTotaIndexFishing(now);
145+
Optional<Mudflat> mudflatResult = mudflatRepository.findBestTotaIndexMudflat(now);
146+
Optional<Surfing> surfingResult = surfingRepository.findBestTotaIndexSurfing(now);
147+
Optional<Scuba> scubaResult = scubaRepository.findBestTotaIndexScuba(now);
142148

143149
if (fishingResult.isPresent()) {
144150
Fishing fishing = fishingResult.get();
@@ -225,4 +231,6 @@ public ActivityWeatherResponse getWeatherBySpot(Float latitude, Float longitude)
225231
fishing.getSeaTempMax().toString()
226232
);
227233
}
234+
235+
228236
}

src/main/java/sevenstar/marineleisure/forecast/repository/FishingRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ Optional<Fishing> findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessT
118118
LocalDateTime endDateTime
119119
);
120120

121+
@Query(value = """
122+
SELECT *
123+
FROM fishing_forecast f
124+
WHERE f.forecast_date = :forecastDate
125+
ORDER BY f.total_index DESC
126+
LIMIT 1
127+
""",nativeQuery = true)
128+
Optional<Fishing> findBestTotaIndexFishing(@Param("forecastDate") LocalDate forecastDate);
129+
121130
Optional<Fishing> findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start,
122131
LocalDateTime end);
123132

src/main/java/sevenstar/marineleisure/forecast/repository/MudflatRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.data.repository.query.Param;
1212

1313
import jakarta.transaction.Transactional;
14+
import sevenstar.marineleisure.forecast.domain.Fishing;
1415
import sevenstar.marineleisure.forecast.domain.Mudflat;
1516
import sevenstar.marineleisure.spot.repository.ActivityRepository;
1617

@@ -78,6 +79,15 @@ Optional<Mudflat> findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessT
7879
LocalDateTime endDateTime
7980
);
8081

82+
@Query(value = """
83+
SELECT *
84+
FROM mudflat_forecast m
85+
WHERE m.forecast_date = :forecastDate
86+
ORDER BY m.total_index DESC
87+
LIMIT 1
88+
""",nativeQuery = true)
89+
Optional<Mudflat> findBestTotaIndexMudflat(@Param("forecastDate") LocalDate forecastDate);
90+
8191
Optional<Mudflat> findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start, LocalDateTime end);
8292

8393
Optional<Mudflat> findBySpotIdAndCreatedAtBeforeOrderByCreatedAtDesc(Long spotId, LocalDateTime createdAtBefore);

src/main/java/sevenstar/marineleisure/forecast/repository/ScubaRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import jakarta.transaction.Transactional;
1414
import sevenstar.marineleisure.forecast.domain.Scuba;
15+
import sevenstar.marineleisure.forecast.domain.Surfing;
1516
import sevenstar.marineleisure.spot.repository.ActivityRepository;
1617

1718
public interface ScubaRepository extends ActivityRepository<Scuba, Long> {
@@ -75,6 +76,15 @@ void updateSunriseAndSunset(
7576
@Param("forecastDate") LocalDate forecastDate
7677
);
7778

79+
@Query(value = """
80+
SELECT *
81+
FROM scuba_forecast s
82+
WHERE s.forecast_date = :forecastDate
83+
ORDER BY s.total_index DESC
84+
LIMIT 1
85+
""",nativeQuery = true)
86+
Optional<Scuba> findBestTotaIndexScuba(@Param("forecastDate") LocalDate forecastDate);
87+
7888
Optional<Scuba> findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start, LocalDateTime end);
7989

8090
Optional<Scuba> findBySpotIdAndCreatedAtBeforeOrderByCreatedAtDesc(Long spotId, LocalDateTime createdAtBefore);

src/main/java/sevenstar/marineleisure/forecast/repository/SurfingRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.data.repository.query.Param;
1111

1212
import jakarta.transaction.Transactional;
13+
import sevenstar.marineleisure.forecast.domain.Fishing;
1314
import sevenstar.marineleisure.forecast.domain.Surfing;
1415
import sevenstar.marineleisure.spot.repository.ActivityRepository;
1516

@@ -77,6 +78,15 @@ Optional<Surfing> findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessT
7778
LocalDateTime endDateTime
7879
);
7980

81+
@Query(value = """
82+
SELECT *
83+
FROM surfing_forecast s
84+
WHERE s.forecast_date = :forecastDate
85+
ORDER BY s.total_index DESC
86+
LIMIT 1
87+
""",nativeQuery = true)
88+
Optional<Surfing> findBestTotaIndexSurfing(@Param("forecastDate") LocalDate forecastDate);
89+
8090
Optional<Surfing> findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start, LocalDateTime end);
8191

8292
Optional<Surfing> findBySpotIdAndCreatedAtBeforeOrderByCreatedAtDesc(Long spotId, LocalDateTime createdAtBefore);

0 commit comments

Comments
 (0)