Your Name 7a4fa8e1d8
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
添加钉钉集成、精选作品和排行榜功能
2025-12-18 15:12:34 +08:00

47 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 });
}
}