# app.py from fastapi import FastAPI, HTTPException from pydantic import BaseModel # <--- Import BaseModel import sqlite3 import os # Define the Pydantic model for Item creation/update class Item(BaseModel): name: str description: str | None = None # Use Union type hint for None app = FastAPI() # ... rest of your existing code ... @app.post("/items/") async def create_item(item: Item): # <--- Change parameter to 'item: Item' conn = get_db_connection() cursor = conn.cursor() try: query = "INSERT INTO items (name, description) VALUES (?, ?)" cursor.execute(query, (item.name, item.description)) # Access attributes using item.name, item.description conn.commit() return {"message": "Item created successfully", "id": cursor.lastrowid} except Exception as e: conn.rollback() raise HTTPException(status_code=500, detail=f"Error creating item: {e}") finally: cursor.close() conn.close() # ... Also update your @app.put("/items/{item_id}") endpoint similarly ... @app.put("/items/{item_id}") async def update_item(item_id: int, item: Item): # <--- Change parameter to 'item: Item' conn = get_db_connection() cursor = conn.cursor() try: updates = [] params = [] if item.name is not None: # Access using item.name updates.append("name = ?") params.append(item.name) if item.description is not None: # Access using item.description updates.append("description = ?") params.append(item.description) if not updates: raise HTTPException(status_code=400, detail="No fields to update provided") query = f"UPDATE items SET {', '.join(updates)} WHERE id = ?" params.append(item_id) cursor.execute(query, tuple(params)) conn.commit() if cursor.rowcount == 0: raise HTTPException(status_code=404, detail="Item not found") return {"message": "Item updated successfully"} except HTTPException: # Re-raise if it's already an HTTPException raise except Exception as e: conn.rollback() raise HTTPException(status_code=500, detail=f"Error updating item: {e}") finally: cursor.close() conn.close() # ... rest of your existing code ...