Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RaminMammadzada/63297bfa9d5c455bc74722d4e4d3ecce to your computer and use it in GitHub Desktop.
Save RaminMammadzada/63297bfa9d5c455bc74722d4e4d3ecce to your computer and use it in GitHub Desktop.
Sample Project: Weather Forecasting System API

Absolutely! For the caching requirement, let's create a take-home assignment that involves building a caching layer into a weather forecasting API.

Project: Weather Forecasting System API

Objective: Implement a RESTful API for a Weather Forecasting System using Java, Spring Boot, and a caching mechanism. The API should allow users to request current weather forecasts for different cities and cache the results to improve performance.

Requirements:

  1. Initialize a new Spring Boot project using Spring Initializr. Choose the following dependencies: Web, JPA, H2, Cache, and RestTemplate.

  2. Structure your project with the following packages:

    • model: Contains domain model classes (City, Forecast)
    • repository: Contains repository interfaces extending JpaRepository
    • service: Contains service classes to encapsulate business logic
    • controller: Contains REST API controllers
    • exception: Contains custom exception classes
    • dto: Contains data transfer objects (DTOs) used in the API
  3. Implement the domain model:

    • City class: Fields should include id, name, and a list of Forecast relationships (OneToMany)
    • Forecast class: Fields should include id, timestamp, temperature, humidity, windSpeed, and a City relationship (ManyToOne)
  4. Implement the repositories:

    • CityRepository: Extends JpaRepository<City, Long>
    • ForecastRepository: Extends JpaRepository<Forecast, Long>
  5. Implement the services:

    • CityService: Contains methods for creating, updating, deleting, and retrieving cities
    • ForecastService: Contains methods for retrieving forecasts; it should use RestTemplate to call an external weather API (e.g., OpenWeatherMap, WeatherStack) and then store the results in a cache before returning them
  6. Implement the REST API controllers:

    • CityController: Handles HTTP requests for CRUD operations on cities
    • ForecastController: Handles HTTP requests for retrieving forecasts
  7. Implement DTOs for the API:

    • CityDTO: Contains fields for id, name
    • ForecastDTO: Contains fields for id, timestamp, temperature, humidity, windSpeed, and a CityDTO
  8. Implement exception handling:

    • Create custom exceptions (ResourceNotFoundException, BadRequestException)
    • Implement a global exception handler with @ControllerAdvice to handle exceptions and return appropriate HTTP response status codes
  9. Configure an H2 in-memory database for the application.

  10. Configure Spring's caching mechanism to cache forecasts for each city. You can use Spring's @Cacheable annotation to do this.

  11. Write unit tests for the service classes using JUnit and Mockito.

  12. (Optional) Implement API documentation using Swagger or OpenAPI.

Deliverables:

  • A fully functional Spring Boot project, including source code and tests
  • A README file with instructions on how to build and run the project, as well as an explanation of any design decisions you made

When you submit your project, please include all necessary files in a single archive (e.g., ZIP or TAR) and provide clear instructions for building and running your application. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment