68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
import { getOptionalAppUserFromRequest } from "@/server/app/app-auth";
|
|
import { getPublishedCollectionBySlug } from "@/server/site/photo-collections";
|
|
import { NextResponse, type NextRequest } from "next/server";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ slug: string }> },
|
|
) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const { slug } = await params;
|
|
const page = parseInt(searchParams.get("page") || "1", 10);
|
|
const pageSize = parseInt(
|
|
searchParams.get("pageSize") ||
|
|
searchParams.get("page_size") ||
|
|
"24",
|
|
10,
|
|
);
|
|
|
|
if (page < 1 || pageSize < 1 || pageSize > 100) {
|
|
return NextResponse.json(
|
|
{ ok: false, error: "Invalid pagination parameters." },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const user = await getOptionalAppUserFromRequest(request);
|
|
const result = await getPublishedCollectionBySlug({
|
|
slug,
|
|
page,
|
|
pageSize,
|
|
userId: user?.id ?? null,
|
|
});
|
|
|
|
if (!result) {
|
|
return NextResponse.json(
|
|
{ ok: false, error: "Collection was not found." },
|
|
{ status: 404 },
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
ok: true,
|
|
data: {
|
|
collection: result.collection,
|
|
photos: result.photos,
|
|
},
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
total: result.total,
|
|
hasMore: result.total > page * pageSize,
|
|
nextPage: result.total > page * pageSize ? page + 1 : null,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
const message =
|
|
error instanceof Error ? error.message : "Unknown error.";
|
|
return NextResponse.json(
|
|
{ ok: false, error: message },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|