39 lines
1009 B
TypeScript
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 }
|
|
);
|
|
}
|
|
}
|