Class WeatherService
java.lang.Object
gr.unipi.weather_app.service.WeatherService
Service class responsible for handling weather-related operations.
This class interacts with an external weather API to fetch real-time weather data, stores it in the database, and provides analytical weather reports.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final WeatherRepositoryprivate StringThe URL extension for the weather API.private final org.springframework.web.reactive.function.client.WebClient -
Constructor Summary
ConstructorsConstructorDescriptionWeatherService(WeatherRepository repository, org.springframework.web.reactive.function.client.WebClient webClient) Constructs a newWeatherServicewith the specified repository and WebClient. -
Method Summary
Modifier and TypeMethodDescriptionFinds and returns the coldest city recorded in the database.Finds and returns the hottest city recorded in the database.private StringformatAverageWeather(String location, Object[] avgWeather) Formats average weather data into a human-readable string.Retrieves the average weather statistics for a specific city.getAverageWeatherForLastDays(int days) Retrieves the average weather statistics for the last specified number of days.Retrieves the most searched cities based on the number of queries stored in the database.getWeather(String city) Fetches weather data for a given city from an external API and saves it to the database.
-
Field Details
-
webClient
private final org.springframework.web.reactive.function.client.WebClient webClient -
repository
-
urlParams
The URL extension for the weather API.The `%s` placeholder in the URL represents the city name, and `j1` indicates the JSON format.
-
-
Constructor Details
-
WeatherService
public WeatherService(WeatherRepository repository, org.springframework.web.reactive.function.client.WebClient webClient) Constructs a newWeatherServicewith the specified repository and WebClient.- Parameters:
repository- the repository used for weather data persistence and retrievalwebClient- the WebClient used to fetch weather data from an external API
-
-
Method Details
-
getWeather
Fetches weather data for a given city from an external API and saves it to the database.- Parameters:
city- the name of the city- Returns:
- the saved
WeatherDataentity containing weather details - Throws:
RuntimeException- if an error occurs while fetching data from the API
-
getAverageWeatherForCity
-
getAverageWeatherForLastDays
Retrieves the average weather statistics for the last specified number of days.- Parameters:
days- the number of days for which the average should be calculated- Returns:
- a formatted string containing the average weather details
-
findHottestCity
Finds and returns the hottest city recorded in the database.- Returns:
- a formatted string containing the hottest city's name and temperature
-
findColdestCity
Finds and returns the coldest city recorded in the database.- Returns:
- a formatted string containing the coldest city's name and temperature
-
getMostSearchedCities
Retrieves the most searched cities based on the number of queries stored in the database.- Returns:
- a formatted string listing the most searched cities and their search counts
-
formatAverageWeather
Formats average weather data into a human-readable string.- Parameters:
location- the location for which the weather data is being displayedavgWeather- an array containing average weather values- Returns:
- a formatted string containing average weather statistics
-