Skip to content

Commit

Permalink
Merge pull request #24 from Nexters/feature/23-current-weather-max-mi…
Browse files Browse the repository at this point in the history
…n-temp-bug

[#23] 오늘 날씨 최고/최저 기온 업데이트 오류 수정
  • Loading branch information
jun108059 authored Nov 17, 2024
2 parents 52eb036 + 94582ef commit 01d51b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/main/kotlin/nexters/weski/batch/ExternalWeatherService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ class ExternalWeatherService(

@Transactional
fun updateCurrentWeather() {
val baseDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
val baseTime = getBaseTime()
val baseLocalDateTime = if (baseTime == "2300") {
LocalDateTime.now().minusDays(1)
} else {
LocalDateTime.now()
}
val baseDate = baseLocalDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"))
skiResortRepository.findAll().forEach { resort ->
val url = "https://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtNcst" +
"?serviceKey=$apiKey" +
Expand Down Expand Up @@ -62,6 +67,9 @@ class ExternalWeatherService(
private fun getBaseTime(): String {
val now = LocalDateTime.now().minusHours(1)
val hour = now.hour.toString().padStart(2, '0')
if (hour == "23") {
return "0000"
}
return "${hour}00"
}

Expand Down Expand Up @@ -91,11 +99,21 @@ class ExternalWeatherService(
val feelsLike = calculateFeelsLike(temperature, windSpeed)
val condition = determineCondition(data)
val description = generateDescription(condition, temperature)

val dailyWeather = dailyWeatherRepository.findBySkiResortAndForecastDate(resort, LocalDate.now())[0]
// dailyWeather.maxTemp보다 temperature이 높으면 maxTemp를 업데이트
if (temperature > dailyWeather.maxTemp) {
dailyWeather.maxTemp = temperature
dailyWeatherRepository.save(dailyWeather)
}
// dailyWeather.minTemp보다 temperature이 낮으면 minTemp을 업데이트
if (temperature < dailyWeather.minTemp) {
dailyWeather.minTemp = temperature
dailyWeatherRepository.save(dailyWeather)
}
return CurrentWeather(
temperature = temperature,
maxTemp = data["TMX"]?.toDoubleOrNull()?.toInt() ?: temperature,
minTemp = data["TMN"]?.toDoubleOrNull()?.toInt() ?: temperature,
maxTemp = dailyWeather.maxTemp,
minTemp = dailyWeather.minTemp,
feelsLike = feelsLike,
condition = condition,
description = description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package nexters.weski.weather

import nexters.weski.ski_resort.SkiResort
import org.springframework.data.jpa.repository.JpaRepository
import java.time.LocalDate

interface DailyWeatherRepository : JpaRepository<DailyWeather, Long> {
fun findAllBySkiResortResortId(resortId: Long): List<DailyWeather>
fun deleteByDDayGreaterThanEqual(dDay: Int)
fun findBySkiResortAndDDay(skiResort: SkiResort, dDay: Int): DailyWeather?
fun findBySkiResortAndForecastDate(skiResort: SkiResort, forecastDate: LocalDate): List<DailyWeather>
}

0 comments on commit 01d51b1

Please sign in to comment.