Skip to content

Commit 6e62128

Browse files
committed
Connected DB
1 parent 7d98eaf commit 6e62128

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

app/api/routes.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
from fastapi import APIRouter
44
from pydantic import BaseModel
5+
import os
6+
from dotenv import load_dotenv
7+
import psycopg2
8+
router = APIRouter()
9+
510

611
router = APIRouter()
712

@@ -25,16 +30,39 @@ class EchoResponse(BaseModel):
2530

2631
@router.get("/")
2732
def root() -> dict:
28-
"""Return a structured welcome message for the API root."""
33+
"""Return a structured welcome message for the API root, including product data."""
34+
load_dotenv()
35+
conn = psycopg2.connect(
36+
host=os.getenv('DB_HOST'),
37+
port=os.getenv('DB_PORT', '5432'),
38+
dbname=os.getenv('DB_NAME'),
39+
user=os.getenv('DB_USER'),
40+
password=os.getenv('DB_PASSWORD')
41+
)
42+
cur = conn.cursor()
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()
2957
epoch = int(time.time() * 1000)
3058
meta = {
3159
"version": __version__,
3260
"time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
3361
"epoch": epoch,
3462
"severity": "success",
35-
"message": "NX AI says hello.",
63+
"message": f"NX AI says hello. Returned {len(products)} products."
3664
}
37-
return {"meta": meta}
65+
return {"meta": meta, "data": products}
3866

3967

4068
@router.get("/health")

0 commit comments

Comments
 (0)