From 75ce40635736260ce5a19b7a33856305ee516ccc Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Sat, 9 May 2026 01:19:08 -0700 Subject: Simplify pantry UX and persist grocery check state server-side - Remove quantity, unit, expiry, and category fields from pantry form; auto-detect category via keyword map - Persist grocery item checked/unchecked state in DB via PATCH /api/grocery/{id}/check-item - Fix unchecking: previously bailed early and never removed the checked class - Fix NOT NULL constraint on ingredients.unit by defaulting to empty string Co-Authored-By: Claude Sonnet 4.6 --- routers/grocery.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'routers') 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.""" -- cgit v1.3-2-g0d8e