From 8a40dfe01798cc4265ad2d1aac2b9b2386d4a9cb Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 5 Aug 2023 12:59:02 +0200 Subject: [PATCH] allow filter for kiosk, to only show boats at specific location --- src/model/boat.rs | 49 +++++++++++++++++++++++++++++++------------ src/model/location.rs | 15 +++++++++++++ src/tera/log.rs | 7 ++++--- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/model/boat.rs b/src/model/boat.rs index 444dcbf..5e2cd5f 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -94,19 +94,7 @@ impl Boat { .is_some() } - pub async fn all(db: &SqlitePool) -> Vec { - let boats = sqlx::query_as!( - Boat, - " -SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external -FROM boat -ORDER BY amount_seats DESC - " - ) - .fetch_all(db) - .await - .unwrap(); //TODO: fixme - + async fn boats_to_details(db: &SqlitePool, boats: Vec) -> Vec { let mut res = Vec::new(); for boat in boats { let mut damage = BoatDamage::None; @@ -125,6 +113,41 @@ ORDER BY amount_seats DESC res } + pub async fn all(db: &SqlitePool) -> Vec { + let boats = sqlx::query_as!( + Boat, + " +SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external +FROM boat +ORDER BY amount_seats DESC + " + ) + .fetch_all(db) + .await + .unwrap(); //TODO: fixme + + Self::boats_to_details(db, boats).await + } + + pub async fn all_at_location(db: &SqlitePool, location: String) -> Vec { + let boats = sqlx::query_as!( + Boat, + " +SELECT boat.id, boat.name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external +FROM boat +INNER JOIN location ON boat.location_id = location.id +WHERE location.name=? +ORDER BY amount_seats DESC + ", + location + ) + .fetch_all(db) + .await + .unwrap(); //TODO: fixme + + Self::boats_to_details(db, boats).await + } + pub async fn create(db: &SqlitePool, boat: BoatToAdd<'_>) -> Result<(), String> { sqlx::query!( "INSERT INTO boat(name, amount_seats, year_built, boatbuilder, default_shipmaster_only_steering, skull, external, location_id, owner) VALUES (?,?,?,?,?,?,?,?,?)", diff --git a/src/model/location.rs b/src/model/location.rs index a66d810..7d174ec 100644 --- a/src/model/location.rs +++ b/src/model/location.rs @@ -23,6 +23,21 @@ impl Location { .ok() } + pub async fn find_by_name(db: &SqlitePool, name: String) -> Option { + sqlx::query_as!( + Self, + " + SELECT id, name + FROM location + WHERE name=? + ", + name + ) + .fetch_one(db) + .await + .ok() + } + pub async fn all(db: &SqlitePool) -> Vec { sqlx::query_as!(Self, "SELECT id, name FROM location") .fetch_all(db) diff --git a/src/tera/log.rs b/src/tera/log.rs index bc8a8df..ed78d54 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -83,16 +83,17 @@ fn new_kiosk(cookies: &CookieJar<'_>) -> Redirect { let mut cookie = Cookie::new("kiosk", "yes".to_string()); cookie.set_expires(OffsetDateTime::now_utc() + Duration::weeks(12)); cookies.add_private(cookie); - Redirect::to("/log") + Redirect::to("/log/Linz") } -#[get("/")] +#[get("/")] async fn kiosk( db: &State, flash: Option>, _kiosk: KioskCookie, + loc: String, ) -> Template { - let boats = Boat::all(db).await; + let boats = Boat::all_at_location(db, loc).await; let coxes = User::cox(db).await; let users = User::all(db).await; let logtypes = LogType::all(db).await;