feat: Generated endpoint endpoints/nigeria-states.get.py via AI for City
This commit is contained in:
parent
87ae7f6b1a
commit
dcc3bbdf46
35
alembic/versions/20250421_223703_b866ca73_update_city.py
Normal file
35
alembic/versions/20250421_223703_b866ca73_update_city.py
Normal file
@ -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)
|
@ -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
|
34
helpers/city_helpers.py
Normal file
34
helpers/city_helpers.py
Normal file
@ -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
|
16
models/city.py
Normal file
16
models/city.py
Normal file
@ -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())
|
26
schemas/city.py
Normal file
26
schemas/city.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user