commit
75e03b42f7
99 files changed
+12285
No files matched your search
@@ -0,0 +1,65 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { prisma } from "@/lib/db";
|
||||
import { getSession, requireDm } from "@/lib/session";
|
||||
import { json, err, asResponse } from "@/lib/api";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
await getSession();
|
||||
const layers = await prisma.mapLayer.findMany({
|
||||
orderBy: [{ order: "asc" }, { createdAt: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
width: true,
|
||||
height: true,
|
||||
isDefault: true,
|
||||
order: true,
|
||||
// imageData omitted from list (huge); fetch per-layer
|
||||
},
|
||||
});
|
||||
return json(layers);
|
||||
} catch (e) {
|
||||
return asResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
await requireDm();
|
||||
const b = (await req.json()) as Partial<{
|
||||
name: string;
|
||||
imageData: string;
|
||||
width: number;
|
||||
height: number;
|
||||
isDefault: boolean;
|
||||
}>;
|
||||
const name = (b.name ?? "").trim();
|
||||
if (!name) return err("Name required");
|
||||
const imageData = (b.imageData ?? "").trim();
|
||||
if (imageData && !/^data:image\/(png|jpeg|jpg|webp|svg\+xml);base64,/.test(imageData)) {
|
||||
return err("imageData must be a data: URL of an image");
|
||||
}
|
||||
if (imageData.length > 6_000_000)
|
||||
return err("Image too large (max ~4.5MB encoded)", 413);
|
||||
if (b.isDefault) {
|
||||
await prisma.mapLayer.updateMany({ data: { isDefault: false } });
|
||||
}
|
||||
const last = await prisma.mapLayer.findFirst({ orderBy: { order: "desc" } });
|
||||
const created = await prisma.mapLayer.create({
|
||||
data: {
|
||||
name,
|
||||
imageData,
|
||||
width: b.width ?? 1000,
|
||||
height: b.height ?? 700,
|
||||
isDefault: b.isDefault ?? false,
|
||||
order: (last?.order ?? -1) + 1,
|
||||
},
|
||||
select: {
|
||||
id: true, name: true, width: true, height: true, isDefault: true, order: true,
|
||||
},
|
||||
});
|
||||
return json(created, { status: 201 });
|
||||
} catch (e) {
|
||||
return asResponse(e);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user