Move workout tag logic into tags blueprint
This commit is contained in:
82
db.py
82
db.py
@@ -341,88 +341,6 @@ class DataBase():
|
||||
def delete_tag_for_dashboard(self, tag_id):
|
||||
self.execute('DELETE FROM Tag WHERE tag_id=%s', [tag_id], commit=True)
|
||||
|
||||
# Note update logic moved to routes/notes.py
|
||||
|
||||
def add_tag_for_workout(self, workout_id, tags_id):
|
||||
# If tags_id is not empty, delete tags that are not in the new selection
|
||||
if tags_id:
|
||||
self.execute(
|
||||
"""
|
||||
DELETE FROM workout_tag
|
||||
WHERE workout_id = %s AND tag_id NOT IN %s
|
||||
""",
|
||||
[workout_id, tuple(tags_id)], commit=True
|
||||
)
|
||||
else:
|
||||
# If tags_id is empty, delete all tags for this workout
|
||||
self.execute(
|
||||
"""
|
||||
DELETE FROM workout_tag
|
||||
WHERE workout_id = %s
|
||||
""",
|
||||
[workout_id], commit=True
|
||||
)
|
||||
|
||||
# Then, attempt to insert the new tags
|
||||
for tag_id in tags_id:
|
||||
self.execute(
|
||||
"""
|
||||
INSERT INTO workout_tag (workout_id, tag_id)
|
||||
VALUES (%s, %s)
|
||||
ON CONFLICT (workout_id, tag_id) DO NOTHING
|
||||
""",
|
||||
[workout_id, tag_id], commit=True
|
||||
)
|
||||
|
||||
# Now fetch updated list of workout tags
|
||||
workout_tags = self.execute("""
|
||||
SELECT
|
||||
T.tag_id AS "tag_id",
|
||||
T.person_id AS "person_id",
|
||||
T.name AS "tag_name",
|
||||
T.filter AS "tag_filter",
|
||||
TRUE AS "is_selected"
|
||||
FROM Workout_Tag WT
|
||||
LEFT JOIN Tag T ON WT.tag_id=T.tag_id
|
||||
WHERE WT.workout_id=%s""", [workout_id])
|
||||
|
||||
return workout_tags
|
||||
|
||||
def create_tag_for_workout(self, person_id, workout_id, tag_name):
|
||||
workout_exercises = self.execute("""
|
||||
SELECT
|
||||
E.exercise_id AS "exercise_id",
|
||||
E.name AS "exercise_name"
|
||||
FROM Workout W
|
||||
LEFT JOIN TopSet T ON W.workout_id=T.workout_id
|
||||
LEFT JOIN Exercise E ON T.exercise_id=E.exercise_id
|
||||
WHERE W.workout_id=%s""", [workout_id])
|
||||
|
||||
tag_filter = "?" + \
|
||||
"&".join(
|
||||
f"exercise_id={e['exercise_id']}" for e in workout_exercises)
|
||||
|
||||
# create tag for person
|
||||
row = self.execute('INSERT INTO Tag (person_id, name, filter) VALUES (%s, %s, %s) RETURNING tag_id AS "tag_id"', [
|
||||
person_id, tag_name, tag_filter], commit=True, one=True)
|
||||
|
||||
# add tag to workout
|
||||
self.execute('INSERT INTO Workout_Tag (workout_id, tag_id) VALUES (%s, %s)', [
|
||||
workout_id, row['tag_id']], commit=True)
|
||||
|
||||
# Now fetch updated list of workout tags
|
||||
workout_tags = self.execute("""
|
||||
SELECT
|
||||
T.tag_id AS "tag_id",
|
||||
T.person_id AS "person_id",
|
||||
T.name AS "tag_name",
|
||||
T.filter AS "tag_filter"
|
||||
FROM Workout_Tag WT
|
||||
LEFT JOIN Tag T ON WT.tag_id=T.tag_id
|
||||
WHERE WT.workout_id=%s""", [workout_id])
|
||||
|
||||
return workout_tags
|
||||
|
||||
def get_workout_tags(self, person_id, workout_id):
|
||||
person_tags = self.execute("""
|
||||
SELECT
|
||||
|
||||
Reference in New Issue
Block a user