41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
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";
|
||
|
||
// 缓存配置:每天中午12点更新(约12小时缓存)
|
||
export const revalidate = 43200; // 12小时,单位秒
|
||
|
||
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 }
|
||
);
|
||
}
|
||
}
|
||
|