feat: Add PUT endpoint for updating Fruit entity
This commit is contained in:
parent
7b3934b43f
commit
034dc8341d
20
alembic/versions/20250430_194116_f803493e_update_fruit.py
Normal file
20
alembic/versions/20250430_194116_f803493e_update_fruit.py
Normal file
@ -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
|
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user