-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWeatherCommandLineRunner.kt
29 lines (23 loc) · 1.1 KB
/
WeatherCommandLineRunner.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package hu.vanio.kotlin.feladat.ms
import hu.vanio.kotlin.feladat.ms.exception.ServiceUnavailable
import hu.vanio.kotlin.feladat.ms.openmeteo.WeatherForecasts
import hu.vanio.kotlin.feladat.ms.openmeteo.WeatherService
import org.springframework.boot.CommandLineRunner
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component
const val separator = "----------------------------------------------"
@Component
@Order(1)
class WeatherCommandLineRunner(val weatherService: WeatherService): CommandLineRunner {
override fun run(vararg args: String?) {
val weatherStatistics = try { weatherService.getWeatherStatistics() } catch (e: ServiceUnavailable) {
println("Weather forecast service is unavailable")
return
}
println("$separator\n${weatherStatistics.println()}$separator")
}
}
fun WeatherForecasts.println() =
"Weather forecast daily averages:\n$separator\n${this.weatherDailyForecast
.map { wdf -> "${wdf.day} | ${wdf.average()}"}
.reduce { first, second -> "${first}\n${second}"}}\n"