from fastapi import FastAPI, HTTPException from pydantic import BaseModel import sqlite3 from datetime import datetime app = FastAPI() DB_PATH = "shopping.db" def init_db(): conn = sqlite3.connect(DB_PATH) c = conn.cursor() c.execute(""" CREATE TABLE IF NOT EXISTS products ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, sku TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """) c.execute(""" CREATE TABLE IF NOT EXISTS lists ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """) c.execute(""" CREATE TABLE IF NOT EXISTS list_items ( id INTEGER PRIMARY KEY AUTOINCREMENT, list_id INTEGER NOT NULL, product_id INTEGER NOT NULL, quantity INTEGER NOT NULL DEFAULT 1, added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (list_id) REFERENCES lists(id) ON DELETE CASCADE, FOREIGN KEY (product_id) REFERENCES products(id) ) """) conn.commit() conn.close() @app.on_event("startup") def startup(): init_db() class Product(BaseModel): name: str sku: str | None = None class List(BaseModel): name: str class ListItem(BaseModel): product_id: int quantity: int = 1 @app.get("/") def read_root(): return {"message": "Shopping List API"}