1. Plan creation, editing and deletion fixed. 2. Planned sets drag&drop implemented.

This commit is contained in:
AG
2025-11-24 22:11:53 +02:00
parent ada10d52e8
commit cce1e58c7b
3 changed files with 202 additions and 33 deletions

View File

@@ -28,8 +28,15 @@ router.get('/', async (req: any, res) => {
const plans = await prisma.workoutPlan.findMany({
where: { userId }
});
res.json(plans);
const mappedPlans = plans.map((p: any) => ({
...p,
steps: p.exercises ? JSON.parse(p.exercises) : []
}));
res.json(mappedPlans);
} catch (error) {
console.error('Error fetching plans:', error);
res.status(500).json({ error: 'Server error' });
}
});
@@ -38,16 +45,18 @@ router.get('/', async (req: any, res) => {
router.post('/', async (req: any, res) => {
try {
const userId = req.user.userId;
const { id, name, description, exercises } = req.body;
const { id, name, description, steps } = req.body;
const exercisesJson = JSON.stringify(steps || []);
const existing = await prisma.workoutPlan.findUnique({ where: { id } });
if (existing) {
const updated = await prisma.workoutPlan.update({
where: { id },
data: { name, description, exercises }
data: { name, description, exercises: exercisesJson }
});
return res.json(updated);
res.json({ ...updated, steps: steps || [] });
} else {
const created = await prisma.workoutPlan.create({
data: {
@@ -55,12 +64,13 @@ router.post('/', async (req: any, res) => {
userId,
name,
description,
exercises
exercises: exercisesJson
}
});
return res.json(created);
res.json({ ...created, steps: steps || [] });
}
} catch (error) {
console.error('Error saving plan:', error);
res.status(500).json({ error: 'Server error' });
}
});