diff --git a/web/src/padelnomics/admin/routes.py b/web/src/padelnomics/admin/routes.py index 138ed69..464d983 100644 --- a/web/src/padelnomics/admin/routes.py +++ b/web/src/padelnomics/admin/routes.py @@ -1417,6 +1417,29 @@ SCENARIO_FORM_FIELDS = [ ] +async def _query_scenarios(search: str, country: str, venue_type: str) -> tuple[list, int]: + """Execute filtered scenario query. Returns (rows, total_count).""" + wheres = ["1=1"] + params: list = [] + if search: + wheres.append("(title LIKE ? OR location LIKE ? OR slug LIKE ?)") + params.extend([f"%{search}%", f"%{search}%", f"%{search}%"]) + if country: + wheres.append("country = ?") + params.append(country) + if venue_type: + wheres.append("venue_type = ?") + params.append(venue_type) + + where = " AND ".join(wheres) + rows = await fetch_all( + f"SELECT * FROM published_scenarios WHERE {where} ORDER BY created_at DESC LIMIT 500", + tuple(params), + ) + total_row = await fetch_one("SELECT COUNT(*) as cnt FROM published_scenarios") + return rows, (total_row["cnt"] if total_row else 0) + + @bp.route("/scenarios") @role_required("admin") async def scenarios(): @@ -1425,37 +1448,20 @@ async def scenarios(): country_filter = request.args.get("country", "") venue_filter = request.args.get("venue_type", "") - wheres = ["1=1"] - params: list = [] - if search: - wheres.append("(title LIKE ? OR location LIKE ? OR slug LIKE ?)") - params.extend([f"%{search}%", f"%{search}%", f"%{search}%"]) - if country_filter: - wheres.append("country = ?") - params.append(country_filter) - if venue_filter: - wheres.append("venue_type = ?") - params.append(venue_filter) - - where = " AND ".join(wheres) - scenario_list = await fetch_all( - f"SELECT * FROM published_scenarios WHERE {where} ORDER BY created_at DESC LIMIT 500", - tuple(params), - ) + scenario_list, total = await _query_scenarios(search, country_filter, venue_filter) countries = await fetch_all( "SELECT DISTINCT country FROM published_scenarios WHERE country != '' ORDER BY country" ) venue_types = await fetch_all( "SELECT DISTINCT venue_type FROM published_scenarios WHERE venue_type != '' ORDER BY venue_type" ) - total = await fetch_one("SELECT COUNT(*) as cnt FROM published_scenarios") return await render_template( "admin/scenarios.html", scenarios=scenario_list, countries=[r["country"] for r in countries], venue_types=[r["venue_type"] for r in venue_types], - total=total["cnt"] if total else 0, + total=total, current_search=search, current_country=country_filter, current_venue_type=venue_filter, @@ -1471,29 +1477,12 @@ async def scenario_results(): country_filter = request.args.get("country", "") venue_filter = request.args.get("venue_type", "") - wheres = ["1=1"] - params: list = [] - if search: - wheres.append("(title LIKE ? OR location LIKE ? OR slug LIKE ?)") - params.extend([f"%{search}%", f"%{search}%", f"%{search}%"]) - if country_filter: - wheres.append("country = ?") - params.append(country_filter) - if venue_filter: - wheres.append("venue_type = ?") - params.append(venue_filter) - - where = " AND ".join(wheres) - scenario_list = await fetch_all( - f"SELECT * FROM published_scenarios WHERE {where} ORDER BY created_at DESC LIMIT 500", - tuple(params), - ) - total = await fetch_one("SELECT COUNT(*) as cnt FROM published_scenarios") + scenario_list, total = await _query_scenarios(search, country_filter, venue_filter) return await render_template( "admin/partials/scenario_results.html", scenarios=scenario_list, - total=total["cnt"] if total else 0, + total=total, is_generating=await _is_generating(), )