feat: 支持文章标签的后端增删改查

This commit is contained in:
seahi committed 2026-09-24 11:09:56 +08:00
1 parent 609c6e18e2
commit 0f6df281c8
2 files changed
+55 -4

No files matched your search

+21 -1
View File
@@ -45,11 +45,12 @@ export const POST: RequestHandler = async ({ request, platform }) => {
try {
const data = (await request.json()) as Record<string, unknown>;
const { slug, title, content, category_slug } = data as {
const { slug, title, content, category_slug, tags } = data as {
slug?: string;
title?: string;
content?: string;
category_slug?: string;
tags?: string[];
};
if (!slug || !title || !content) {
@@ -79,6 +80,25 @@ export const POST: RequestHandler = async ({ request, platform }) => {
.run();
if (result.success) {
const post = await db
.prepare('SELECT id FROM posts WHERE slug = ?')
.bind(slug)
.first<{ id: number }>();
if (post && tags && tags.length > 0) {
for (const tagName of tags) {
await db.prepare('INSERT OR IGNORE INTO tags (name) VALUES (?)').bind(tagName).run();
const tag = await db
.prepare('SELECT id FROM tags WHERE name = ?')
.bind(tagName)
.first<{ id: number }>();
if (tag) {
await db
.prepare('INSERT INTO post_tags (post_id, tag_id) VALUES (?, ?)')
.bind(post.id, tag.id)
.run();
}
}
}
return json({ success: true, slug, title });
} else {
return json({ error: 'Failed to insert post' }, { status: 500 });
+34 -3
View File
@@ -18,12 +18,26 @@ export const GET: RequestHandler = async ({ params, platform }) => {
`
)
.bind(slug)
.first();
.first<Record<string, unknown>>();
if (!post) {
return json({ error: 'Post not found' }, { status: 404 });
}
const { results: tagsResults } = await db
.prepare(
`
SELECT t.name
FROM tags t
JOIN post_tags pt ON t.id = pt.tag_id
WHERE pt.post_id = ?
`
)
.bind(post.id)
.all();
post.tags = tagsResults.map((t: Record<string, unknown>) => t.name);
return json({ data: post });
} catch (err: unknown) {
console.error('Failed to get post', err);
@@ -39,13 +53,14 @@ export const PUT: RequestHandler = async ({ params, request, platform }) => {
try {
const data = (await request.json()) as Record<string, unknown>;
const { title, content, category_slug } = data as {
const { title, content, category_slug, tags } = data as {
title?: string;
content?: string;
category_slug?: string;
tags?: string[];
};
if (!title && !content && !category_slug) {
if (!title && !content && category_slug === undefined && tags === undefined) {
return json({ error: 'No fields to update' }, { status: 400 });
}
@@ -93,6 +108,22 @@ export const PUT: RequestHandler = async ({ params, request, platform }) => {
.run();
if (result.success) {
if (tags !== undefined) {
await db.prepare('DELETE FROM post_tags WHERE post_id = ?').bind(existingPost.id).run();
for (const tagName of tags) {
await db.prepare('INSERT OR IGNORE INTO tags (name) VALUES (?)').bind(tagName).run();
const tag = await db
.prepare('SELECT id FROM tags WHERE name = ?')
.bind(tagName)
.first<{ id: number }>();
if (tag) {
await db
.prepare('INSERT INTO post_tags (post_id, tag_id) VALUES (?, ?)')
.bind(existingPost.id, tag.id)
.run();
}
}
}
return json({ success: true, slug });
} else {
return json({ error: 'Failed to update post' }, { status: 500 });