import { prisma } from "@/lib/db"; import { getSession, requireDm } from "@/lib/session"; import { json, err, asResponse } from "@/lib/api"; export const dynamic = "force-dynamic"; type Ctx = { params: Promise<{ id: string }> }; export async function GET(_req: Request, ctx: Ctx) { try { await getSession(); const { id } = await ctx.params; const layer = await prisma.mapLayer.findUnique({ where: { id } }); if (!layer) return err("Not found", 404); return json(layer); // includes imageData } catch (e) { return asResponse(e); } } export async function PATCH(req: Request, ctx: Ctx) { try { await requireDm(); const { id } = await ctx.params; const b = (await req.json()) as Partial<{ name: string; imageData: string; width: number; height: number; isDefault: boolean; order: number; }>; if (b.imageData !== undefined && b.imageData !== "" && !/^data:image\/(png|jpeg|jpg|webp|svg\+xml);base64,/.test(b.imageData)) { return err("imageData must be a data: URL of an image"); } if (b.isDefault) { await prisma.mapLayer.updateMany({ where: { NOT: { id } }, data: { isDefault: false }, }); } const updated = await prisma.mapLayer.update({ where: { id }, data: { name: b.name, imageData: b.imageData, width: b.width, height: b.height, isDefault: b.isDefault, order: b.order, }, }); return json(updated); } catch (e) { return asResponse(e); } } export async function DELETE(_req: Request, ctx: Ctx) { try { await requireDm(); const { id } = await ctx.params; await prisma.mapLayer.delete({ where: { id } }); return json({ ok: true }); } catch (e) { return asResponse(e); } }