From 3fd22cdcd809fe2b579cfc7695528d8de2963acd Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 29 Apr 2025 07:25:31 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20Add=20Weather=20Forecast=20Service=20wi?= =?UTF-8?q?th=20new=20endpoint,=20model,=20schema,=20migration,=20and=20re?= =?UTF-8?q?quirements=20=E2=9C=85=20(auto-linted)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...072440_4768d695_update_weather_forecast.py | 35 +++++ endpoints/weather.get.py | 24 ++++ helpers/weather_forecast_helpers.py | 131 ++++++++++++++++++ models/weather_forecast.py | 19 +++ requirements.txt | 3 + schemas/weather_forecast.py | 33 +++++ 6 files changed, 245 insertions(+) create mode 100644 alembic/versions/20250429_072440_4768d695_update_weather_forecast.py create mode 100644 helpers/weather_forecast_helpers.py create mode 100644 models/weather_forecast.py create mode 100644 schemas/weather_forecast.py diff --git a/alembic/versions/20250429_072440_4768d695_update_weather_forecast.py b/alembic/versions/20250429_072440_4768d695_update_weather_forecast.py new file mode 100644 index 0000000..c22dc95 --- /dev/null +++ b/alembic/versions/20250429_072440_4768d695_update_weather_forecast.py @@ -0,0 +1,35 @@ +"""create table for weather_forecasts +Revision ID: 1a2b3c4d5e6f +Revises: 0001 +Create Date: 2023-05-24 12:00:00 +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func +import uuid + +# revision identifiers, used by Alembic. +revision = '1a2b3c4d5e6f' +down_revision = '0001' +branch_labels = None +depends_on = None + +def upgrade(): + op.create_table( + 'weather_forecasts', + sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())), + sa.Column('location', sa.String(), nullable=False), + sa.Column('date', sa.DateTime(), nullable=False), + sa.Column('temperature', sa.Float(), nullable=False), + sa.Column('humidity', sa.Float(), nullable=False), + sa.Column('wind_speed', sa.Float(), nullable=False), + sa.Column('precipitation_probability', sa.Float(), nullable=False), + sa.Column('description', sa.String(), nullable=False), + sa.Column('created_at', sa.DateTime(), server_default=func.now()), + sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now()), + sa.Index('ix_weather_forecasts_location', 'location'), + sa.Index('ix_weather_forecasts_date', 'date') + ) + +def downgrade(): + op.drop_table('weather_forecasts') \ No newline at end of file diff --git a/endpoints/weather.get.py b/endpoints/weather.get.py index e69de29..b14ac6e 100644 --- a/endpoints/weather.get.py +++ b/endpoints/weather.get.py @@ -0,0 +1,24 @@ +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 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 diff --git a/helpers/weather_forecast_helpers.py b/helpers/weather_forecast_helpers.py new file mode 100644 index 0000000..7793cbf --- /dev/null +++ b/helpers/weather_forecast_helpers.py @@ -0,0 +1,131 @@ +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 + +def get_weather_forecasts( + db: Session, location: Optional[str] = None, start_date: Optional[datetime] = None, end_date: Optional[datetime] = None +) -> List[WeatherForecast]: + """ + Retrieves weather forecasts from the database based on the provided filters. + + 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). + + Returns: + List[WeatherForecast]: A list of weather forecast objects matching the filters. + """ + 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() + +def get_weather_forecast_by_id(db: Session, forecast_id: Union[str, uuid.UUID]) -> Optional[WeatherForecast]: + """ + Retrieves a weather forecast by its ID. + + Args: + db (Session): The database session. + forecast_id (Union[str, uuid.UUID]): The ID of the weather forecast. + + Returns: + Optional[WeatherForecast]: The weather forecast object if found, otherwise None. + """ + return db.query(WeatherForecast).filter(WeatherForecast.id == forecast_id).first() + +def create_weather_forecast(db: Session, forecast_data: WeatherForecastCreate) -> WeatherForecastSchema: + """ + Creates a new weather forecast in the database. + + Args: + db (Session): The database session. + forecast_data (WeatherForecastCreate): The data for the weather forecast to create. + + Returns: + WeatherForecastSchema: The newly created weather forecast object. + """ + db_forecast = WeatherForecast(**forecast_data.dict()) + db.add(db_forecast) + db.commit() + db.refresh(db_forecast) + return WeatherForecastSchema.from_orm(db_forecast) + +def update_weather_forecast( + db: Session, forecast_id: Union[str, uuid.UUID], forecast_data: WeatherForecastUpdate +) -> WeatherForecastSchema: + """ + Updates an existing weather forecast in the database. + + Args: + db (Session): The database session. + forecast_id (Union[str, uuid.UUID]): The ID of the weather forecast to update. + forecast_data (WeatherForecastUpdate): The updated data for the weather forecast. + + Returns: + WeatherForecastSchema: The updated weather forecast object. + """ + db_forecast = db.query(WeatherForecast).filter(WeatherForecast.id == forecast_id).first() + if not db_forecast: + raise ValueError(f"Weather forecast with ID {forecast_id} not found") + + update_data = forecast_data.dict(exclude_unset=True) + for field, value in update_data.items(): + setattr(db_forecast, field, value) + + db.commit() + db.refresh(db_forecast) + return WeatherForecastSchema.from_orm(db_forecast) + +def delete_weather_forecast(db: Session, forecast_id: Union[str, uuid.UUID]) -> bool: + """ + Deletes a weather forecast from the database. + + Args: + db (Session): The database session. + forecast_id (Union[str, uuid.UUID]): The ID of the weather forecast to delete. + + Returns: + bool: True if the weather forecast was successfully deleted, False otherwise. + """ + db_forecast = db.query(WeatherForecast).filter(WeatherForecast.id == forecast_id).first() + if not db_forecast: + return False + + db.delete(db_forecast) + db.commit() + return True + +def get_average_temperature_by_location(db: Session, location: str, start_date: datetime, end_date: datetime) -> Optional[float]: + """ + Retrieves the average temperature for a given location and date range. + + Args: + db (Session): The database session. + location (str): The location to filter by. + start_date (datetime): The start date of the date range (inclusive). + end_date (datetime): The end date of the date range (inclusive). + + Returns: + Optional[float]: The average temperature if forecasts are found, otherwise None. + """ + avg_temp = db.query(func.avg(WeatherForecast.temperature)).filter( + WeatherForecast.location == location, + WeatherForecast.date >= start_date, + WeatherForecast.date <= end_date, + ).scalar() + + return avg_temp if avg_temp is not None else None \ No newline at end of file diff --git a/models/weather_forecast.py b/models/weather_forecast.py new file mode 100644 index 0000000..e0a7087 --- /dev/null +++ b/models/weather_forecast.py @@ -0,0 +1,19 @@ +from sqlalchemy import Column, String, Float, DateTime +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.sql import func +from core.database import Base +import uuid + +class WeatherForecast(Base): + __tablename__ = "weather_forecasts" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + location = Column(String, nullable=False, index=True) + date = Column(DateTime, nullable=False, index=True) + temperature = Column(Float, nullable=False) + humidity = Column(Float, nullable=False) + wind_speed = Column(Float, nullable=False) + precipitation_probability = Column(Float, nullable=False) + description = Column(String, nullable=False) + created_at = Column(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 596e6f3..db12c92 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,6 @@ sqlalchemy>=1.4.0 python-dotenv>=0.19.0 bcrypt>=3.2.0 alembic>=1.13.1 +jose +passlib +pydantic diff --git a/schemas/weather_forecast.py b/schemas/weather_forecast.py new file mode 100644 index 0000000..cf71e28 --- /dev/null +++ b/schemas/weather_forecast.py @@ -0,0 +1,33 @@ +from pydantic import BaseModel +from typing import Optional +from datetime import datetime +import uuid + +class WeatherForecastBase(BaseModel): + location: str + date: datetime + temperature: float + humidity: float + wind_speed: float + precipitation_probability: float + description: str + +class WeatherForecastCreate(WeatherForecastBase): + pass + +class WeatherForecastUpdate(WeatherForecastBase): + location: Optional[str] = None + date: Optional[datetime] = None + temperature: Optional[float] = None + humidity: Optional[float] = None + wind_speed: Optional[float] = None + precipitation_probability: Optional[float] = None + description: Optional[str] = None + +class WeatherForecastSchema(WeatherForecastBase): + id: uuid.UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file