Skip to content

Commit 4fca169

Browse files
committed
Return prompts list and add delete endpoint
Refactor the /prompts root handler to return a warning meta when the prompts table is missing and, when present, fetch all records from the prompts table (id, prompt, response, duration_ms, llm, timestamp) using get_db_connection_direct(), returning structured data. Remove the previous placeholder/`kata` logic and adjust the meta message. Add a DELETE /prompts/{id} endpoint that deletes a prompt by id, handles DB errors with psycopg2/HTTPException, commits/rolls back as needed, and returns the deleted_id or a 404 if not found. Also import HTTPException at the top and ensure DB connections are closed in finally blocks.
1 parent 70590ed commit 4fca169

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

app/api/prompts/prompts.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
from fastapi import HTTPException
22
from app import __version__
33

44
import os
@@ -34,20 +34,23 @@ def root() -> dict:
3434
conn.close()
3535
if not exists:
3636
meta = make_meta("warning", "Table 'prompts' does not exist.")
37-
# Do not include 'kata' key or any other keys in data
38-
response = {"meta": meta}
37+
return {"meta": meta}
3938
else:
40-
meta = make_meta("success", "Prompts endpoint")
41-
# Example: include 'kata' key only if table exists (add as needed)
42-
data = [
43-
{"init": f"{base_url}/prompts", "kata": "example"},
44-
]
45-
response = {"meta": meta, "data": data}
46-
# Remove 'kata' key from all items in data if table does not exist
47-
if not exists:
48-
for item in data:
49-
item.pop('kata', None)
50-
return response
39+
meta = make_meta("success", "Prompts")
40+
# Fetch all records from prompts table
41+
data = []
42+
conn = get_db_connection_direct()
43+
try:
44+
with conn.cursor() as cur:
45+
cur.execute("SELECT id, prompt, response, duration_ms, llm, timestamp FROM prompts ORDER BY id DESC;")
46+
rows = cur.fetchall()
47+
keys = ["id", "prompt", "response", "duration_ms", "llm", "timestamp"]
48+
for row in rows:
49+
data.append(dict(zip(keys, row)))
50+
finally:
51+
conn.close()
52+
return {"meta": meta, "data": data}
53+
5154

5255

5356
# POST /prompts endpoint
@@ -84,3 +87,25 @@ def create_prompt(prompt_in: PromptCreate = Body(...)):
8487
keys = ["id", "prompt", "response", "duration_ms", "llm", "timestamp"]
8588
return dict(zip(keys, row))
8689
return {"error": "Failed to insert prompt."}
90+
91+
92+
# DELETE /prompts/{id} endpoint
93+
@router.delete("/prompts/{id}", status_code=status.HTTP_200_OK)
94+
def delete_prompt(id: int):
95+
"""Delete a prompt record by id from the prompts table."""
96+
from app.utils.db import get_db_connection_direct
97+
import psycopg2
98+
conn = get_db_connection_direct()
99+
try:
100+
with conn.cursor() as cur:
101+
cur.execute("DELETE FROM prompts WHERE id = %s RETURNING id;", (id,))
102+
row = cur.fetchone()
103+
conn.commit()
104+
except psycopg2.Error as e:
105+
conn.rollback()
106+
raise HTTPException(status_code=500, detail=str(e))
107+
finally:
108+
conn.close()
109+
if row:
110+
return {"success": True, "deleted_id": row[0]}
111+
raise HTTPException(status_code=404, detail=f"Prompt with id {id} not found.")

0 commit comments

Comments
 (0)