Class WeatherService

java.lang.Object
gr.unipi.weather_app.service.WeatherService

@Service public class WeatherService extends Object
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 Details

    • webClient

      private final org.springframework.web.reactive.function.client.WebClient webClient
    • repository

      private final WeatherRepository repository
    • urlParams

      @Value("${weather.api.url-params}") private String 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 new WeatherService with the specified repository and WebClient.
      Parameters:
      repository - the repository used for weather data persistence and retrieval
      webClient - the WebClient used to fetch weather data from an external API
  • Method Details

    • getWeather

      public WeatherData getWeather(String city)
      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 WeatherData entity containing weather details
      Throws:
      RuntimeException - if an error occurs while fetching data from the API
    • getAverageWeatherForCity

      public String getAverageWeatherForCity(String city)
      Retrieves the average weather statistics for a specific city.
      Parameters:
      city - the name of the city
      Returns:
      a formatted string containing the average weather details
    • getAverageWeatherForLastDays

      public String getAverageWeatherForLastDays(int days)
      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

      public String findHottestCity()
      Finds and returns the hottest city recorded in the database.
      Returns:
      a formatted string containing the hottest city's name and temperature
    • findColdestCity

      public String findColdestCity()
      Finds and returns the coldest city recorded in the database.
      Returns:
      a formatted string containing the coldest city's name and temperature
    • getMostSearchedCities

      public String 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

      private String formatAverageWeather(String location, Object[] avgWeather)
      Formats average weather data into a human-readable string.
      Parameters:
      location - the location for which the weather data is being displayed
      avgWeather - an array containing average weather values
      Returns:
      a formatted string containing average weather statistics