feat(api): admin guests CRUD endpoints

This commit is contained in:
belisards
2026-05-03 16:21:26 -03:00
parent 7b2e2cc3c5
commit 32f9403bd8
2 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
import { NextRequest, NextResponse } from 'next/server';
import { eq } from 'drizzle-orm';
import { db, guests } from '@/lib/db';
import { verifyAdminToken } from '@/lib/auth/tokens';
export async function PATCH(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
if (!verifyAdminToken(request)) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = await params;
const body = await request.json().catch(() => ({}));
const name = (body?.name ?? '').toString().trim();
if (!name) {
return NextResponse.json({ error: 'name is required' }, { status: 400 });
}
const [row] = await db
.update(guests)
.set({ name, updatedAt: new Date() })
.where(eq(guests.id, id))
.returning();
if (!row) return NextResponse.json({ error: 'Not found' }, { status: 404 });
return NextResponse.json({ success: true, guest: row });
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
if (!verifyAdminToken(request)) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = await params;
const result = await db.delete(guests).where(eq(guests.id, id)).returning();
if (result.length === 0) return NextResponse.json({ error: 'Not found' }, { status: 404 });
return NextResponse.json({ success: true });
}