From a5dfe420e929d521750462aa24ec311c6153059a Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 14 Apr 2025 11:17:12 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/food-api.get.py via AI for Country --- ...20250414_111648_305e7051_update_country.py | 36 ++++++++++ endpoints/food-api.get.py | 16 +++++ helpers/country_helpers.py | 69 +++++++++++++++++++ models/country.py | 14 ++++ schemas/country.py | 23 +++++++ 5 files changed, 158 insertions(+) create mode 100644 alembic/versions/20250414_111648_305e7051_update_country.py create mode 100644 helpers/country_helpers.py create mode 100644 models/country.py create mode 100644 schemas/country.py diff --git a/alembic/versions/20250414_111648_305e7051_update_country.py b/alembic/versions/20250414_111648_305e7051_update_country.py new file mode 100644 index 0000000..c0c5845 --- /dev/null +++ b/alembic/versions/20250414_111648_305e7051_update_country.py @@ -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) \ No newline at end of file diff --git a/endpoints/food-api.get.py b/endpoints/food-api.get.py index e69de29..cc17735 100644 --- a/endpoints/food-api.get.py +++ b/endpoints/food-api.get.py @@ -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 \ No newline at end of file diff --git a/helpers/country_helpers.py b/helpers/country_helpers.py new file mode 100644 index 0000000..a3c2c32 --- /dev/null +++ b/helpers/country_helpers.py @@ -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() \ No newline at end of file diff --git a/models/country.py b/models/country.py new file mode 100644 index 0000000..6645ae9 --- /dev/null +++ b/models/country.py @@ -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()) \ No newline at end of file diff --git a/schemas/country.py b/schemas/country.py new file mode 100644 index 0000000..fd5c54a --- /dev/null +++ b/schemas/country.py @@ -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 \ No newline at end of file