diff --git a/README.md b/README.md index 0f7bbb2..0a4daa3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,14 @@ -# shopping-list-api +# Shopping List API -Simple internal API to track shopping list items using SQLite and FastAPI \ No newline at end of file +A simple internal API to track shopping list items using SQLite and FastAPI. + +## Setup + +```bash +pip install -r requirements.txt +uvicorn main:app --reload +``` + +## API + +Endpoints will be documented after implementation. diff --git a/main.py b/main.py new file mode 100644 index 0000000..13966f0 --- /dev/null +++ b/main.py @@ -0,0 +1,59 @@ +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"} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0a4249c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +fastapi +uvicorn +sqlalchemy +pydantic