summaryrefslogtreecommitdiff
path: root/routers/grocery.py
diff options
context:
space:
mode:
Diffstat (limited to 'routers/grocery.py')
-rw-r--r--routers/grocery.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/routers/grocery.py b/routers/grocery.py
index cbc66bb..a74776d 100644
--- a/routers/grocery.py
+++ b/routers/grocery.py
@@ -211,6 +211,28 @@ async def mark_purchased(id: int, db: Session = Depends(get_db)):
return GroceryListRead.from_orm(grocery_list)
+@router.delete("/{id}/items")
+async def delete_grocery_item(id: int, request: dict = Body(...), db: Session = Depends(get_db)):
+ """Remove a single item from a grocery list by name."""
+ grocery_list = db.query(GroceryList).filter(GroceryList.id == id).first()
+ if not grocery_list:
+ raise HTTPException(status_code=404, detail="Grocery list not found")
+
+ item_name = request.get("name")
+ if not item_name:
+ raise HTTPException(status_code=400, detail="Item name is required")
+
+ try:
+ items = json.loads(grocery_list.items)
+ except json.JSONDecodeError:
+ items = []
+
+ items = [i for i in items if not (isinstance(i, dict) and i.get("name") == item_name)]
+ grocery_list.items = json.dumps(items)
+ db.commit()
+ return {"status": "ok"}
+
+
@ai_router.post("/suggest-recipe")
async def suggest_recipe_endpoint(db: Session = Depends(get_db)):
"""Suggest a recipe based on current pantry."""