47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { NextResponse } from "next/server";
|
||
import { db } from "@/lib/db";
|
||
import { generatedImage, user } from "@/lib/db/schema";
|
||
import { eq, desc, sql } from "drizzle-orm";
|
||
import { auth } from "@/app/(auth)/auth";
|
||
|
||
// GET: 获取精选榜单(前5名用户)
|
||
export async function GET() {
|
||
try {
|
||
const session = await auth();
|
||
|
||
if (!session?.user?.id) {
|
||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||
}
|
||
|
||
// 查询精选数量排行榜
|
||
const ranking = await db
|
||
.select({
|
||
userId: generatedImage.userId,
|
||
userName: user.name,
|
||
userEmail: user.email,
|
||
userDepartment: user.department,
|
||
featuredCount: sql<number>`count(*)::int`,
|
||
})
|
||
.from(generatedImage)
|
||
.leftJoin(user, eq(generatedImage.userId, user.id))
|
||
.where(eq(generatedImage.isFeatured, true))
|
||
.groupBy(generatedImage.userId, user.name, user.email, user.department)
|
||
.orderBy(desc(sql`count(*)`))
|
||
.limit(5);
|
||
|
||
return NextResponse.json({
|
||
ranking: ranking.map((item, index) => ({
|
||
rank: index + 1,
|
||
userId: item.userId,
|
||
userName: item.userName || item.userEmail?.split("@")[0] || "未知用户",
|
||
department: item.userDepartment || "未设置部门",
|
||
featuredCount: item.featuredCount,
|
||
})),
|
||
});
|
||
} catch (error) {
|
||
console.error("[Featured Ranking API] Error:", error);
|
||
return NextResponse.json({ error: "Failed to fetch ranking" }, { status: 500 });
|
||
}
|
||
}
|
||
|