Fix tags, may have increased load on database while only using one query to fetch workout, topsets, and all tags

This commit is contained in:
Peter Stockings
2024-11-04 22:33:08 +11:00
parent 10326ccd7a
commit b5493e627c
6 changed files with 48 additions and 33 deletions

View File

@@ -18,17 +18,19 @@ class Workout:
t.repetitions,
t.weight,
tag.tag_id,
tag.name AS tag_name
tag.name AS tag_name,
tag.filter as tag_filter,
CASE WHEN wt.workout_id IS NOT NULL THEN TRUE ELSE FALSE END AS is_selected
FROM
workout w
JOIN
LEFT JOIN
topset t ON w.workout_id = t.workout_id
JOIN
LEFT JOIN
exercise e ON t.exercise_id = e.exercise_id
LEFT JOIN
workout_tag wt ON w.workout_id = wt.workout_id
LEFT JOIN
tag ON wt.tag_id = tag.tag_id
tag ON TRUE -- Join to get all tags
WHERE
w.person_id = %s
AND w.workout_id = %s;
@@ -37,8 +39,6 @@ class Workout:
if not data:
return {"error": "Workout not found"}, 404
exercises = self.execute("SELECT exercise_id, name FROM exercise;")
# Initialize workout dictionary
workout_data = {
@@ -48,7 +48,6 @@ class Workout:
"note": data[0]["note"],
"tags": [],
"top_sets": [],
"exercises": exercises
}
# Initialize helpers for tracking unique tags and topsets
@@ -57,15 +56,21 @@ class Workout:
# Process each row and add tags and topsets to workout_data
for row in data:
# Add unique tags
# Add all unique tags with is_selected property
tag_id = row["tag_id"]
if tag_id and tag_id not in tag_set:
workout_data["tags"].append({"tag_id": tag_id, "tag_name": row["tag_name"]})
workout_data["tags"].append({
"tag_id": tag_id,
"tag_name": row["tag_name"],
"tag_filter": row["tag_filter"],
"person_id": person_id,
"is_selected": row["is_selected"]
})
tag_set.add(tag_id)
# Add unique topsets based on topset_id
topset_id = row["topset_id"]
if topset_id not in topsets_seen:
if topset_id and topset_id not in topsets_seen:
workout_data["top_sets"].append({
"topset_id": topset_id,
"exercise_id": row["exercise_id"],