c912afd46a
Extends the previous loader-only sweep across the full tree: every remaining `await locals.auth()` now falls back through `locals.session ?? await locals.auth()`, so the hook's cached result is reused. 68 files, 107 sites touched — loaders, form actions, and API endpoints across cospend / tasks / fitness / faith / recipe / admin. hooks.server.ts is intentionally left alone since it's the originating call that populates locals.session in the first place.
64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { json } from '@sveltejs/kit';
|
|
import type { RequestHandler } from './$types';
|
|
import { dbConnect } from '$utils/db';
|
|
import { WorkoutTemplate } from '$models/WorkoutTemplate';
|
|
|
|
// POST /api/fitness/seed-example - Create the example workout template
|
|
export const POST: RequestHandler = async ({ locals }) => {
|
|
const session = locals.session ?? await locals.auth();
|
|
if (!session || !session.user?.nickname) {
|
|
return json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
await dbConnect();
|
|
|
|
// Check if example template already exists for this user
|
|
const existingTemplate = await WorkoutTemplate.findOne({
|
|
name: 'Push Day (Example)',
|
|
createdBy: session.user.nickname
|
|
});
|
|
|
|
if (existingTemplate) {
|
|
return json({ message: 'Example template already exists', template: existingTemplate });
|
|
}
|
|
|
|
// Create the example template with barbell squats and barbell bench press
|
|
const exampleTemplate = new WorkoutTemplate({
|
|
name: 'Push Day (Example)',
|
|
description: 'A sample push workout with squats and bench press - 3 sets of 10 reps each at 90kg',
|
|
exercises: [
|
|
{
|
|
name: 'Barbell Squats',
|
|
sets: [
|
|
{ reps: 10, weight: 90, rpe: 7 },
|
|
{ reps: 10, weight: 90, rpe: 8 },
|
|
{ reps: 10, weight: 90, rpe: 9 }
|
|
],
|
|
restTime: 120 // 2 minutes
|
|
},
|
|
{
|
|
name: 'Barbell Bench Press',
|
|
sets: [
|
|
{ reps: 10, weight: 90, rpe: 7 },
|
|
{ reps: 10, weight: 90, rpe: 8 },
|
|
{ reps: 10, weight: 90, rpe: 9 }
|
|
],
|
|
restTime: 120 // 2 minutes
|
|
}
|
|
],
|
|
isPublic: false,
|
|
createdBy: session.user.nickname
|
|
});
|
|
|
|
await exampleTemplate.save();
|
|
|
|
return json({
|
|
message: 'Example template created successfully!',
|
|
template: exampleTemplate
|
|
}, { status: 201 });
|
|
} catch (error) {
|
|
console.error('Error creating example template:', error);
|
|
return json({ error: 'Failed to create example template' }, { status: 500 });
|
|
}
|
|
}; |