From 1a4d5ac569e626da17477c9cde30553e7a9474b3 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 20:21:59 +0200 Subject: [PATCH 1/3] create own default_destination table to remove clutter; Fixes #646 --- migration.sql | 6 +++++ src/model/distance.rs | 21 ++++++++++++++++ src/model/logbook.rs | 34 -------------------------- src/model/mod.rs | 1 + src/tera/log.rs | 5 ++-- templates/includes/forms/log.html.tera | 2 +- 6 files changed, 32 insertions(+), 37 deletions(-) create mode 100644 src/model/distance.rs diff --git a/migration.sql b/migration.sql index 6a15998..7da1feb 100644 --- a/migration.sql +++ b/migration.sql @@ -213,3 +213,9 @@ CREATE TABLE IF NOT EXISTS "trailer_reservation" ( "created_at" datetime not null default CURRENT_TIMESTAMP ); +CREATE TABLE IF NOT EXISTS "distance" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "destination" text NOT NULL, + "distance_in_km" integer NOT NULL +); + diff --git a/src/model/distance.rs b/src/model/distance.rs new file mode 100644 index 0000000..91e944f --- /dev/null +++ b/src/model/distance.rs @@ -0,0 +1,21 @@ +use serde::Serialize; +use sqlx::{FromRow, SqlitePool}; + +#[derive(FromRow, Serialize, Clone, Debug)] +pub struct Distance { + pub id: i64, + pub destination: String, + pub distance_in_km: i64, +} + +impl Distance { + pub async fn all(db: &SqlitePool) -> Vec { + sqlx::query_as!( + Self, + "SELECT id, destination, distance_in_km FROM distance;" + ) + .fetch_all(db) + .await + .unwrap() + } +} diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 861c21f..cac3fdc 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -496,25 +496,6 @@ ORDER BY departure DESC Ok(()) } - pub async fn distances(db: &SqlitePool) -> Vec<(String, i64)> { - let result = sqlx::query!("SELECT destination, distance_in_km FROM logbook WHERE id IN (SELECT MIN(id) FROM logbook GROUP BY destination) AND destination IS NOT NULL AND distance_in_km IS NOT NULL;") - .fetch_all(db) - .await - .unwrap(); - - result - .into_iter() - .filter_map(|r| { - if let (Some(destination), Some(distance_in_km)) = (r.destination, r.distance_in_km) - { - Some((destination, distance_in_km)) - } else { - None - } - }) - .collect() - } - async fn remove_rowers(&self, db: &mut Transaction<'_, Sqlite>) { sqlx::query!("DELETE FROM rower WHERE logbook_id=?", self.id) .execute(db.deref_mut()) @@ -1071,21 +1052,6 @@ mod test { assert_eq!(res, Err(LogbookCreateError::TooManyRowers(1, 2))); } - #[sqlx::test] - fn test_distances() { - let pool = testdb!(); - - let res = Logbook::distances(&pool).await; - - assert_eq!( - res, - vec![ - ("Ottensheim".into(), 25 as i64), - ("Ottensheim + Regattastrecke".into(), 29 as i64), - ] - ); - } - #[sqlx::test] fn test_succ_home() { let pool = testdb!(); diff --git a/src/model/mod.rs b/src/model/mod.rs index e6d3349..2ab60e0 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -14,6 +14,7 @@ pub mod boat; pub mod boatdamage; pub mod boathouse; pub mod boatreservation; +pub mod distance; pub mod event; pub mod family; pub mod location; diff --git a/src/tera/log.rs b/src/tera/log.rs index 9244bbb..1939faa 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -18,6 +18,7 @@ use tera::Context; use crate::model::{ boat::Boat, boatreservation::BoatReservation, + distance::Distance, log::Log, logbook::{ LogToAdd, LogToFinalize, LogToUpdate, Logbook, LogbookAdminUpdateError, LogbookCreateError, @@ -75,7 +76,7 @@ async fn index( }); let logtypes = LogType::all(db).await; - let distances = Logbook::distances(db).await; + let distances = Distance::all(db).await; let on_water = Logbook::on_water(db).await; @@ -180,7 +181,7 @@ async fn kiosk( }); let logtypes = LogType::all(db).await; - let distances = Logbook::distances(db).await; + let distances = Distance::all(db).await; let on_water = Logbook::on_water(db).await; diff --git a/templates/includes/forms/log.html.tera b/templates/includes/forms/log.html.tera index e666945..beb5452 100644 --- a/templates/includes/forms/log.html.tera +++ b/templates/includes/forms/log.html.tera @@ -55,7 +55,7 @@ value="" data-relation="distance_in_km" /> - {% for distance in distances %}
From d7e57317534ff3dfcbcadb4d5d676b4898cedf36 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 20:34:55 +0200 Subject: [PATCH 2/3] 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 %} From 8e65a6540d13cbd2de4ce5d285cd7165129812ee Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 20:43:45 +0200 Subject: [PATCH 3/3] fix tests --- seeds.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/seeds.sql b/seeds.sql index d832416..b1e3009 100644 --- a/seeds.sql +++ b/seeds.sql @@ -67,3 +67,5 @@ INSERT INTO "boat_damage" (boat_id, desc, user_id_created, created_at, lock_boat INSERT INTO "notification" (user_id, message, category) VALUES (1, 'This is a test notification', 'test-cat'); INSERT INTO "trailer" (name) VALUES('Großer Hänger'); INSERT INTO "trailer" (name) VALUES('Kleiner Hänger'); +insert into distance(destination, distance_in_km) values('Ottensheim', 25); +