From d7e57317534ff3dfcbcadb4d5d676b4898cedf36 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 20:34:55 +0200 Subject: [PATCH] order by most used destination --- src/model/distance.rs | 14 +++++++++++++- templates/includes/forms/log.html.tera | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) 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 %}