46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
||
import { requireAuth } from '@/lib/auth'
|
||
import { readConfig } from '@/lib/config'
|
||
import { testDb, getDbStats, getTables, runQuery } from '@/lib/db'
|
||
|
||
export async function GET(req: NextRequest) {
|
||
const err = await requireAuth(req)
|
||
if (err) return err
|
||
|
||
const dbId = req.nextUrl.searchParams.get('dbId')
|
||
const action = req.nextUrl.searchParams.get('action') ?? 'stats'
|
||
|
||
if (!dbId) return NextResponse.json({ error: 'dbId gerekli' }, { status: 400 })
|
||
|
||
const config = await readConfig()
|
||
const db = config.databases.find(d => d.id === dbId)
|
||
if (!db) return NextResponse.json({ error: 'DB bulunamadı' }, { status: 404 })
|
||
|
||
if (action === 'test') return NextResponse.json(await testDb(db))
|
||
try {
|
||
if (action === 'stats') return NextResponse.json(await getDbStats(db))
|
||
if (action === 'tables') return NextResponse.json(await getTables(db))
|
||
} catch (e: unknown) {
|
||
return NextResponse.json({ error: e instanceof Error ? e.message : 'Bağlantı hatası' }, { status: 400 })
|
||
}
|
||
|
||
return NextResponse.json({ error: 'Geçersiz action' }, { status: 400 })
|
||
}
|
||
|
||
export async function POST(req: NextRequest) {
|
||
const err = await requireAuth(req)
|
||
if (err) return err
|
||
|
||
const body = await req.json()
|
||
if (body.action === 'test') {
|
||
return NextResponse.json(await testDb(body.db))
|
||
}
|
||
|
||
const { dbId, sql } = body
|
||
const config = await readConfig()
|
||
const db = config.databases.find(d => d.id === dbId)
|
||
if (!db) return NextResponse.json({ error: 'DB bulunamadı' }, { status: 404 })
|
||
|
||
return NextResponse.json(await runQuery(db, sql))
|
||
}
|