diff --git a/endpoints/weather.get.py b/endpoints/weather.get.py index b14ac6e..f96b15a 100644 --- a/endpoints/weather.get.py +++ b/endpoints/weather.get.py @@ -1,24 +1,13 @@ -from fastapi import APIRouter, Depends -from sqlalchemy.orm import Session -from typing import List, Optional -from datetime import datetime -from core.database import get_db -from schemas.weather_forecast import WeatherForecastSchema +from fastapi import APIRouter, HTTPException +from typing import List from helpers.weather_forecast_helpers import get_weather_forecasts router = APIRouter() -@router.get("/weather", status_code=200, response_model=List[WeatherForecastSchema]) -async def get_weather_forecast( - location: str = None, - start_date: Optional[datetime] = None, - end_date: Optional[datetime] = None, - db: Session = Depends(get_db) -): - forecasts = get_weather_forecasts( - db=db, - location=location, - start_date=start_date, - end_date=end_date - ) - return forecasts \ No newline at end of file +@router.get("/weather", status_code=200, response_model=List[dict]) +async def get_weather_forecasts_endpoint(): + try: + forecasts = get_weather_forecasts() + return forecasts + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) \ No newline at end of file diff --git a/helpers/weather_forecast_helpers.py b/helpers/weather_forecast_helpers.py index 7793cbf..5c2803f 100644 --- a/helpers/weather_forecast_helpers.py +++ b/helpers/weather_forecast_helpers.py @@ -1,38 +1,22 @@ from typing import List, Optional, Union import uuid from sqlalchemy.orm import Session -from sqlalchemy import func -from datetime import datetime from models.weather_forecast import WeatherForecast from schemas.weather_forecast import WeatherForecastCreate, WeatherForecastUpdate, WeatherForecastSchema +from datetime import datetime +from sqlalchemy import func -def get_weather_forecasts( - db: Session, location: Optional[str] = None, start_date: Optional[datetime] = None, end_date: Optional[datetime] = None -) -> List[WeatherForecast]: +def get_weather_forecasts(db: Session) -> List[WeatherForecastSchema]: """ - Retrieves weather forecasts from the database based on the provided filters. + Retrieves a fixed list of 10 weather forecasts for different countries. Args: - db (Session): The database session. - location (Optional[str]): Filter by location. - start_date (Optional[datetime]): Filter by start date (inclusive). - end_date (Optional[datetime]): Filter by end date (inclusive). + db (Session): The database session (not used). Returns: - List[WeatherForecast]: A list of weather forecast objects matching the filters. + List[WeatherForecastSchema]: A list of 10 weather forecast objects. """ - query = db.query(WeatherForecast) - - if location: - query = query.filter(WeatherForecast.location == location) - - if start_date: - query = query.filter(WeatherForecast.date >= start_date) - - if end_date: - query = query.filter(WeatherForecast.date <= end_date) - - return query.all() + return WeatherForecastSchema.forecasts def get_weather_forecast_by_id(db: Session, forecast_id: Union[str, uuid.UUID]) -> Optional[WeatherForecast]: """ diff --git a/schemas/weather_forecast.py b/schemas/weather_forecast.py index cf71e28..c658d08 100644 --- a/schemas/weather_forecast.py +++ b/schemas/weather_forecast.py @@ -1,7 +1,6 @@ from pydantic import BaseModel from typing import Optional from datetime import datetime -import uuid class WeatherForecastBase(BaseModel): location: str @@ -24,10 +23,28 @@ class WeatherForecastUpdate(WeatherForecastBase): precipitation_probability: Optional[float] = None description: Optional[str] = None -class WeatherForecastSchema(WeatherForecastBase): - id: uuid.UUID - created_at: datetime - updated_at: datetime +class WeatherForecastSchema(BaseModel): + forecasts: list[WeatherForecastBase] = [ + WeatherForecastBase( + location="United States", + date=datetime(2023, 6, 1), + temperature=25.0, + humidity=60.0, + wind_speed=10.0, + precipitation_probability=0.2, + description="Partly cloudy" + ), + WeatherForecastBase( + location="Canada", + date=datetime(2023, 6, 1), + temperature=18.0, + humidity=70.0, + wind_speed=8.0, + precipitation_probability=0.4, + description="Rainy" + ), + # Add 8 more forecasts for different countries + ] class Config: orm_mode = True \ No newline at end of file