From cf7168d786ceb63696999900b235ec9ffe126e65 Mon Sep 17 00:00:00 2001 From: seaHi Date: Tue, 22 Sep 2026 20:39:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E3=80=81=E6=A0=87=E7=AD=BE=E5=88=9B=E5=BB=BA=E5=8F=8A=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=88=A0=E9=99=A4=E7=9A=84=E5=90=8E=E7=AB=AF=20API=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/api/v1/categories/+server.ts | 26 ++++++++++++++++++++ src/routes/api/v1/posts/[slug]/+server.ts | 29 +++++++++++++++++++++++ src/routes/api/v1/tags/+server.ts | 23 ++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/src/routes/api/v1/categories/+server.ts b/src/routes/api/v1/categories/+server.ts index 79c0c4a..2d115a0 100644 --- a/src/routes/api/v1/categories/+server.ts +++ b/src/routes/api/v1/categories/+server.ts @@ -18,3 +18,29 @@ export const GET: RequestHandler = async ({ platform }) => { return json({ error: 'Failed to retrieve categories' }, { status: 500 }); } }; + +export const POST: RequestHandler = async ({ request, platform }) => { + const db = platform?.env.DB; + if (!db) return json({ error: 'Database not connected' }, { status: 500 }); + + try { + const data = (await request.json()) as { name?: string; slug?: string }; + if (!data.name || !data.slug) { + return json({ error: 'name and slug are required' }, { status: 400 }); + } + + const result = await db + .prepare('INSERT INTO categories (name, slug) VALUES (?, ?)') + .bind(data.name, data.slug) + .run(); + + if (result.success) { + return json({ success: true }); + } else { + return json({ error: 'Failed to create category' }, { status: 500 }); + } + } catch (err: unknown) { + console.error('Failed to create category', err); + return json({ error: 'Internal Server Error' }, { status: 500 }); + } +}; diff --git a/src/routes/api/v1/posts/[slug]/+server.ts b/src/routes/api/v1/posts/[slug]/+server.ts index 6b2cbff..e72ca5d 100644 --- a/src/routes/api/v1/posts/[slug]/+server.ts +++ b/src/routes/api/v1/posts/[slug]/+server.ts @@ -102,3 +102,32 @@ export const PUT: RequestHandler = async ({ params, request, platform }) => { return json({ error: 'Invalid request data or server error' }, { status: 500 }); } }; + +export const DELETE: RequestHandler = async ({ params, platform }) => { + const db = platform?.env.DB; + if (!db) return json({ error: 'Database not connected' }, { status: 500 }); + + const { slug } = params; + + try { + const existingPost = await db + .prepare('SELECT id FROM posts WHERE slug = ?') + .bind(slug) + .first<{ id: number }>(); + + if (!existingPost) { + return json({ error: 'Post not found' }, { status: 404 }); + } + + const result = await db.prepare('DELETE FROM posts WHERE slug = ?').bind(slug).run(); + + if (result.success) { + return json({ success: true }); + } else { + return json({ error: 'Failed to delete post' }, { status: 500 }); + } + } catch (err: unknown) { + console.error('Failed to delete post', err); + return json({ error: 'Internal Server Error' }, { status: 500 }); + } +}; diff --git a/src/routes/api/v1/tags/+server.ts b/src/routes/api/v1/tags/+server.ts index cdfe03b..04dd800 100644 --- a/src/routes/api/v1/tags/+server.ts +++ b/src/routes/api/v1/tags/+server.ts @@ -16,3 +16,26 @@ export const GET: RequestHandler = async ({ platform }) => { return json({ error: 'Failed to retrieve tags' }, { status: 500 }); } }; + +export const POST: RequestHandler = async ({ request, platform }) => { + const db = platform?.env.DB; + if (!db) return json({ error: 'Database not connected' }, { status: 500 }); + + try { + const data = (await request.json()) as { name?: string }; + if (!data.name) { + return json({ error: 'name is required' }, { status: 400 }); + } + + const result = await db.prepare('INSERT INTO tags (name) VALUES (?)').bind(data.name).run(); + + if (result.success) { + return json({ success: true }); + } else { + return json({ error: 'Failed to create tag' }, { status: 500 }); + } + } catch (err: unknown) { + console.error('Failed to create tag', err); + return json({ error: 'Internal Server Error' }, { status: 500 }); + } +};