refactor: Modify PUT /fruits endpoint to return updated fruit in data object ✅ (auto-linted)
This commit is contained in:
parent
034dc8341d
commit
1f41289194
@ -1,20 +1,15 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends
|
||||||
from sqlalchemy.orm import Session
|
from typing import Dict
|
||||||
from core.database import get_db
|
from helpers.fruit_helpers import update_fruit
|
||||||
from schemas.fruit import FruitSchema, FruitUpdate
|
from schemas.fruit import FruitSchema
|
||||||
from helpers.fruit_helpers import get_fruit_by_id, update_fruit
|
from db import get_db, Session
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.put("/fruits", status_code=200, response_model=FruitSchema)
|
@router.put("/fruits", response_model=Dict[str, FruitSchema])
|
||||||
async def update_fruit_endpoint(
|
async def update_fruit_endpoint(
|
||||||
fruit_id: str,
|
fruit_update: FruitSchema,
|
||||||
fruit_data: FruitUpdate,
|
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
fruit = get_fruit_by_id(db, fruit_id)
|
updated_fruit = update_fruit(db=db, fruit_update=fruit_update)
|
||||||
if not fruit:
|
return {"data": updated_fruit}
|
||||||
raise HTTPException(status_code=404, detail="Fruit not found")
|
|
||||||
|
|
||||||
updated_fruit = update_fruit(db, fruit_id, fruit_data)
|
|
||||||
return updated_fruit
|
|
@ -10,3 +10,4 @@ alembic>=1.13.1
|
|||||||
jose
|
jose
|
||||||
passlib
|
passlib
|
||||||
pydantic
|
pydantic
|
||||||
|
db
|
||||||
|
@ -16,6 +16,9 @@ class FruitUpdate(FruitBase):
|
|||||||
description: Optional[str] = Field(None, description="Description of the fruit")
|
description: Optional[str] = Field(None, description="Description of the fruit")
|
||||||
price: Optional[int] = Field(None, description="Price of the fruit")
|
price: Optional[int] = Field(None, description="Price of the fruit")
|
||||||
|
|
||||||
|
class FruitResponse(BaseModel):
|
||||||
|
data: FruitBase
|
||||||
|
|
||||||
class FruitSchema(FruitBase):
|
class FruitSchema(FruitBase):
|
||||||
id: uuid.UUID
|
id: uuid.UUID
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
|
Loading…
x
Reference in New Issue
Block a user