-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
169 lines (123 loc) · 4.62 KB
/
main.py
File metadata and controls
169 lines (123 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from typing import List, Optional
from datetime import date
import sqlite3
import os
from backend.schema_models.schema_models import Prompt,PromptCreate,PromptUpdate
app = FastAPI(title="PromptHub API")
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
os.makedirs("backend/database/", exist_ok=True)
DB_PATH = os.path.join(BASE_DIR, 'backend/database/prompthub.db')
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
cursor = conn.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS PromptHub (
id INTEGER PRIMARY KEY AUTOINCREMENT,
prompt_name VARCHAR(100) NOT NULL UNIQUE,
prompt_body TEXT NOT NULL,
date DATE NOT NULL,
tag VARCHAR(50),
category VARCHAR(50) NOT NULL
);""")
conn.commit()
# Enable CORS for your frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ============ API ENDPOINTS ============
@app.get("/api/prompts", response_model=List[Prompt])
def get_prompts(
tag: Optional[str] = None,
category: Optional[str] = None,
search: Optional[str] = None
):
lis=cursor.execute("SELECT * FROM PromptHub").fetchall()
result=[]
for d in lis:
result.append ({
'id': d[0],
'title': d[1],
'body': d[2],
'date': d[3],
'favorite': d[4],
'type': d[5]
})
if tag and tag != "all":
result = [p for p in result if p["favorite"] == tag]
if category and category != "all":
result = [p for p in result if p["type"] == category]
if search:
search_lower = search.lower()
result = [
p for p in result
if search_lower in p["title"].lower() or search_lower in p["body"].lower()
]
return result
@app.post("/api/prompts", response_model=Prompt)
def create_prompt(prompt: PromptCreate):
"""Create a new prompt"""
date_str = date.today().isoformat()
cursor.execute("""
INSERT INTO PromptHub (prompt_name, prompt_body, date, tag, category)
VALUES (?, ?, ?, ?, ?)
""", (prompt.title, prompt.body, date_str, prompt.favorite, prompt.type))
conn.commit()
# Get the last inserted ID
new_id = cursor.lastrowid
# Create response with CORRECT field names matching the Prompt model
new_prompt = {
"id": new_id,
"title": prompt.title,
"body": prompt.body,
"favorite": prompt.favorite,
"type": prompt.type,
"date": date_str
}
return new_prompt
@app.delete("/api/prompts/{prompt_id}")
def delete_prompt(prompt_id: int):
"""Delete a prompt by ID"""
cursor.execute("DELETE FROM PromptHub WHERE id = ?", (prompt_id,))
conn.commit()
return {"message": "Deleted successfully"}
@app.put("/api/prompts/{prompt_id}")
def update_prompt(prompt_id: int, prompt_update: PromptUpdate):
"""Update a prompt body by ID - prints values to console"""
# print(f"UPDATE REQUEST RECEIVED:")
# print(f" Prompt ID: {prompt_id}")
# print(f" New Body: {prompt_update.body}")
cursor.execute("UPDATE PromptHub SET prompt_body = ? WHERE id = ?",(prompt_update.body, prompt_id))
conn.commit()
return {
"message": "Update request received",
"id": prompt_id,
"body": prompt_update.body
}
# ============ STATIC FILES & FRONTEND ROUTES ============
def get_file_path(filename):
return os.path.join(BASE_DIR, filename)
@app.get("/")
async def serve_index():
"""Serve the main HTML page"""
return FileResponse(get_file_path("frontend/index.html"))
@app.get("/write_prompt.html")
async def serve_write_prompt():
"""Serve the write prompt page"""
return FileResponse(get_file_path("frontend/write_prompt.html"))
@app.get("/show_prompts.html")
async def serve_show_prompts():
"""Serve the show prompts page"""
return FileResponse(get_file_path("frontend/show_prompts.html"))
@app.get("/index.html")
async def serve_index_alt():
"""Serve frontend/index.html via explicit path"""
return FileResponse(get_file_path("frontend/index.html"))
app.mount("/frontend/static", StaticFiles(directory=os.path.join(BASE_DIR, "frontend/static")), name="static")
app.mount("/frontend/scripts", StaticFiles(directory=os.path.join(BASE_DIR, "frontend/scripts")), name="scripts")
# Run with: uvicorn main:app --reload