File size: 5,259 Bytes
b273c57 | 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 | interface Env {
DB: D1Database;
}
export const onRequest = async (context: { request: Request; env: Env }) => {
const { request, env } = context;
const url = new URL(request.url);
const path = url.pathname;
if (request.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
});
}
try {
if (path === "/api/db/professionals") {
const type = url.searchParams.get("type");
if (request.method === "GET") {
let query = "SELECT * FROM professionals";
const params: any[] = [];
if (type) {
query += " WHERE type = ?";
params.push(type);
}
const { results } = await env.DB.prepare(query).bind(...params).all();
return new Response(JSON.stringify(results), {
headers: { "Content-Type": "application/json" },
});
}
if (request.method === "POST") {
const { data, type: bodyType } = await request.json();
const professionalsArray = Array.isArray(data) ? data : [data];
const statements = professionalsArray.map((p: any) =>
env.DB.prepare(
"INSERT INTO professionals (name, specialty, address, contact, website, summary, type) VALUES (?, ?, ?, ?, ?, ?, ?)"
).bind(
p.name,
p.specialty,
p.address,
p.contact,
p.website,
p.summary,
bodyType || type || 'lawyer'
)
);
await env.DB.batch(statements);
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
if (request.method === "DELETE") {
if (type) {
await env.DB.prepare("DELETE FROM professionals WHERE type = ?").bind(type).run();
} else {
await env.DB.prepare("DELETE FROM professionals").run();
}
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
}
if (path === "/api/db/chats") {
if (request.method === "GET") {
const { results } = await env.DB.prepare("SELECT * FROM chat_sessions ORDER BY timestamp DESC").all();
const chats = results.map((r: any) => ({
...r,
messages: JSON.parse(r.messages)
}));
return new Response(JSON.stringify(chats), {
headers: { "Content-Type": "application/json" },
});
}
if (request.method === "POST") {
const chat = await request.json();
await env.DB.prepare(
"INSERT OR REPLACE INTO chat_sessions (id, title, docType, timestamp, messages) VALUES (?, ?, ?, ?, ?)"
).bind(
chat.id,
chat.title,
chat.docType,
chat.timestamp,
JSON.stringify(chat.messages)
).run();
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
if (request.method === "DELETE") {
const id = url.searchParams.get("id");
if (id) {
await env.DB.prepare("DELETE FROM chat_sessions WHERE id = ?").bind(id).run();
} else {
await env.DB.prepare("DELETE FROM chat_sessions").run();
}
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
}
if (path === "/api/db/checkpoints") {
if (request.method === "GET") {
const { results } = await env.DB.prepare("SELECT * FROM checkpoints ORDER BY timestamp DESC").all();
const checkpoints = results.map((r: any) => ({
...r,
state: JSON.parse(r.state)
}));
return new Response(JSON.stringify(checkpoints), {
headers: { "Content-Type": "application/json" },
});
}
if (request.method === "POST") {
const ckpt = await request.json();
await env.DB.prepare(
"INSERT OR REPLACE INTO checkpoints (id, name, timestamp, state) VALUES (?, ?, ?, ?)"
).bind(
ckpt.id,
ckpt.name,
ckpt.timestamp,
JSON.stringify(ckpt.state)
).run();
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
if (request.method === "DELETE") {
const id = url.searchParams.get("id");
if (id) {
await env.DB.prepare("DELETE FROM checkpoints WHERE id = ?").bind(id).run();
} else {
await env.DB.prepare("DELETE FROM checkpoints").run();
}
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
}
return new Response("Not Found", { status: 404 });
} catch (error: any) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
};
|