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 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<Self> {
sqlx::query_as!(
pub async fn all(db: &SqlitePool) -> Vec<BoatWithDetails> {
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 {