add details to boat

This commit is contained in:
philipp 2023-07-30 20:13:00 +02:00
parent fafe82b15a
commit aeefd9e43f

View File

@ -1,3 +1,4 @@
use chrono::serde;
use rocket::FromForm; use rocket::FromForm;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool}; use sqlx::{FromRow, SqlitePool};
@ -19,6 +20,19 @@ pub struct Boat {
external: bool, external: bool,
} }
pub enum BoatDamage {
None,
Light,
Locked,
}
pub struct BoatWithDetails {
#[serde(flatten)]
boat: Boat,
damage: BoatDamage,
on_water: bool,
}
#[derive(FromForm)] #[derive(FromForm)]
pub struct BoatToAdd<'r> { pub struct BoatToAdd<'r> {
pub name: &'r str, pub name: &'r str,
@ -75,8 +89,8 @@ impl Boat {
.is_some() .is_some()
} }
pub async fn all(db: &SqlitePool) -> Vec<Self> { pub async fn all(db: &SqlitePool) -> Vec<BoatWithDetails> {
sqlx::query_as!( let boats = sqlx::query_as!(
Boat, Boat,
" "
SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external 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) .fetch_all(db)
.await .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 { pub async fn create(db: &SqlitePool, boat: BoatToAdd<'_>) -> bool {