From 8db5262f46253232d9a5ea940c13712989c8f45a Mon Sep 17 00:00:00 2001 From: "Marcus A." Date: Sun, 5 Apr 2026 23:31:25 +0000 Subject: [PATCH] Initial commit: FastAPI scaffolding, SQLite schema, and basic models\n\n- Add main.py with DB initialization and Pydantic models\n- Add requirements.txt (fastapi, uvicorn, sqlalchemy, pydantic)\n- Update README with project description and setup\n\nOperation: shopping-list-api-2026-04-05\nWI1: Project setup and database design --- README.md | 15 ++++++++++-- main.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 main.py create mode 100644 requirements.txt 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