Admin portal, public pages, footer, site config
This commit is contained in:
@@ -2,27 +2,45 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const key = request.nextUrl.searchParams.get('key');
|
||||
if (!key) {
|
||||
const { rows } = await db.query(
|
||||
`SELECT key, value FROM config WHERE key IN ('logo','facebook_url','instagram_url','whatsapp_url','address','phone','email','wifi_password')`
|
||||
);
|
||||
const config: Record<string, string> = {};
|
||||
rows.forEach((r: any) => config[r.key] = r.value);
|
||||
return NextResponse.json({ data: config });
|
||||
}
|
||||
const PUBLIC_KEYS = ['logo','favicon','facebook_url','instagram_url','whatsapp_url','address','phone','email','wifi_password'];
|
||||
|
||||
const { rows } = await db.query('SELECT value FROM config WHERE key = $1', [key]);
|
||||
return NextResponse.json({ data: rows[0]?.value || null });
|
||||
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();
|
||||
const { key, value } = body;
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user