-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Nexters/feature/2-weather-api
[#2] 날씨 정보 조회 API
- Loading branch information
Showing
13 changed files
with
280 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nexters.weski.weather | ||
|
||
import jakarta.persistence.* | ||
import nexters.weski.common.BaseEntity | ||
import nexters.weski.ski_resort.SkiResort | ||
import java.time.LocalDateTime | ||
|
||
@Entity | ||
@Table(name = "hourly_weather") | ||
data class HourlyWeather( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0, | ||
|
||
val forecastTime: LocalDateTime, | ||
val temperature: Int, | ||
val precipitationChance: Int, | ||
val condition: String, | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "resort_id") | ||
val skiResort: SkiResort | ||
) : BaseEntity() |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/nexters/weski/weather/HourlyWeatherRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package nexters.weski.weather | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import java.time.LocalDateTime | ||
|
||
interface HourlyWeatherRepository : JpaRepository<HourlyWeather, Long> { | ||
fun findAllBySkiResortResortIdAndForecastTimeBetween( | ||
resortId: Long, | ||
startTime: LocalDateTime, | ||
endTime: LocalDateTime | ||
): List<HourlyWeather> | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/nexters/weski/weather/WeatherController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package nexters.weski.weather | ||
|
||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/weather") | ||
class WeatherController( | ||
private val weatherService: WeatherService | ||
) { | ||
@GetMapping("/{resortId}") | ||
fun getWeatherByResortId(@PathVariable resortId: Long): WeatherDto? { | ||
return weatherService.getWeatherByResortId(resortId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nexters.weski.weather | ||
|
||
import org.springframework.stereotype.Service | ||
import java.time.LocalDateTime | ||
import java.time.LocalTime | ||
|
||
@Service | ||
class WeatherService( | ||
private val currentWeatherRepository: CurrentWeatherRepository, | ||
private val hourlyWeatherRepository: HourlyWeatherRepository, | ||
private val dailyWeatherRepository: DailyWeatherRepository | ||
) { | ||
fun getWeatherByResortId(resortId: Long): WeatherDto? { | ||
val currentWeather = currentWeatherRepository.findBySkiResortResortId(resortId) ?: return null | ||
// 오늘, 내일 날짜의 날씨 정보만 가져옴 | ||
val startTime = LocalDateTime.now().with(LocalTime.MIN) | ||
val endTime = startTime.plusDays(2).with(LocalTime.MAX) | ||
val hourlyWeather = hourlyWeatherRepository.findAllBySkiResortResortIdAndForecastTimeBetween( | ||
resortId, startTime, endTime | ||
) | ||
val dailyWeather = dailyWeatherRepository.findAllBySkiResortResortId(resortId) | ||
|
||
return WeatherDto.fromEntities(currentWeather, hourlyWeather, dailyWeather) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/nexters/weski/weather/WeatherUpdateService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package nexters.weski.weather | ||
|
||
|
||
import nexters.weski.ski_resort.SkiResortRepository | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class WeatherUpdateService( | ||
private val skiResortRepository: SkiResortRepository, | ||
private val currentWeatherRepository: CurrentWeatherRepository, | ||
) { | ||
// @Scheduled(fixedRate = 3600000) // 1시간마다 실행 | ||
fun updateWeatherData() { | ||
val skiResorts = skiResortRepository.findAll() | ||
skiResorts.forEach { resort -> | ||
println(resort) | ||
// 외부 API 호출하여 날씨 정보 가져오기 | ||
// currentWeatherRepository.save(업데이트된 데이터) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.