59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { requireRole } from '@/lib/auth';
|
|
import { db } from '@/lib/db';
|
|
|
|
const PUBLIC_KEYS = ['logo','favicon','tagline','facebook_url','instagram_url','whatsapp_url','address','phone','email'];
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const key = request.nextUrl.searchParams.get('key');
|
|
if (!key) {
|
|
const { rows } = await db.query(
|
|
`SELECT key, value FROM config WHERE key IN (${PUBLIC_KEYS.map((_,i) => `$${i+1}`).join(',')})`,
|
|
PUBLIC_KEYS
|
|
);
|
|
const config: Record<string, string> = {};
|
|
rows.forEach((r: any) => config[r.key] = r.value);
|
|
return NextResponse.json({ data: config });
|
|
}
|
|
|
|
const { rows } = await db.query('SELECT value FROM config WHERE key = $1', [key]);
|
|
return NextResponse.json({ data: rows[0]?.value || null });
|
|
} catch (err: any) {
|
|
return NextResponse.json({ error: err.message }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
await requireRole('admin');
|
|
const body = await request.json();
|
|
|
|
if (body.values && typeof body.values === 'object') {
|
|
for (const [key, value] of Object.entries(body.values)) {
|
|
await db.query(
|
|
`INSERT INTO config (key, value) VALUES ($1, $2)
|
|
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`,
|
|
[key, value as string]
|
|
);
|
|
}
|
|
return NextResponse.json({ ok: true });
|
|
}
|
|
|
|
const { key, value } = body;
|
|
if (!key || typeof value !== 'string') {
|
|
return NextResponse.json({ error: 'Missing key or value' }, { status: 400 });
|
|
}
|
|
|
|
await db.query(
|
|
`INSERT INTO config (key, value) VALUES ($1, $2)
|
|
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`,
|
|
[key, value]
|
|
);
|
|
|
|
return NextResponse.json({ ok: true });
|
|
} catch (err: any) {
|
|
return NextResponse.json({ error: err.message || 'Failed to save asset' }, { status: 401 });
|
|
}
|
|
}
|