From dcc3bbdf467265ca991e7997fdf21d7e05d0be55 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 21 Apr 2025 22:37:17 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/nigeria-states.get.py via AI for City --- .../20250421_223703_b866ca73_update_city.py | 35 +++++++++++++++++++ endpoints/nigeria-states.get.py | 16 +++++++++ helpers/city_helpers.py | 34 ++++++++++++++++++ models/city.py | 16 +++++++++ schemas/city.py | 26 ++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 alembic/versions/20250421_223703_b866ca73_update_city.py create mode 100644 helpers/city_helpers.py create mode 100644 models/city.py create mode 100644 schemas/city.py diff --git a/alembic/versions/20250421_223703_b866ca73_update_city.py b/alembic/versions/20250421_223703_b866ca73_update_city.py new file mode 100644 index 0000000..fe39639 --- /dev/null +++ b/alembic/versions/20250421_223703_b866ca73_update_city.py @@ -0,0 +1,35 @@ +"""create table for cities +Revision ID: 7f8dc3a4b9c1 +Revises: 0001 +Create Date: 2023-05-25 12:34:56 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func +import uuid + +# revision identifiers, used by Alembic. +revision = '7f8dc3a4b9c1' +down_revision = '0001' +branch_labels = None +depends_on = None + +def upgrade(): + table_name = "cities" + + op.create_table( + table_name, + sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())), + sa.Column('name', sa.String(), nullable=False), + sa.Column('state', sa.String(), nullable=False), + sa.Column('country', sa.String(), nullable=True, server_default='Nigeria'), + sa.Column('population', sa.Integer(), 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_cities_name', 'name') + ) + +def downgrade(): + table_name = "cities" + op.drop_table(table_name) \ No newline at end of file diff --git a/endpoints/nigeria-states.get.py b/endpoints/nigeria-states.get.py index e69de29..2bab0e1 100644 --- a/endpoints/nigeria-states.get.py +++ b/endpoints/nigeria-states.get.py @@ -0,0 +1,16 @@ +from fastapi import APIRouter, Depends +from typing import List +from sqlalchemy.orm import Session +from core.database import get_db +from schemas.city import CitySchema +from helpers.city_helpers import get_random_cities_in_nigeria + +router = APIRouter() + +@router.get("/nigeria-states", status_code=200, response_model=List[CitySchema]) +async def get_random_cities( + limit: int = 10, + db: Session = Depends(get_db) +): + cities = get_random_cities_in_nigeria(db=db, limit=limit) + return cities \ No newline at end of file diff --git a/helpers/city_helpers.py b/helpers/city_helpers.py new file mode 100644 index 0000000..05758e2 --- /dev/null +++ b/helpers/city_helpers.py @@ -0,0 +1,34 @@ +import random +from typing import List, Optional +from sqlalchemy.orm import Session +from models.city import City +from schemas.city import CitySchema + +def get_random_cities_in_nigeria(db: Session, limit: int = 10) -> List[CitySchema]: + """ + Retrieves a list of random cities in Nigeria from the database. + + Args: + db (Session): The database session. + limit (int, optional): The maximum number of cities to retrieve. Defaults to 10. + + Returns: + List[CitySchema]: A list of random city schemas. + """ + cities = db.query(City).filter(City.country == "Nigeria").all() + random_cities = random.sample(cities, min(limit, len(cities))) + return [CitySchema.from_orm(city) for city in random_cities] + +def get_city_by_id(db: Session, city_id: str) -> Optional[CitySchema]: + """ + Retrieves a city by its ID from the database. + + Args: + db (Session): The database session. + city_id (str): The ID of the city to retrieve. + + Returns: + Optional[CitySchema]: The city schema if found, otherwise None. + """ + city = db.query(City).filter(City.id == city_id).first() + return CitySchema.from_orm(city) if city else None \ No newline at end of file diff --git a/models/city.py b/models/city.py new file mode 100644 index 0000000..63f69e4 --- /dev/null +++ b/models/city.py @@ -0,0 +1,16 @@ +from sqlalchemy import Column, String, Integer, DateTime +from sqlalchemy.sql import func +from sqlalchemy.dialects.postgresql import UUID +from core.database import Base +import uuid + +class City(Base): + __tablename__ = "cities" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name = Column(String, nullable=False, index=True) + state = Column(String, nullable=False) + country = Column(String, default="Nigeria") + population = Column(Integer, 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/schemas/city.py b/schemas/city.py new file mode 100644 index 0000000..f7b4766 --- /dev/null +++ b/schemas/city.py @@ -0,0 +1,26 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +import uuid + +class CityBase(BaseModel): + name: str = Field(..., description="Name of the city") + state: str = Field(..., description="State the city belongs to") + country: str = Field("Nigeria", description="Country the city belongs to") + population: int = Field(..., description="Population of the city") + +class CityCreate(CityBase): + pass + +class CityUpdate(CityBase): + name: Optional[str] = Field(None, description="Name of the city") + state: Optional[str] = Field(None, description="State the city belongs to") + population: Optional[int] = Field(None, description="Population of the city") + +class CitySchema(CityBase): + id: uuid.UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file