Files
FlorteSite/src/app/api/layers/[id]/route.ts
T
faumaray 75e03b42f7 init
Signed-off-by: faumaray <faumaray@gmail.com>
2026-05-04 08:30:15 +03:00

66 lines
1.8 KiB
TypeScript

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);
}
}