74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
// =====================================================
|
|
// 資料庫同步管理 API
|
|
// =====================================================
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { dbSync } from '@/lib/database-sync';
|
|
|
|
// 獲取同步狀態
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const status = await dbSync.getSyncStatus();
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '同步狀態獲取成功',
|
|
data: status
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('獲取同步狀態失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '獲取同步狀態失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// 執行同步操作
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const { action, tableName, condition } = body;
|
|
|
|
switch (action) {
|
|
case 'sync_table':
|
|
if (!tableName) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '缺少表名參數'
|
|
}, { status: 400 });
|
|
}
|
|
|
|
const syncResult = await dbSync.syncFromMasterToSlave(tableName, condition);
|
|
|
|
if (syncResult) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: `成功同步表 ${tableName} 到備機`
|
|
});
|
|
} else {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: `同步表 ${tableName} 失敗`
|
|
}, { status: 500 });
|
|
}
|
|
|
|
default:
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '無效的操作'
|
|
}, { status: 400 });
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('執行同步操作失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '執行同步操作失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|