feat: optimize mappool loading; sort by average star rating and streamline query
This commit is contained in:
@@ -6,10 +6,12 @@ import { mappool } from '$lib/server/db/schema';
|
|||||||
export const load: PageServerLoad = async ({ locals }) => {
|
export const load: PageServerLoad = async ({ locals }) => {
|
||||||
if (!locals.user) redirect(302, '/');
|
if (!locals.user) redirect(302, '/');
|
||||||
|
|
||||||
const mappools = await db.query.mappool.findMany({
|
const mappools = await db.query.mappool.findMany({ with: { slots: true } });
|
||||||
with: { slots: true },
|
|
||||||
orderBy: (m, { desc }) => [desc(m.createdAt)]
|
const avgSR = (m: typeof mappools[0]) =>
|
||||||
});
|
m.slots.length ? m.slots.reduce((s, sl) => s + (sl.starRating ?? 0), 0) / m.slots.length : 0;
|
||||||
|
|
||||||
|
mappools.sort((a, b) => avgSR(b) - avgSR(a));
|
||||||
|
|
||||||
return { mappools };
|
return { mappools };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user