first commit

This commit is contained in:
luoyangwei
2026-06-06 00:49:07 +08:00
commit 2b860d201e
134 changed files with 22773 additions and 0 deletions
+142
View File
@@ -0,0 +1,142 @@
import {
getAppUserFromRequest,
getOptionalAppUserFromRequest,
} from "@/server/app/app-auth";
import {
addUserFavorite,
removeUserFavorite,
} from "@/server/app/user-favorites";
import { getPickedGalleryPhotoById } from "@/server/site/gallery-photos";
import { NextResponse, type NextRequest } from "next/server";
import { z } from "zod";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
const paramsSchema = z.object({
id: z.uuid(),
});
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
try {
const parsed = paramsSchema.safeParse(await params);
if (!parsed.success) {
return NextResponse.json(
{ ok: false, error: "Invalid photo id." },
{ status: 400 },
);
}
const user = await getOptionalAppUserFromRequest(request);
const data = await getPickedGalleryPhotoById({
id: parsed.data.id,
userId: user?.id ?? null,
});
if (!data) {
return NextResponse.json(
{ ok: false, error: "Photo was not found." },
{ status: 404 },
);
}
return NextResponse.json({
ok: true,
data,
});
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error.";
return NextResponse.json(
{ ok: false, error: message },
{ status: 500 },
);
}
}
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
try {
const user = await getAppUserFromRequest(request);
if (!user) {
return NextResponse.json(
{ ok: false, error: "Unauthorized." },
{ status: 401 },
);
}
const parsed = paramsSchema.safeParse(await params);
if (!parsed.success) {
return NextResponse.json(
{ ok: false, error: "Invalid photo id." },
{ status: 400 },
);
}
const data = await addUserFavorite({
userId: user.id,
photoId: parsed.data.id,
});
return NextResponse.json({
ok: true,
data,
});
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error.";
return NextResponse.json(
{ ok: false, error: message },
{ status: 500 },
);
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
try {
const user = await getAppUserFromRequest(request);
if (!user) {
return NextResponse.json(
{ ok: false, error: "Unauthorized." },
{ status: 401 },
);
}
const parsed = paramsSchema.safeParse(await params);
if (!parsed.success) {
return NextResponse.json(
{ ok: false, error: "Invalid photo id." },
{ status: 400 },
);
}
await removeUserFavorite({
userId: user.id,
photoId: parsed.data.id,
});
return NextResponse.json({
ok: true,
});
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error.";
return NextResponse.json(
{ ok: false, error: message },
{ status: 500 },
);
}
}
+54
View File
@@ -0,0 +1,54 @@
import { getOptionalAppUserFromRequest } from "@/server/app/app-auth";
import { listPickedGalleryPhotos } from "@/server/site/gallery-photos";
import { NextResponse, type NextRequest } from "next/server";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const page = parseInt(searchParams.get("page") || "1", 10);
const pageSize = parseInt(
searchParams.get("pageSize") ||
searchParams.get("page_size") ||
"24",
10,
);
const colorFamily = searchParams.get("colorFamily");
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 listPickedGalleryPhotos({
colorFamily,
page,
pageSize,
userId: user?.id ?? null,
});
return NextResponse.json({
ok: true,
data: result.data,
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 },
);
}
}