Skip to content

Commit b8ce5e2

Browse files
committed
Add /products/update endpoint and routing
Introduce a new endpoint at GET /products/update (202 Accepted) implemented in app/api/products/update.py that processes data/big_data.csv and updates the product table (matching item or EAN) using get_db_connection. The handler returns a meta payload with version, base_url and timestamp. Wire the new router into app/api/routes.py and add a link to the new update route in the root API listing (app/api/root.py).
1 parent b115b52 commit b8ce5e2

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

app/api/products/update.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from fastapi import APIRouter, status
2+
import os, csv, time
3+
from app.api.db import get_db_connection
4+
from app import __version__
5+
6+
router = APIRouter()
7+
8+
@router.get("/products/update", status_code=status.HTTP_202_ACCEPTED)
9+
def update_products() -> dict:
10+
"""Process the large big_data.csv file to update products."""
11+
csv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/big_data.csv'))
12+
process_csv(csv_path)
13+
base_url = os.getenv("BASE_URL", "http://localhost:8000")
14+
epoch = int(time.time() * 1000)
15+
meta = {
16+
"severity": "info",
17+
"title": "Product update from big_data.csv started",
18+
"version": __version__,
19+
"base_url": base_url,
20+
"time": epoch,
21+
}
22+
return {"meta": meta, "data": {"filename": "big_data.csv"}}
23+
24+
def process_csv(csv_path: str):
25+
conn_gen = get_db_connection()
26+
conn = next(conn_gen)
27+
cur = conn.cursor()
28+
with open(csv_path, newline='') as csvfile:
29+
reader = csv.DictReader(csvfile)
30+
for row in reader:
31+
cur.execute(
32+
"""
33+
UPDATE product SET
34+
Params=%(Params)s,
35+
title=%(desc)s,
36+
UOS=%(UOS)s,
37+
Pack_Description=%(Pack_Description)s,
38+
Hierarchy1=%(Hierarchy1)s,
39+
Hierarchy2=%(Hierarchy2)s,
40+
Hierarchy3=%(Hierarchy3)s,
41+
UOP=%(UOP)s,
42+
sSell1=%(sSell1)s,
43+
sSell2=%(sSell2)s,
44+
sSell3=%(sSell3)s,
45+
sSell4=%(sSell4)s,
46+
sSell5=%(sSell5)s,
47+
pack1=%(pack1)s,
48+
pack2=%(pack2)s,
49+
pack3=%(pack3)s,
50+
pack4=%(pack4)s,
51+
pack5=%(pack5)s,
52+
EAN=%(EAN)s
53+
WHERE item=%(item)s OR EAN=%(EAN)s
54+
""",
55+
row
56+
)
57+
conn.commit()
58+
cur.close()
59+
conn.close()

app/api/root.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def root() -> dict:
2626
"name": "products",
2727
"url": f"{base_url}/products",
2828
"children": [
29-
{"name": "seed", "url": f"{base_url}/products/seed"}
29+
{"name": "seed", "url": f"{base_url}/products/seed"},
30+
{"name": "update", "url": f"{base_url}/products/update"}
3031
]
3132
}
3233
]

app/api/routes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
from app.api.health import router as health_router
1515
from app.api.products.products import router as products_router
1616
from app.api.products.seed import router as seed_router
17+
from app.api.products.update import router as update_router
1718

1819
router.include_router(root_router)
1920
router.include_router(health_router)
2021
router.include_router(products_router)
2122
router.include_router(seed_router)
23+
router.include_router(update_router)

0 commit comments

Comments
 (0)