diff --git a/src/model/stat.rs b/src/model/stat.rs index 2ed663a..c49b36c 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -104,9 +104,11 @@ pub struct Stat { impl Stat { pub async fn guest(db: &SqlitePool, year: Option) -> Stat { - let year = match year { - Some(year) => year, - None => chrono::Local::now().year(), + let year = year.unwrap_or_else(|| chrono::Local::now().year()); + let year_filter = if year == 0 { + String::new() + } else { + format!("AND l.arrival LIKE '{}-%'", year) }; //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) // proper guests @@ -121,7 +123,7 @@ LEFT JOIN ( FROM rower GROUP BY logbook_id ) m ON l.id = m.logbook_id -WHERE l.distance_in_km IS NOT NULL AND l.arrival LIKE '{year}-%' AND not b.external; +WHERE l.distance_in_km IS NOT NULL {year_filter} AND not b.external; " )) .fetch_one(db) @@ -145,7 +147,7 @@ WHERE u.id NOT IN ( WHERE ro.name = 'Donau Linz' ) AND l.distance_in_km IS NOT NULL -AND l.arrival LIKE '{year}-%' +{year_filter} AND u.name != 'Externe Steuerperson'; " )) @@ -183,25 +185,27 @@ AND u.name != 'Externe Steuerperson'; } pub async fn people(db: &SqlitePool, year: Option) -> Vec { - let year = match year { - Some(year) => year, - None => chrono::Local::now().year(), + let year = year.unwrap_or_else(|| chrono::Local::now().year()); + let year_filter = if year == 0 { + String::new() + } else { + format!("AND l.arrival LIKE '{}-%'", year) }; //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) sqlx::query(&format!( " SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km, COUNT(*) AS amount_trips FROM ( - SELECT * FROM user + SELECT * FROM user WHERE id IN ( - SELECT user_id FROM user_role - JOIN role ON user_role.role_id = role.id + SELECT user_id FROM user_role + JOIN role ON user_role.role_id = role.id WHERE role.name = 'Donau Linz' ) ) u INNER JOIN rower r ON u.id = r.rower_id INNER JOIN logbook l ON r.logbook_id = l.id -WHERE l.distance_in_km IS NOT NULL AND l.arrival LIKE '{year}-%' AND u.name != 'Externe Steuerperson' +WHERE l.distance_in_km IS NOT NULL {year_filter} AND u.name != 'Externe Steuerperson' GROUP BY u.name ORDER BY rowed_km DESC, u.name; " diff --git a/templates/stat.people.html.tera b/templates/stat.people.html.tera index ce947a7..3aaee4b 100644 --- a/templates/stat.people.html.tera +++ b/templates/stat.people.html.tera @@ -78,11 +78,12 @@ var queryParams = new URLSearchParams(window.location.search); return queryParams.get('year'); } - + function populateYears() { var select = document.getElementById('yearSelect'); var currentYear = new Date().getFullYear(); var selectedYear = getYearFromURL() || currentYear; + for (var year = 1977; year <= currentYear; year++) { var option = document.createElement('option'); option.value = option.textContent = year; @@ -91,13 +92,21 @@ } select.appendChild(option); } + + var gesamtOption = document.createElement('option'); + gesamtOption.value = 0; + gesamtOption.textContent = 'GESAMT'; + if (selectedYear == 0) { + gesamtOption.selected = true; + } + select.appendChild(gesamtOption); } - + function changeYear() { var selectedYear = document.getElementById('yearSelect').value; window.location.href = '?year=' + selectedYear; } - + populateYears(); {% endblock content %}