42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { auth } from "@/lib/auth";
|
|
import { prisma } from "@/lib/db";
|
|
import { getApiLang, pickText } from "@/lib/api-locale";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const enabledOnly = request.nextUrl.searchParams.get("enabled") === "1";
|
|
const lang = getApiLang(request);
|
|
const where = enabledOnly ? { enabled: true } : {};
|
|
const images = await prisma.galleryImage.findMany({
|
|
where,
|
|
orderBy: { sortOrder: "asc" },
|
|
});
|
|
return NextResponse.json(
|
|
images.map((img) => ({
|
|
...img,
|
|
title: pickText(img.title, img.titleEn, lang),
|
|
titleZh: img.title,
|
|
titleEn: img.titleEn,
|
|
}))
|
|
);
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const session = await auth();
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const image = await prisma.galleryImage.create({
|
|
data: {
|
|
imageUrl: body.imageUrl,
|
|
title: body.title ?? "",
|
|
titleEn: body.titleEn ?? "",
|
|
sortOrder: body.sortOrder ?? 0,
|
|
enabled: body.enabled ?? true,
|
|
},
|
|
});
|
|
return NextResponse.json(image);
|
|
}
|