Refactor get workout call

This commit is contained in:
Peter Stockings
2023-12-09 17:00:45 +11:00
parent 1ff616f4e0
commit c702a2ba3d
3 changed files with 41 additions and 43 deletions

34
db.py
View File

@@ -198,16 +198,16 @@ class DataBase():
def get_workout(self, person_id, workout_id):
topsets = self.execute("""
SELECT
P.person_id AS "PersonId",
P.name AS "PersonName",
W.workout_id AS "WorkoutId",
W.start_date AS "StartDate",
T.topset_id AS "TopSetId",
E.exercise_id AS "ExerciseId",
E.name AS "ExerciseName",
T.repetitions AS "Repetitions",
T.weight AS "Weight",
W.note AS "Note"
P.person_id,
P.name AS "person_name",
W.workout_id,
W.start_date,
T.topset_id,
E.exercise_id,
E.name AS "exercise_name",
T.repetitions,
T.weight,
W.note
FROM Person P
LEFT JOIN Workout W ON P.person_id=W.person_id
LEFT JOIN TopSet T ON W.workout_id=T.workout_id
@@ -216,15 +216,13 @@ class DataBase():
AND W.workout_id = %s
ORDER BY T.topset_id""", [person_id, workout_id])
note = next((t['Note'] for t in topsets), '')
return {
'PersonId': person_id,
'PersonName': next((t['PersonName'] for t in topsets), 'Unknown'),
'WorkoutId': workout_id,
'StartDate': topsets[0]['StartDate'],
'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions']} for t in topsets if t['TopSetId'] is not None],
'Note': note
'person_id': person_id,
'person_name': topsets[0]['person_name'],
'workout_id': workout_id,
'start_date': topsets[0]['start_date'],
'top_sets': [{"topset_id": t['topset_id'], "exercise_id": t['exercise_id'], "exercise_name": t['exercise_name'], "weight": t['weight'], "repetitions": t['repetitions']} for t in topsets if t['topset_id'] is not None],
'note': topsets[0]['note']
}
def get_topset(self, topset_id):