feat: Generated endpoint endpoints/countries.get.py via AI for Country with auto lint fixes
This commit is contained in:
parent
5f32bf23c6
commit
7c4aba7cc6
31
alembic/versions/20250419_122511_440bcd2f_update_country.py
Normal file
31
alembic/versions/20250419_122511_440bcd2f_update_country.py
Normal file
@ -0,0 +1,31 @@
|
||||
"""create table for countries
|
||||
Revision ID: 1d2e3f4a5b6c
|
||||
Revises: 0001
|
||||
Create Date: 2023-05-18 12:34:56
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import func
|
||||
import uuid
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '1d2e3f4a5b6c'
|
||||
down_revision = '0001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'countries',
|
||||
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||
sa.Column('iso_code', sa.String(), nullable=False, unique=True),
|
||||
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_countries_name', 'name'),
|
||||
sa.Index('ix_countries_iso_code', 'iso_code')
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('countries')
|
@ -0,0 +1,13 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from typing import List
|
||||
from sqlalchemy.orm import Session
|
||||
from core.database import get_db
|
||||
from schemas.country import CountrySchema
|
||||
from helpers.country_helpers import get_european_countries
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/countries", status_code=200, response_model=List[CountrySchema])
|
||||
async def get_european_countries_endpoint(db: Session = Depends(get_db)):
|
||||
countries = get_european_countries(db)
|
||||
return countries
|
84
helpers/country_helpers.py
Normal file
84
helpers/country_helpers.py
Normal file
@ -0,0 +1,84 @@
|
||||
from typing import List, Optional
|
||||
from uuid import UUID
|
||||
from sqlalchemy.orm import Session
|
||||
from models.country import Country
|
||||
from schemas.country import CountryCreate, CountryUpdate
|
||||
|
||||
def get_all_countries(db: Session) -> List[Country]:
|
||||
"""
|
||||
Retrieves all countries from the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
|
||||
Returns:
|
||||
List[Country]: A list of all country objects.
|
||||
"""
|
||||
return db.query(Country).all()
|
||||
|
||||
def get_country_by_id(db: Session, country_id: UUID) -> Optional[Country]:
|
||||
"""
|
||||
Retrieves a single country by its ID.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
country_id (UUID): The ID of the country to retrieve.
|
||||
|
||||
Returns:
|
||||
Optional[Country]: The country object if found, otherwise None.
|
||||
"""
|
||||
return db.query(Country).filter(Country.id == country_id).first()
|
||||
|
||||
def create_country(db: Session, country_data: CountryCreate) -> Country:
|
||||
"""
|
||||
Creates a new country in the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
country_data (CountryCreate): The data for the country to create.
|
||||
|
||||
Returns:
|
||||
Country: The newly created country object.
|
||||
"""
|
||||
db_country = Country(**country_data.dict())
|
||||
db.add(db_country)
|
||||
db.commit()
|
||||
db.refresh(db_country)
|
||||
return db_country
|
||||
|
||||
def update_country(db: Session, country_id: UUID, country_data: CountryUpdate) -> Optional[Country]:
|
||||
"""
|
||||
Updates an existing country in the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
country_id (UUID): The ID of the country to update.
|
||||
country_data (CountryUpdate): The updated data for the country.
|
||||
|
||||
Returns:
|
||||
Optional[Country]: The updated country object if found, otherwise None.
|
||||
"""
|
||||
db_country = db.query(Country).filter(Country.id == country_id).first()
|
||||
if not db_country:
|
||||
return None
|
||||
|
||||
for field, value in country_data.dict(exclude_unset=True).items():
|
||||
setattr(db_country, field, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_country)
|
||||
return db_country
|
||||
|
||||
def get_european_countries(db: Session) -> List[Country]:
|
||||
"""
|
||||
Retrieves a list of European countries from the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
|
||||
Returns:
|
||||
List[Country]: A list of country objects representing European countries.
|
||||
"""
|
||||
# Define a list of ISO codes for European countries
|
||||
european_iso_codes = ["AL", "AD", "AT", "BY", "BE", "BA", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IS", "IE", "IT", "LV", "LI", "LT", "LU", "MT", "MD", "MC", "ME", "NL", "NO", "PL", "PT", "RO", "RU", "SM", "RS", "SK", "SI", "ES", "SE", "CH", "UA", "GB", "VA"]
|
||||
return db.query(Country).filter(Country.iso_code.in_(european_iso_codes)).all()
|
14
models/country.py
Normal file
14
models/country.py
Normal file
@ -0,0 +1,14 @@
|
||||
from sqlalchemy import Column, String, DateTime
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.sql import func
|
||||
from core.database import Base
|
||||
import uuid
|
||||
|
||||
class Country(Base):
|
||||
__tablename__ = "countries"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name = Column(String, nullable=False, unique=True, index=True)
|
||||
iso_code = Column(String, nullable=False, unique=True, index=True)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
23
schemas/country.py
Normal file
23
schemas/country.py
Normal file
@ -0,0 +1,23 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
class CountryBase(BaseModel):
|
||||
name: str = Field(..., description="Name of the country")
|
||||
iso_code: str = Field(..., description="ISO code of the country")
|
||||
|
||||
class CountryCreate(CountryBase):
|
||||
pass
|
||||
|
||||
class CountryUpdate(CountryBase):
|
||||
name: Optional[str] = Field(None, description="Name of the country")
|
||||
iso_code: Optional[str] = Field(None, description="ISO code of the country")
|
||||
|
||||
class CountrySchema(CountryBase):
|
||||
id: UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user