Skip to content

Commit 6d6d7cf

Browse files
committed
Add queue delete endpoint and improve APIs
Add a new DELETE /queue/delete route to remove queue records by id and register it in the queue router. Change the GET queue summary to return a random example record (ORDER BY RANDOM() LIMIT 1) instead of the most recently updated records. Update the LinkedIn import to point to linkedin.csv instead of the sample file. Add a global RequestValidationError handler in app.main that returns errors using the existing make_meta format, and add required imports for that handler.
1 parent b5ee4da commit 6d6d7cf

5 files changed

Lines changed: 42 additions & 6 deletions

File tree

app/api/queue/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .routes.get import router as get_router
88

99
from .routes.create import router as create_router
10+
from .routes.delete import router as delete_router
1011

1112
from .routes.import_linkedin import router as import_linkedin_router
1213

@@ -18,6 +19,7 @@
1819
router.include_router(empty_router)
1920
router.include_router(get_router)
2021
router.include_router(create_router)
22+
router.include_router(delete_router)
2123
router.include_router(import_linkedin_router)
2224
router.include_router(alter_router)
2325
router.include_router(rename_router)

app/api/queue/routes/delete.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
from fastapi import APIRouter, HTTPException
3+
from app.utils.make_meta import make_meta
4+
from app.utils.db import get_db_connection_direct
5+
6+
router = APIRouter()
7+
8+
@router.delete("/queue/delete")
9+
def delete_queue_record(id: int) -> dict:
10+
"""DELETE /queue/delete: Delete a record from the queue table by id."""
11+
try:
12+
conn = get_db_connection_direct()
13+
cursor = conn.cursor()
14+
cursor.execute("DELETE FROM queue WHERE id = %s RETURNING id;", (id,))
15+
deleted = cursor.fetchone()
16+
conn.commit()
17+
conn.close()
18+
if deleted:
19+
return {"meta": make_meta("success", f"Record with id {id} deleted.")}
20+
else:
21+
return {"meta": make_meta("error", f"No record found with id {id}.")}
22+
except Exception as e:
23+
msg = str(e)
24+
return {"meta": make_meta("error", msg)}

app/api/queue/routes/get.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def read_queue() -> dict:
2727
for row in cursor.fetchall()
2828
]
2929

30-
# 3. Get the 10 most recently updated records
31-
cursor.execute("SELECT * FROM queue ORDER BY updated DESC LIMIT 10;")
30+
# 3. Get a random record
31+
cursor.execute("SELECT * FROM queue ORDER BY RANDOM() LIMIT 1;")
3232
columns = [desc[0] for desc in cursor.description] if cursor.description else []
3333
rows = cursor.fetchall()
34-
most_recent = [dict(zip(columns, row)) for row in rows] if rows and columns else []
34+
random_record = [dict(zip(columns, row)) for row in rows] if rows and columns else []
3535

3636
# 4. Get unique values from collection and group columns
3737
cursor.execute("SELECT DISTINCT collection FROM queue WHERE collection IS NOT NULL;")
@@ -47,7 +47,7 @@ def read_queue() -> dict:
4747
"in_queue": record_count,
4848
"collections": collections,
4949
"groups": groups,
50-
"example": most_recent[:1],
50+
"example": random_record,
5151
# "queue_schema": schema
5252
}
5353
}

app/api/queue/routes/import_linkedin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@router.post("/queue/import/linkedin")
1111
def import_linkedin_csv() -> dict:
1212
"""POST /queue/import/linkedin: Import data from linkedin.csv into the queue table, robust for large files."""
13-
csv_path = os.path.join(os.path.dirname(__file__), "../csv/linkedin/linkedin_sample.csv")
13+
csv_path = os.path.join(os.path.dirname(__file__), "../csv/linkedin/linkedin.csv")
1414
if not os.path.exists(csv_path):
1515
raise HTTPException(status_code=404, detail="linkedin.csv not found")
1616
try:

app/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from app import __version__
2-
from fastapi import FastAPI
2+
from fastapi import FastAPI, Request
3+
from fastapi.responses import JSONResponse
4+
from fastapi.exceptions import RequestValidationError
5+
from app.utils.make_meta import make_meta
36
from fastapi.middleware.cors import CORSMiddleware
47
from fastapi.staticfiles import StaticFiles
58
from fastapi.responses import FileResponse
@@ -26,6 +29,13 @@
2629
allow_headers=["*"]
2730
)
2831

32+
33+
# Global validation error handler for make_meta pattern
34+
@app.exception_handler(RequestValidationError)
35+
async def validation_exception_handler(request: Request, exc: RequestValidationError):
36+
msg = exc.errors()[0]['msg'] if exc.errors() else str(exc)
37+
return JSONResponse(status_code=422, content={"meta": make_meta("error", msg)})
38+
2939
app.include_router(router)
3040

3141
# Mount static directory

0 commit comments

Comments
 (0)