diff options
Diffstat (limited to 'routers/grocery.py')
| -rw-r--r-- | routers/grocery.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/routers/grocery.py b/routers/grocery.py index 17215d2..cbc66bb 100644 --- a/routers/grocery.py +++ b/routers/grocery.py @@ -130,6 +130,43 @@ async def delete_current_grocery_list(db: Session = Depends(get_db)): return {"status": "deleted"} +@router.patch("/{id}/check-item") +async def check_grocery_item(id: int, request: dict = Body(...), db: Session = Depends(get_db)): + """Mark a grocery item as checked/unchecked.""" + 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") + checked = request.get("checked", False) + + if not item_name: + raise HTTPException(status_code=400, detail="Item name is required") + + # Parse items + try: + items = json.loads(grocery_list.items) + except json.JSONDecodeError: + items = [] + + # Find and update the item + item_found = False + for item in items: + if isinstance(item, dict) and item.get("name") == item_name: + item["checked"] = checked + item_found = True + break + + if not item_found: + raise HTTPException(status_code=404, detail="Item not found in grocery list") + + # Save back to database + grocery_list.items = json.dumps(items) + db.commit() + + return {"status": "ok"} + + @router.put("/{id}/purchased") async def mark_purchased(id: int, db: Session = Depends(get_db)): """Mark a grocery list as purchased and update pantry.""" |
