From c9af87894528e189772912a3dfed5f060497d145 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 14 Apr 2025 14:36:31 +0000 Subject: [PATCH] feat: Updated endpoint endpoints/countries.post.py via AI with auto lint fixes --- ...20250414_143618_3dc10b52_update_country.py | 20 +++++++++++++++++++ endpoints/countries.post.py | 12 ++++------- helpers/country_helpers.py | 4 ++-- models/country.py | 13 +++++++++++- schemas/country.py | 12 +++++++++++ 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 alembic/versions/20250414_143618_3dc10b52_update_country.py diff --git a/alembic/versions/20250414_143618_3dc10b52_update_country.py b/alembic/versions/20250414_143618_3dc10b52_update_country.py new file mode 100644 index 0000000..7ca5eec --- /dev/null +++ b/alembic/versions/20250414_143618_3dc10b52_update_country.py @@ -0,0 +1,20 @@ +"""add continent column to countries table +Revision ID: 2fb9c8d6e5a4 +Revises: 2b7a3c5d1e6a +Create Date: 2023-05-30 12:34:56 + +""" +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic +revision = '2fb9c8d6e5a4' +down_revision = '2b7a3c5d1e6a' +branch_labels = None +depends_on = None + +def upgrade(): + op.add_column('countries', sa.Column('continent', sa.String(), nullable=False)) + +def downgrade(): + op.drop_column('countries', 'continent') \ No newline at end of file diff --git a/endpoints/countries.post.py b/endpoints/countries.post.py index 5945d33..516990d 100644 --- a/endpoints/countries.post.py +++ b/endpoints/countries.post.py @@ -1,19 +1,15 @@ -from fastapi import APIRouter, Depends, status +from fastapi import APIRouter, Depends from sqlalchemy.orm import Session from core.database import get_db -from schemas.country import CountryCreate, CountrySchema -from helpers.country_helpers import get_country_by_name, create_country +from schemas.country import CountryCreate +from helpers.country_helpers import create_country router = APIRouter() -@router.post("/countries", status_code=status.HTTP_201_CREATED, response_model=CountrySchema) +@router.post("/countries", status_code=201) async def create_new_country( country_data: CountryCreate, db: Session = Depends(get_db) ): - existing_country = get_country_by_name(db, name=country_data.name) - if existing_country: - return existing_country - new_country = create_country(db, country_data) return new_country \ No newline at end of file diff --git a/helpers/country_helpers.py b/helpers/country_helpers.py index 7370c0a..16d7fb2 100644 --- a/helpers/country_helpers.py +++ b/helpers/country_helpers.py @@ -18,7 +18,7 @@ def get_country_by_name(db: Session, name: str) -> Optional[Country]: def create_country(db: Session, country_data: CountryCreate) -> Country: """ - Creates a new country in the database. + Creates a new country in the database, including the continent. Args: db (Session): The database session. @@ -48,7 +48,7 @@ def get_all_countries(db: Session) -> List[CountrySchema]: def update_country(db: Session, country_id: str, country_data: CountryUpdate) -> Optional[CountrySchema]: """ - Updates an existing country in the database. + Updates an existing country in the database, including the continent. Args: db (Session): The database session. diff --git a/models/country.py b/models/country.py index 6ea489b..facab0e 100644 --- a/models/country.py +++ b/models/country.py @@ -1,8 +1,18 @@ -from sqlalchemy import Column, String, Float +from sqlalchemy import Column, String, Float, Enum from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.sql import func from core.database import Base import uuid +import enum + +class Continent(enum.Enum): + AFRICA = "Africa" + ANTARCTICA = "Antarctica" + ASIA = "Asia" + EUROPE = "Europe" + NORTH_AMERICA = "North America" + OCEANIA = "Oceania" + SOUTH_AMERICA = "South America" class Country(Base): __tablename__ = "countries" @@ -11,5 +21,6 @@ class Country(Base): name = Column(String, nullable=False, unique=True, index=True) latitude = Column(Float, nullable=False) longitude = Column(Float, nullable=False) + continent = Column(Enum(Continent), nullable=False) created_at = Column(Float, default=func.now()) updated_at = Column(Float, default=func.now(), onupdate=func.now()) \ No newline at end of file diff --git a/schemas/country.py b/schemas/country.py index 1523b19..33736a6 100644 --- a/schemas/country.py +++ b/schemas/country.py @@ -2,11 +2,22 @@ from pydantic import BaseModel, Field from typing import Optional from datetime import datetime import uuid +import enum + +class Continent(enum.Enum): + AFRICA = "Africa" + ANTARCTICA = "Antarctica" + ASIA = "Asia" + EUROPE = "Europe" + NORTH_AMERICA = "North America" + OCEANIA = "Oceania" + SOUTH_AMERICA = "South America" class CountryBase(BaseModel): name: str = Field(..., description="Country name") latitude: float = Field(..., description="Country latitude") longitude: float = Field(..., description="Country longitude") + continent: Continent = Field(..., description="Country continent") class CountryCreate(CountryBase): pass @@ -15,6 +26,7 @@ class CountryUpdate(CountryBase): name: Optional[str] = Field(None, description="Country name") latitude: Optional[float] = Field(None, description="Country latitude") longitude: Optional[float] = Field(None, description="Country longitude") + continent: Optional[Continent] = Field(None, description="Country continent") class CountrySchema(CountryBase): id: uuid.UUID