feat: 添加分类、标签创建及文章删除的后端 API 接口
This commit is contained in:
1 parent
bb8444ebbc
commit
cf7168d786
3 files changed
+78
No files matched your search
@@ -18,3 +18,29 @@ export const GET: RequestHandler = async ({ platform }) => {
|
|||||||
return json({ error: 'Failed to retrieve categories' }, { status: 500 });
|
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 });
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -102,3 +102,32 @@ export const PUT: RequestHandler = async ({ params, request, platform }) => {
|
|||||||
return json({ error: 'Invalid request data or server error' }, { status: 500 });
|
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 });
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -16,3 +16,26 @@ export const GET: RequestHandler = async ({ platform }) => {
|
|||||||
return json({ error: 'Failed to retrieve tags' }, { status: 500 });
|
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 });
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in new issue
Block a user