|
| 1 | +import os |
| 2 | +import psycopg2 |
| 3 | +from dotenv import load_dotenv |
| 4 | +from fastapi import APIRouter, status |
| 5 | + |
| 6 | +router = APIRouter() |
| 7 | + |
| 8 | +@router.post("/products/reset", status_code=status.HTTP_200_OK) |
| 9 | +def reset_products() -> dict: |
| 10 | + """Delete and recreate the product table, then seed with initial data.""" |
| 11 | + load_dotenv() |
| 12 | + conn = psycopg2.connect( |
| 13 | + host=os.getenv('DB_HOST'), |
| 14 | + port=os.getenv('DB_PORT', '5432'), |
| 15 | + dbname=os.getenv('DB_NAME'), |
| 16 | + user=os.getenv('DB_USER'), |
| 17 | + password=os.getenv('DB_PASSWORD') |
| 18 | + ) |
| 19 | + cur = conn.cursor() |
| 20 | + # Drop and recreate table |
| 21 | + cur.execute(''' |
| 22 | + DROP TABLE IF EXISTS product; |
| 23 | + CREATE TABLE product ( |
| 24 | + id SERIAL PRIMARY KEY, |
| 25 | + name VARCHAR(255) NOT NULL, |
| 26 | + description TEXT, |
| 27 | + price NUMERIC(10,2) NOT NULL, |
| 28 | + in_stock BOOLEAN DEFAULT TRUE, |
| 29 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 30 | + ); |
| 31 | + ''') |
| 32 | + # Seed data |
| 33 | + seed_products = [ |
| 34 | + ("Apple", "Fresh red apple", 0.99, True), |
| 35 | + ("Banana", "Organic banana", 0.59, True), |
| 36 | + ("Orange", "Juicy orange", 1.29, True), |
| 37 | + ] |
| 38 | + cur.executemany( |
| 39 | + "INSERT INTO product (name, description, price, in_stock) VALUES (%s, %s, %s, %s);", |
| 40 | + seed_products |
| 41 | + ) |
| 42 | + conn.commit() |
| 43 | + cur.execute('SELECT id, name, description, price, in_stock, created_at FROM product;') |
| 44 | + products = [ |
| 45 | + { |
| 46 | + "id": row[0], |
| 47 | + "name": row[1], |
| 48 | + "description": row[2], |
| 49 | + "price": float(row[3]), |
| 50 | + "in_stock": row[4], |
| 51 | + "created_at": row[5].isoformat() if row[5] else None |
| 52 | + } |
| 53 | + for row in cur.fetchall() |
| 54 | + ] |
| 55 | + cur.close() |
| 56 | + conn.close() |
| 57 | + return {"message": "Product table reset and seeded.", "data": products} |
| 58 | +import os, time |
| 59 | +import psycopg2 |
| 60 | +from dotenv import load_dotenv |
| 61 | +from app import __version__ |
| 62 | + |
| 63 | +router = APIRouter() |
| 64 | + |
| 65 | +@router.get("/products") |
| 66 | +def root() -> dict: |
| 67 | + """Return a structured welcome message for the API root, including product data.""" |
| 68 | + load_dotenv() |
| 69 | + conn = psycopg2.connect( |
| 70 | + host=os.getenv('DB_HOST'), |
| 71 | + port=os.getenv('DB_PORT', '5432'), |
| 72 | + dbname=os.getenv('DB_NAME'), |
| 73 | + user=os.getenv('DB_USER'), |
| 74 | + password=os.getenv('DB_PASSWORD') |
| 75 | + ) |
| 76 | + cur = conn.cursor() |
| 77 | + cur.execute('SELECT id, name, description, price, in_stock, created_at FROM product;') |
| 78 | + products = [ |
| 79 | + { |
| 80 | + "id": row[0], |
| 81 | + "name": row[1], |
| 82 | + "description": row[2], |
| 83 | + "price": float(row[3]), |
| 84 | + "in_stock": row[4], |
| 85 | + "created_at": row[5].isoformat() if row[5] else None |
| 86 | + } |
| 87 | + for row in cur.fetchall() |
| 88 | + ] |
| 89 | + cur.close() |
| 90 | + conn.close() |
| 91 | + |
| 92 | + load_dotenv() |
| 93 | + base_url = os.getenv("BASE_URL", "http://localhost:8000") |
| 94 | + |
| 95 | + epoch = int(time.time() * 1000) |
| 96 | + meta = { |
| 97 | + "severity": "success", |
| 98 | + "title": "Product List", |
| 99 | + "version": __version__, |
| 100 | + "base_url": base_url, |
| 101 | + "time": epoch, |
| 102 | + } |
| 103 | + return {"meta": meta, "data": products} |
0 commit comments