claude-code-cchui/src/app/api/conversations/all/route.ts
gaoziman 199772a95d feat(api): 添加对话数据导出和清除 API
- 新增 GET /api/conversations/export 导出所有对话数据
- 新增 GET /api/conversations/all 获取对话统计信息
- 新增 DELETE /api/conversations/all 清除所有对话和消息
2025-12-19 15:57:12 +08:00

53 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server';
import { db } from '@/drizzle/db';
import { conversations, messages } from '@/drizzle/schema';
import { sql } from 'drizzle-orm';
// GET /api/conversations/all - 获取统计信息
export async function GET() {
try {
// 获取对话数量
const conversationCount = await db
.select({ count: sql<number>`count(*)` })
.from(conversations);
// 获取消息数量
const messageCount = await db
.select({ count: sql<number>`count(*)` })
.from(messages);
return NextResponse.json({
conversationCount: Number(conversationCount[0]?.count || 0),
messageCount: Number(messageCount[0]?.count || 0),
});
} catch (error) {
console.error('Failed to get stats:', error);
return NextResponse.json(
{ error: 'Failed to get stats' },
{ status: 500 }
);
}
}
// DELETE /api/conversations/all - 清除所有对话和消息
export async function DELETE() {
try {
// 先删除所有消息
await db.delete(messages);
// 再删除所有对话
await db.delete(conversations);
return NextResponse.json({
success: true,
message: 'All conversations and messages have been deleted',
});
} catch (error) {
console.error('Failed to delete all conversations:', error);
return NextResponse.json(
{ error: 'Failed to delete all conversations' },
{ status: 500 }
);
}
}