Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""NX AI - FastAPI/Python/Postgres/tsvector"""

# Current Version
__version__ = "1.0.8"
__version__ = "1.0.9"
8 changes: 0 additions & 8 deletions app/api/echo.py

This file was deleted.

56 changes: 0 additions & 56 deletions app/api/import_csv.py

This file was deleted.

1 change: 1 addition & 0 deletions app/api/products/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .products import router
from .reset import router as reset_router
4 changes: 2 additions & 2 deletions app/api/products/products.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from app import __version__
from fastapi import APIRouter
from fastapi import status
import os, time
import psycopg2
from dotenv import load_dotenv
Expand Down Expand Up @@ -39,11 +40,10 @@ def root() -> dict:

epoch = int(time.time() * 1000)
meta = {
"severity": "success",
"title": "Product List",
"description": "from the products Postgres table",
"version": __version__,
"base_url": base_url,
"time": epoch,
"severity": "success",
}
return {"meta": meta, "data": products}
103 changes: 103 additions & 0 deletions app/api/products/reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import os
import psycopg2
from dotenv import load_dotenv
from fastapi import APIRouter, status

router = APIRouter()

@router.post("/products/reset", status_code=status.HTTP_200_OK)
def reset_products() -> dict:
"""Delete and recreate the product table, then seed with initial data."""
load_dotenv()
conn = psycopg2.connect(
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT', '5432'),
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD')
)
cur = conn.cursor()
# Drop and recreate table
cur.execute('''
DROP TABLE IF EXISTS product;
CREATE TABLE product (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
price NUMERIC(10,2) NOT NULL,
in_stock BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
''')
# Seed data
seed_products = [
("Apple", "Fresh red apple", 0.99, True),
("Banana", "Organic banana", 0.59, True),
("Orange", "Juicy orange", 1.29, True),
]
cur.executemany(
"INSERT INTO product (name, description, price, in_stock) VALUES (%s, %s, %s, %s);",
seed_products
)
conn.commit()
cur.execute('SELECT id, name, description, price, in_stock, created_at FROM product;')
products = [
{
"id": row[0],
"name": row[1],
"description": row[2],
"price": float(row[3]),
"in_stock": row[4],
"created_at": row[5].isoformat() if row[5] else None
}
for row in cur.fetchall()
]
cur.close()
conn.close()
return {"message": "Product table reset and seeded.", "data": products}
import os, time
import psycopg2
from dotenv import load_dotenv
from app import __version__

router = APIRouter()

@router.get("/products")
def root() -> dict:
"""Return a structured welcome message for the API root, including product data."""
load_dotenv()
conn = psycopg2.connect(
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT', '5432'),
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD')
)
cur = conn.cursor()
cur.execute('SELECT id, name, description, price, in_stock, created_at FROM product;')
products = [
{
"id": row[0],
"name": row[1],
"description": row[2],
"price": float(row[3]),
"in_stock": row[4],
"created_at": row[5].isoformat() if row[5] else None
}
for row in cur.fetchall()
]
cur.close()
conn.close()

load_dotenv()
base_url = os.getenv("BASE_URL", "http://localhost:8000")

epoch = int(time.time() * 1000)
meta = {
"severity": "success",
"title": "Product List",
"version": __version__,
"base_url": base_url,
"time": epoch,
}
return {"meta": meta, "data": products}
4 changes: 1 addition & 3 deletions app/api/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ def root() -> dict:
base_url = os.getenv("BASE_URL", "http://localhost:8000")
epoch = int(time.time() * 1000)
meta = {
"severity": "success",
"title": "NX-AI says hi",
"description": "This is the base_url",
"version": __version__,
"base_url": base_url,
"time": epoch,
"severity": "success",
"message": "Welcome to NX AI!"
}
endpoints = [
{"name": "docs", "url": f"{base_url}/docs"},
Expand Down
6 changes: 2 additions & 4 deletions app/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

from app.api.root import router as root_router
from app.api.health import router as health_router
from app.api.echo import router as echo_router
from app.api.import_csv import router as import_csv_router
from app.api.products.products import router as products_router
from app.api.products.reset import router as reset_router

router.include_router(root_router)
router.include_router(health_router)
router.include_router(echo_router)
router.include_router(import_csv_router)
router.include_router(products_router)
router.include_router(reset_router)
5 changes: 2 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from app import __version__
"""NX AI - FastAPI entry point."""

"""NX-AI Open Source, production ready Python FastAPI/Postgres app for NX"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -12,7 +11,7 @@
from app.api.routes import router

app = FastAPI(
title="NX AI",
title="NX-AI",
description="Production-ready Python FastAPI app for NX",
version=__version__,
)
Expand Down
4 changes: 1 addition & 3 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def test_root_returns_welcome_message() -> None:
json_data = response.json()
assert "meta" in json_data
assert "data" in json_data
assert "message" in json_data["meta"]
assert "NX AI" in json_data["meta"]["message"]

assert "title" in json_data["meta"]

def test_health_returns_ok() -> None:
"""GET /health should return status ok."""
Expand Down
Loading