|
1 | 1 | import { db } from '$lib/server/db/index.js'; |
2 | | -import { project, user, devlog } from '$lib/server/db/schema.js'; |
| 2 | +import { project, user, devlog, legionReview } from '$lib/server/db/schema.js'; |
3 | 3 | import { error } from '@sveltejs/kit'; |
4 | | -import { eq, and, sql, ne, inArray } from 'drizzle-orm'; |
| 4 | +import { eq, and, sql, ne, inArray, desc, gt } from 'drizzle-orm'; |
5 | 5 | import type { Actions } from './$types'; |
6 | 6 | import { getCurrentlyPrinting } from './utils.server'; |
7 | 7 |
|
@@ -31,13 +31,34 @@ export async function load({ locals }) { |
31 | 31 | .from(user) |
32 | 32 | .where(and(ne(user.trust, 'red'), ne(user.hackatimeTrust, 'red'))); // hide banned users |
33 | 33 |
|
| 34 | + const legionAgg = db |
| 35 | + .$with('legionAgg') |
| 36 | + .as( |
| 37 | + db |
| 38 | + .select({ userId: legionReview.userId, legionCnt: sql<number>`COUNT(*)`.as('legionCnt') }) |
| 39 | + .from(legionReview) |
| 40 | + .where(eq(legionReview.action, 'print')) |
| 41 | + .groupBy(legionReview.userId) |
| 42 | + ); |
| 43 | + |
| 44 | + const totalExpr = sql<number>`COALESCE(${legionAgg.legionCnt}, 0)`; |
| 45 | + |
| 46 | + const leaderboard = await db |
| 47 | + .with(legionAgg) |
| 48 | + .select({ id: user.id, name: user.name, review_count: totalExpr }) |
| 49 | + .from(user) |
| 50 | + .leftJoin(legionAgg, eq(legionAgg.userId, user.id)) |
| 51 | + .where(and(ne(user.trust, 'red'), ne(user.hackatimeTrust, 'red'), gt(totalExpr, 0))) |
| 52 | + .orderBy(desc(totalExpr)); |
| 53 | + |
34 | 54 | const currentlyPrinting = await getCurrentlyPrinting(locals.user); |
35 | 55 |
|
36 | 56 | return { |
37 | 57 | allProjects, |
38 | 58 | projects, |
39 | 59 | users, |
40 | | - currentlyPrinting |
| 60 | + currentlyPrinting, |
| 61 | + leaderboard |
41 | 62 | }; |
42 | 63 | } |
43 | 64 |
|
|
0 commit comments