feat: Generated endpoint endpoints/food-api.get.py via AI for Country
This commit is contained in:
parent
6a4c60283f
commit
a5dfe420e9
36
alembic/versions/20250414_111648_305e7051_update_country.py
Normal file
36
alembic/versions/20250414_111648_305e7051_update_country.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
"""create table for countries
|
||||||
|
Revision ID: 2f8d421e8c9b
|
||||||
|
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 = '2f8d421e8c9b'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
table_name = "countries"
|
||||||
|
|
||||||
|
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, unique=True),
|
||||||
|
sa.Column('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()),
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_countries_name'), table_name, ['name'], unique=True)
|
||||||
|
op.create_index(op.f('ix_countries_code'), table_name, ['code'], unique=True)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
table_name = "countries"
|
||||||
|
op.drop_index(op.f('ix_countries_name'), table_name=table_name)
|
||||||
|
op.drop_index(op.f('ix_countries_code'), table_name=table_name)
|
||||||
|
op.drop_table(table_name)
|
@ -0,0 +1,16 @@
|
|||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
|
from schemas.country import CountrySchema
|
||||||
|
from helpers.country_helpers import get_countries_by_names_or_codes
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/food-api", status_code=200, response_model=List[CountrySchema])
|
||||||
|
async def get_countries_by_names_or_codes_endpoint(
|
||||||
|
names_or_codes: List[str],
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
countries = get_countries_by_names_or_codes(db, names_or_codes)
|
||||||
|
return countries
|
69
helpers/country_helpers.py
Normal file
69
helpers/country_helpers.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import or_
|
||||||
|
from models.country import Country
|
||||||
|
|
||||||
|
def get_countries_by_names(db: Session, country_names: List[str]) -> List[Country]:
|
||||||
|
"""
|
||||||
|
Retrieves a list of countries by their names.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
country_names (List[str]): A list of country names.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Country]: A list of country objects matching the provided names.
|
||||||
|
"""
|
||||||
|
return db.query(Country).filter(Country.name.in_(country_names)).all()
|
||||||
|
|
||||||
|
def get_countries_by_codes(db: Session, country_codes: List[str]) -> List[Country]:
|
||||||
|
"""
|
||||||
|
Retrieves a list of countries by their codes.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
country_codes (List[str]): A list of country codes.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Country]: A list of country objects matching the provided codes.
|
||||||
|
"""
|
||||||
|
return db.query(Country).filter(Country.code.in_(country_codes)).all()
|
||||||
|
|
||||||
|
def get_countries_by_names_or_codes(db: Session, names_or_codes: List[str]) -> List[Country]:
|
||||||
|
"""
|
||||||
|
Retrieves a list of countries by their names or codes.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
names_or_codes (List[str]): A list of country names or codes.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Country]: A list of country objects matching the provided names or codes.
|
||||||
|
"""
|
||||||
|
return db.query(Country).filter(or_(Country.name.in_(names_or_codes), Country.code.in_(names_or_codes))).all()
|
||||||
|
|
||||||
|
def get_country_by_name(db: Session, country_name: str) -> Optional[Country]:
|
||||||
|
"""
|
||||||
|
Retrieves a country by its name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
country_name (str): The name of the country.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Country]: The country object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Country).filter(Country.name == country_name).first()
|
||||||
|
|
||||||
|
def get_country_by_code(db: Session, country_code: str) -> Optional[Country]:
|
||||||
|
"""
|
||||||
|
Retrieves a country by its code.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
country_code (str): The code of the country.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Country]: The country object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Country).filter(Country.code == country_code).first()
|
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)
|
||||||
|
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="Country name")
|
||||||
|
code: str = Field(..., description="Country code")
|
||||||
|
|
||||||
|
class CountryCreate(CountryBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class CountryUpdate(CountryBase):
|
||||||
|
name: Optional[str] = Field(None, description="Country name")
|
||||||
|
code: Optional[str] = Field(None, description="Country code")
|
||||||
|
|
||||||
|
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