flbxcup/src/routes/+page.server.ts

38 lines
833 B
TypeScript

import { db } from '$lib/server/db';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ locals }) => {
// Check if user is authenticated
const user = locals.user;
// If user is not authenticated, return an empty array for competitions
if (!user) {
return {
competitions: []
};
}
try {
// Fetch competitions from the database
const userCompetitions = db.query.competitions.findMany({
orderBy: (competitions, { desc }) => [desc(competitions.start_date)],
limit: 10,
with: {
breakperiods: true,
fields: true,
matches: true,
scheduling_modes: true,
teams: true
}
});
return {
competitions: await userCompetitions
};
} catch (error) {
console.error('Error fetching competitions:', error);
return {
competitions: []
};
}
};