diff --git a/src/model/distance.rs b/src/model/distance.rs index 91e944f..05e2caa 100644 --- a/src/model/distance.rs +++ b/src/model/distance.rs @@ -9,10 +9,22 @@ pub struct Distance { } impl Distance { + /// Return all default `distance`s, ordered by usage in logbook entries pub async fn all(db: &SqlitePool) -> Vec { sqlx::query_as!( Self, - "SELECT id, destination, distance_in_km FROM distance;" + "SELECT + d.id, + d.destination, + d.distance_in_km +FROM + distance d +LEFT JOIN + logbook l ON d.destination = l.destination AND d.distance_in_km = l.distance_in_km +GROUP BY + d.id, d.destination, d.distance_in_km +ORDER BY + COUNT(l.id) DESC, d.destination ASC;" ) .fetch_all(db) .await diff --git a/templates/includes/forms/log.html.tera b/templates/includes/forms/log.html.tera index beb5452..b72dad1 100644 --- a/templates/includes/forms/log.html.tera +++ b/templates/includes/forms/log.html.tera @@ -53,7 +53,8 @@ id="destination" name="destination" value="" - data-relation="distance_in_km" /> + data-relation="distance_in_km" + autocomplete="off" /> {% for distance in distances %}