Your Name 01e124ebb1
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
fix: mark image stats route as dynamic
2026-03-25 15:42:07 +08:00

39 lines
1009 B
TypeScript

import { NextResponse } from "next/server";
import { db } from "@/lib/db";
import { generatedImage } from "@/lib/db/schema";
import { eq, sql } from "drizzle-orm";
import { auth } from "@/app/(auth)/auth";
export const dynamic = "force-dynamic";
export async function GET() {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const userId = session.user.id;
// 查询该用户的生图总数
const result = await db
.select({ count: sql<number>`count(*)::int` })
.from(generatedImage)
.where(eq(generatedImage.userId, userId));
const totalImages = result[0]?.count ?? 0;
return NextResponse.json({
totalImages,
updatedAt: new Date().toISOString(),
});
} catch (error) {
console.error("[User Image Stats API] Error:", error);
return NextResponse.json(
{ error: "Failed to fetch image stats" },
{ status: 500 }
);
}
}