From 46ff51a4f947588a1f5bccc5824846a4b60e8e4e Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sat, 12 Apr 2025 02:06:48 +0000 Subject: [PATCH] feat: Updated endpoint endpoints/fruits.get.py via AI --- .../20250412_020645_39e66f0b_update_fruit.py | 19 +++++++++++++++++++ endpoints/fruits.get.py | 2 +- helpers/fruit_helpers.py | 4 ++-- models/fruit.py | 1 + schemas/fruit.py | 8 ++++---- 5 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 alembic/versions/20250412_020645_39e66f0b_update_fruit.py diff --git a/alembic/versions/20250412_020645_39e66f0b_update_fruit.py b/alembic/versions/20250412_020645_39e66f0b_update_fruit.py new file mode 100644 index 0000000..61ebe26 --- /dev/null +++ b/alembic/versions/20250412_020645_39e66f0b_update_fruit.py @@ -0,0 +1,19 @@ +"""add country field to fruits table +Revision ID: f7d3e8a9c2b6 +Revises: b4f8c2e9d1a5 +Create Date: 2024-01-30 10:00:00.000000 +""" +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = 'f7d3e8a9c2b6' +down_revision = 'b4f8c2e9d1a5' +branch_labels = None +depends_on = None + +def upgrade(): + op.add_column('fruits', sa.Column('country', sa.String(), nullable=True)) + +def downgrade(): + op.drop_column('fruits', 'country') \ No newline at end of file diff --git a/endpoints/fruits.get.py b/endpoints/fruits.get.py index 723a551..4c6d614 100644 --- a/endpoints/fruits.get.py +++ b/endpoints/fruits.get.py @@ -7,7 +7,7 @@ from helpers.fruit_helpers import get_all_fruits router = APIRouter() -@router.get("/fruits", response_model=List[FruitSchema]) +@router.get("/fruits", response_model=List[FruitSchema], status_code=200) async def get_fruits(db: Session = Depends(get_db)): fruits = get_all_fruits(db) return fruits \ No newline at end of file diff --git a/helpers/fruit_helpers.py b/helpers/fruit_helpers.py index aff6355..1231144 100644 --- a/helpers/fruit_helpers.py +++ b/helpers/fruit_helpers.py @@ -56,7 +56,7 @@ def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit: Args: db (Session): The database session - fruit_data (FruitCreate): The data for the fruit to create + fruit_data (FruitCreate): The data for the fruit to create, including optional country Returns: Fruit: The newly created fruit object @@ -74,7 +74,7 @@ def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Option Args: db (Session): The database session fruit_id (UUID): The ID of the fruit to update - fruit_data (FruitUpdate): The update data for the fruit + fruit_data (FruitUpdate): The update data for the fruit, including optional country Returns: Optional[Fruit]: The updated fruit object if found, otherwise None diff --git a/models/fruit.py b/models/fruit.py index 764ed44..0e4020a 100644 --- a/models/fruit.py +++ b/models/fruit.py @@ -10,6 +10,7 @@ class Fruit(Base): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = Column(String, unique=True, nullable=False, index=True) description = Column(String, nullable=True) + country = Column(String, nullable=True) is_active = Column(Boolean, default=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/fruit.py b/schemas/fruit.py index c5b0c95..a815d64 100644 --- a/schemas/fruit.py +++ b/schemas/fruit.py @@ -8,8 +8,8 @@ class FruitName(BaseModel): class FruitBase(BaseModel): name: str = Field(..., min_length=1, description="Fruit name") - decorative_name: str = Field(..., description="Decorative prefix for the fruit name") description: Optional[str] = Field(None, description="Fruit description") + country: Optional[str] = Field(None, description="Country of origin") is_active: bool = Field(True, description="Whether the fruit is active") class FruitCreate(FruitBase): @@ -17,8 +17,8 @@ class FruitCreate(FruitBase): class FruitUpdate(BaseModel): name: Optional[str] = Field(None, min_length=1, description="Fruit name") - decorative_name: Optional[str] = Field(None, description="Decorative prefix for the fruit name") - description: Optional[str] = Field(None, description="Fruit description") + description: Optional[str] = Field(None, description="Fruit description") + country: Optional[str] = Field(None, description="Country of origin") is_active: Optional[bool] = Field(None, description="Whether the fruit is active") class FruitSchema(FruitBase): @@ -32,8 +32,8 @@ class FruitSchema(FruitBase): "example": { "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "name": "Apple", - "decorative_name": "Sweet Apple", "description": "A sweet, edible fruit produced by an apple tree", + "country": "United States", "is_active": True, "created_at": "2023-01-01T12:00:00", "updated_at": "2023-01-01T12:00:00"