2025-09-03 01:01:19 +02:00
|
|
|
import { command, query } from '$app/server';
|
2025-09-02 22:51:17 +02:00
|
|
|
import { db } from '$lib/db';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
|
|
|
|
interface WorkOutData {
|
|
|
|
|
pushups: number;
|
|
|
|
|
situps: number;
|
|
|
|
|
plankSeconds: number;
|
|
|
|
|
runKm: number;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 01:01:19 +02:00
|
|
|
export const saveWorkout = command(
|
2025-09-02 22:51:17 +02:00
|
|
|
z.object({
|
|
|
|
|
pushups: z.number().min(0),
|
|
|
|
|
situps: z.number().min(0),
|
|
|
|
|
plankSeconds: z.number().min(0),
|
|
|
|
|
runKm: z.number().min(0)
|
|
|
|
|
}),
|
|
|
|
|
async (workoutData: WorkOutData) => {
|
|
|
|
|
const { pushups, situps, plankSeconds, runKm } = workoutData;
|
|
|
|
|
|
|
|
|
|
// Validate input data
|
|
|
|
|
if (typeof pushups !== 'number' || pushups < 0) {
|
|
|
|
|
throw new Error('Invalid pushups value');
|
|
|
|
|
}
|
|
|
|
|
if (typeof situps !== 'number' || situps < 0) {
|
|
|
|
|
throw new Error('Invalid situps value');
|
|
|
|
|
}
|
|
|
|
|
if (typeof plankSeconds !== 'number' || plankSeconds < 0) {
|
|
|
|
|
throw new Error('Invalid plank time value');
|
|
|
|
|
}
|
|
|
|
|
if (typeof runKm !== 'number' || runKm < 0) {
|
|
|
|
|
throw new Error('Invalid run distance value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Insert or update today's workout
|
|
|
|
|
const result = await db.query(
|
|
|
|
|
`
|
|
|
|
|
INSERT INTO daily_exercises (date, pushups, situps, plank_time_seconds, run_distance_km)
|
|
|
|
|
VALUES (CURRENT_DATE, $1, $2, $3, $4)
|
|
|
|
|
ON CONFLICT (date)
|
|
|
|
|
DO UPDATE SET
|
|
|
|
|
pushups = $1,
|
|
|
|
|
situps = $2,
|
|
|
|
|
plank_time_seconds = $3,
|
|
|
|
|
run_distance_km = $4,
|
|
|
|
|
updated_at = CURRENT_TIMESTAMP
|
|
|
|
|
RETURNING *
|
|
|
|
|
`,
|
|
|
|
|
[pushups, situps, plankSeconds, runKm]
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-03 00:54:01 +02:00
|
|
|
await getTodaysWorkout().refresh();
|
|
|
|
|
|
2025-09-02 22:51:17 +02:00
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
data: result.rows[0],
|
|
|
|
|
message: 'Workout saved successfully!'
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error saving workout:', error);
|
|
|
|
|
throw new Error('Failed to save workout to database');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const getTodaysWorkout = query(async () => {
|
|
|
|
|
try {
|
|
|
|
|
// Get today's workout data
|
|
|
|
|
const result = await db.query('SELECT * FROM daily_exercises WHERE date = CURRENT_DATE');
|
|
|
|
|
|
|
|
|
|
if (result.rows.length === 0) {
|
|
|
|
|
return {
|
|
|
|
|
data: null,
|
|
|
|
|
message: 'No workout recorded for today'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
data: result.rows[0]
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching workout:', error);
|
|
|
|
|
throw new Error('Failed to fetch workout from database');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const getWorkoutHistory = query(async (days: number = 7) => {
|
|
|
|
|
try {
|
|
|
|
|
// Get workout history for the last N days
|
|
|
|
|
const result = await db.query(
|
|
|
|
|
`
|
|
|
|
|
SELECT * FROM daily_exercises
|
|
|
|
|
WHERE date >= CURRENT_DATE - INTERVAL '$1 days'
|
|
|
|
|
ORDER BY date DESC
|
|
|
|
|
`,
|
|
|
|
|
[days]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
data: result.rows,
|
|
|
|
|
message: `Retrieved ${result.rows.length} workout records`
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching workout history:', error);
|
|
|
|
|
throw new Error('Failed to fetch workout history from database');
|
|
|
|
|
}
|
|
|
|
|
});
|