You are required to create a FastAPI application that manages city data and their corresponding temperature data. The application will have two main components (apps):
- A CRUD (Create, Read, Update, Delete) API for managing city data.
- An API that fetches current temperature data for all cities in the database and stores this data in the database. This API should also provide a list endpoint to retrieve the history of all temperature data.
- Create a new FastAPI application.
- Define a Pydantic model
City
with the following fields:id
: a unique identifier for the city.name
: the name of the city.additional_info
: any additional information about the city.
- Implement a SQLite database using SQLAlchemy and create a corresponding
City
table. - Implement the following endpoints:
POST /cities
: Create a new city.GET /cities
: Get a list of all cities.- Optional:
GET /cities/{city_id}
: Get the details of a specific city. - Optional:
PUT /cities/{city_id}
: Update the details of a specific city. DELETE /cities/{city_id}
: Delete a specific city.
- Define a Pydantic model
Temperature
with the following fields:id
: a unique identifier for the temperature record.city_id
: a reference to the city.date_time
: the date and time when the temperature was recorded.temperature
: the recorded temperature.
- Create a corresponding
Temperature
table in the database. - Implement an endpoint
POST /temperatures/update
that fetches the current temperature for all cities in the database from an online resource of your choice. Store this data in theTemperature
table. You should use an async function to fetch the temperature data. - Implement the following endpoints:
GET /temperatures
: Get a list of all temperature records.GET /temperatures/?city_id={city_id}
: Get the temperature records for a specific city.
- Use dependency injection where appropriate.
- Organize your project according to the FastAPI project structure guidelines.
Your task will be evaluated based on the following criteria:
- Functionality: Your application should meet all the requirements outlined above.
- Code Quality: Your code should be clean, readable, and well-organized.
- Error Handling: Your application should handle potential errors gracefully.
- Documentation: Your code should be well-documented (README.md).
Please submit the following:
- The complete source code of your application.
- A README file that includes:
- Instructions on how to run your application.
- A brief explanation of your design choices.
- Any assumptions or simplifications you made.
Good luck!