From 7c4aba7cc650408982352a0112005c71f92d102b Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sat, 19 Apr 2025 12:25:42 -0500 Subject: [PATCH] feat: Generated endpoint endpoints/countries.get.py via AI for Country with auto lint fixes --- ...20250419_122511_440bcd2f_update_country.py | 31 +++++++ endpoints/countries.get.py | 13 +++ helpers/country_helpers.py | 84 +++++++++++++++++++ models/country.py | 14 ++++ schemas/country.py | 23 +++++ 5 files changed, 165 insertions(+) create mode 100644 alembic/versions/20250419_122511_440bcd2f_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/20250419_122511_440bcd2f_update_country.py b/alembic/versions/20250419_122511_440bcd2f_update_country.py new file mode 100644 index 0000000..e94931a --- /dev/null +++ b/alembic/versions/20250419_122511_440bcd2f_update_country.py @@ -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') \ No newline at end of file diff --git a/endpoints/countries.get.py b/endpoints/countries.get.py index e69de29..a403ba6 100644 --- a/endpoints/countries.get.py +++ b/endpoints/countries.get.py @@ -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 \ No newline at end of file diff --git a/helpers/country_helpers.py b/helpers/country_helpers.py new file mode 100644 index 0000000..b184b4e --- /dev/null +++ b/helpers/country_helpers.py @@ -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() \ No newline at end of file diff --git a/models/country.py b/models/country.py new file mode 100644 index 0000000..354c31f --- /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) + 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()) \ No newline at end of file diff --git a/schemas/country.py b/schemas/country.py new file mode 100644 index 0000000..9e53931 --- /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="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 \ No newline at end of file