Add exercise category search in settings
This commit is contained in:
@@ -3,16 +3,42 @@ class Exercises:
|
||||
self.execute = db_connection_method
|
||||
|
||||
def get(self, query):
|
||||
# Add wildcards to the query
|
||||
search_query = f"%{query}%"
|
||||
# We need to fetch exercises with their attributes.
|
||||
# Since an exercise can have many attributes, we'll fetch basic info first or use a join.
|
||||
# But wait, the settings page just lists names. We can fetch attributes separately for each row or do a group_concat-like join.
|
||||
# However, for the settings list, we want to show the tags.
|
||||
|
||||
# Let's use a simpler approach: fetch exercises and then for each one (or via a single join) get attributes.
|
||||
exercises = self.execute("SELECT exercise_id, name FROM exercise WHERE LOWER(name) LIKE LOWER(%s) ORDER BY name ASC;", [search_query])
|
||||
|
||||
if not query:
|
||||
exercises = self.execute("SELECT exercise_id, name FROM exercise ORDER BY name ASC;")
|
||||
for ex in exercises:
|
||||
ex['attributes'] = self.get_exercise_attributes(ex['exercise_id'])
|
||||
return exercises
|
||||
|
||||
# Check for category:value syntax
|
||||
if ':' in query:
|
||||
category_part, value_part = query.split(':', 1)
|
||||
category_part = f"%{category_part.strip().lower()}%"
|
||||
value_part = f"%{value_part.strip().lower()}%"
|
||||
|
||||
query = """
|
||||
SELECT DISTINCT e.exercise_id, e.name
|
||||
FROM exercise e
|
||||
JOIN exercise_to_attribute eta ON e.exercise_id = eta.exercise_id
|
||||
JOIN exercise_attribute attr ON eta.attribute_id = attr.attribute_id
|
||||
JOIN exercise_attribute_category cat ON attr.category_id = cat.category_id
|
||||
WHERE LOWER(cat.name) LIKE LOWER(%s) AND LOWER(attr.name) LIKE LOWER(%s)
|
||||
ORDER BY e.name ASC;
|
||||
"""
|
||||
exercises = self.execute(query, [category_part, value_part])
|
||||
else:
|
||||
# Fallback: search in name OR attribute name
|
||||
search_term = query.strip().lower()
|
||||
search_query = f"%{search_term}%"
|
||||
query = """
|
||||
SELECT DISTINCT e.exercise_id, e.name
|
||||
FROM exercise e
|
||||
LEFT JOIN exercise_to_attribute eta ON e.exercise_id = eta.exercise_id
|
||||
LEFT JOIN exercise_attribute attr ON eta.attribute_id = attr.attribute_id
|
||||
WHERE LOWER(e.name) LIKE LOWER(%s) OR LOWER(attr.name) LIKE LOWER(%s)
|
||||
ORDER BY e.name ASC;
|
||||
"""
|
||||
exercises = self.execute(query, [search_query, search_query])
|
||||
|
||||
for ex in exercises:
|
||||
ex['attributes'] = self.get_exercise_attributes(ex['exercise_id'])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user