Add ability to add/update/delete exercise categories

This commit is contained in:
Peter Stockings
2026-02-08 16:48:47 +11:00
parent ce28f7f749
commit a6eca1b4ac
6 changed files with 269 additions and 3 deletions

View File

@@ -147,3 +147,38 @@ class Exercises:
return distribution
# Category Management
def add_category(self, name):
result = self.execute('INSERT INTO exercise_attribute_category (name) VALUES (%s) RETURNING category_id, name', [name], commit=True, one=True)
return result
def update_category(self, category_id, name):
self.execute('UPDATE exercise_attribute_category SET name = %s WHERE category_id = %s', [name, category_id], commit=True)
return {"category_id": category_id, "name": name}
def delete_category(self, category_id):
# First delete all attributes in this category
attributes = self.execute('SELECT attribute_id FROM exercise_attribute WHERE category_id = %s', [category_id])
for attr in attributes:
self.delete_attribute(attr['attribute_id'])
self.execute('DELETE FROM exercise_attribute_category WHERE category_id = %s', [category_id], commit=True)
# Attribute Management
def add_attribute(self, name, category_id):
result = self.execute('INSERT INTO exercise_attribute (name, category_id) VALUES (%s, %s) RETURNING attribute_id, name, category_id', [name, category_id], commit=True, one=True)
return result
def update_attribute(self, attribute_id, name, category_id=None):
if category_id:
self.execute('UPDATE exercise_attribute SET name = %s, category_id = %s WHERE attribute_id = %s', [name, category_id, attribute_id], commit=True)
else:
self.execute('UPDATE exercise_attribute SET name = %s WHERE attribute_id = %s', [name, attribute_id], commit=True)
return self.execute('SELECT attribute_id, name, category_id FROM exercise_attribute WHERE attribute_id = %s', [attribute_id], one=True)
def delete_attribute(self, attribute_id):
# Remove from all exercises first
self.execute('DELETE FROM exercise_to_attribute WHERE attribute_id = %s', [attribute_id], commit=True)
# Delete the attribute
self.execute('DELETE FROM exercise_attribute WHERE attribute_id = %s', [attribute_id], commit=True)