From aeefd9e43fc32b91e97d372eb8187ce5fb9ec376 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 30 Jul 2023 20:13:00 +0200 Subject: [PATCH] add details to boat --- src/model/boat.rs | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/model/boat.rs b/src/model/boat.rs index 585ceef..3158688 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -1,3 +1,4 @@ +use chrono::serde; use rocket::FromForm; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, SqlitePool}; @@ -19,6 +20,19 @@ pub struct Boat { external: bool, } +pub enum BoatDamage { + None, + Light, + Locked, +} + +pub struct BoatWithDetails { + #[serde(flatten)] + boat: Boat, + damage: BoatDamage, + on_water: bool, +} + #[derive(FromForm)] pub struct BoatToAdd<'r> { pub name: &'r str, @@ -75,8 +89,8 @@ impl Boat { .is_some() } - pub async fn all(db: &SqlitePool) -> Vec { - sqlx::query_as!( + 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 @@ -86,7 +100,21 @@ ORDER BY amount_seats DESC ) .fetch_all(db) .await - .unwrap() //TODO: fixme + .unwrap(); //TODO: fixme + + let mut res = Vec::new(); + for boat in boats { + let damage = match boat.is_locked(db).await { + true => BoatDamage::Locked, + false => BoatDamage::None, + }; + res.push(BoatWithDetails { + damage, + on_water: boat.on_water(db).await, + boat, + }) + } + res } pub async fn create(db: &SqlitePool, boat: BoatToAdd<'_>) -> bool {