From 034dc8341dc1dd75cceacd52d54d42710284c83f Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Wed, 30 Apr 2025 19:41:24 +0000 Subject: [PATCH] feat: Add PUT endpoint for updating Fruit entity --- .../20250430_194116_f803493e_update_fruit.py | 20 +++++++++++++++++++ endpoints/fruits.put.py | 20 +++++++++++++++++++ helpers/fruit_helpers.py | 12 +++-------- models/fruit.py | 13 +++++++++++- 4 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 alembic/versions/20250430_194116_f803493e_update_fruit.py diff --git a/alembic/versions/20250430_194116_f803493e_update_fruit.py b/alembic/versions/20250430_194116_f803493e_update_fruit.py new file mode 100644 index 0000000..b23c000 --- /dev/null +++ b/alembic/versions/20250430_194116_f803493e_update_fruit.py @@ -0,0 +1,20 @@ +"""add helper function to handle fruit update logic +Revision ID: 8e7d9b6b9c21 +Revises: 1ad6a8ecd0d7 +Create Date: 2023-05-25 12:34:56.789012 + +""" + +# revision identifiers, used by Alembic +revision = '8e7d9b6b9c21' +down_revision = '1ad6a8ecd0d7' +branch_labels = None +depends_on = None + +def upgrade(): + # No database changes needed for adding a helper function + pass + +def downgrade(): + # No database changes needed for removing a helper function + pass \ No newline at end of file diff --git a/endpoints/fruits.put.py b/endpoints/fruits.put.py index e69de29..eff5cf6 100644 --- a/endpoints/fruits.put.py +++ b/endpoints/fruits.put.py @@ -0,0 +1,20 @@ +from fastapi import APIRouter, Depends, HTTPException +from sqlalchemy.orm import Session +from core.database import get_db +from schemas.fruit import FruitSchema, FruitUpdate +from helpers.fruit_helpers import get_fruit_by_id, update_fruit + +router = APIRouter() + +@router.put("/fruits", status_code=200, response_model=FruitSchema) +async def update_fruit_endpoint( + fruit_id: str, + fruit_data: FruitUpdate, + db: Session = Depends(get_db) +): + fruit = get_fruit_by_id(db, fruit_id) + if not fruit: + raise HTTPException(status_code=404, detail="Fruit not found") + + updated_fruit = update_fruit(db, fruit_id, fruit_data) + return updated_fruit \ No newline at end of file diff --git a/helpers/fruit_helpers.py b/helpers/fruit_helpers.py index 44f7c73..4d69e76 100644 --- a/helpers/fruit_helpers.py +++ b/helpers/fruit_helpers.py @@ -53,7 +53,7 @@ def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit: db.refresh(db_fruit) return db_fruit -def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Optional[Fruit]: +def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Fruit: """ Updates an existing fruit in the database. @@ -63,15 +63,9 @@ def update_fruit(db: Session, fruit_id: UUID, fruit_data: FruitUpdate) -> Option fruit_data (FruitUpdate): The updated data for the fruit. Returns: - Optional[Fruit]: The updated fruit object if found, otherwise None. + Fruit: The updated fruit object. """ - db_fruit = db.query(Fruit).filter(Fruit.id == fruit_id).first() - if not db_fruit: - return None - - for field, value in fruit_data.dict(exclude_unset=True).items(): - setattr(db_fruit, field, value) - + db_fruit = Fruit.update_fruit(fruit_id, **fruit_data.dict(exclude_unset=True)) db.commit() db.refresh(db_fruit) return db_fruit diff --git a/models/fruit.py b/models/fruit.py index a3faeed..f8e9832 100644 --- a/models/fruit.py +++ b/models/fruit.py @@ -17,4 +17,15 @@ class Fruit(Base): @classmethod def create_new_fruit(cls, name, description, price): new_fruit = cls(name=name, description=description, price=price) - return new_fruit \ No newline at end of file + return new_fruit + + @classmethod + def update_fruit(cls, fruit_id, name=None, description=None, price=None): + fruit = cls.query.get(fruit_id) + if name: + fruit.name = name + if description: + fruit.description = description + if price: + fruit.price = price + return fruit \ No newline at end of file