feat: Updated endpoint endpoints/countries.post.py via AI with auto lint fixes
This commit is contained in:
parent
6f032ff4c6
commit
c9af878945
20
alembic/versions/20250414_143618_3dc10b52_update_country.py
Normal file
20
alembic/versions/20250414_143618_3dc10b52_update_country.py
Normal file
@ -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')
|
@ -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
|
@ -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.
|
||||
|
@ -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())
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user