From 8b07ace876ad09869d69e58358c973d80d1a71b9 Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 6 Apr 2024 18:55:38 +0200 Subject: [PATCH] show owner of boat in boat km list --- src/model/boat.rs | 8 ++++++++ src/model/stat.rs | 13 +++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/model/boat.rs b/src/model/boat.rs index 71ce3ce..d5d2494 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -343,6 +343,14 @@ ORDER BY amount_seats DESC Ok(()) } + pub async fn owner(&self, db: &SqlitePool) -> Option { + if let Some(owner_id) = self.owner { + Some(User::find_by_id(db, owner_id as i32).await.unwrap()) + } else { + None + } + } + pub async fn delete(&self, db: &SqlitePool) { sqlx::query!("DELETE FROM boat WHERE id=?", self.id) .execute(db) diff --git a/src/model/stat.rs b/src/model/stat.rs index 05d7ea2..baa05aa 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -5,6 +5,8 @@ use chrono::Datelike; use serde::Serialize; use sqlx::{FromRow, Row, SqlitePool}; +use super::boat::Boat; + #[derive(Serialize, Clone)] pub struct BoatStat { pot_years: Vec, @@ -26,7 +28,7 @@ impl BoatStat { let rows = sqlx::query( " SELECT - boat.name, + boat.id, location.name AS location, CAST(strftime('%Y', arrival) AS INTEGER) AS year, CAST(SUM(distance_in_km) AS INTEGER) AS rowed_km @@ -49,7 +51,14 @@ impl BoatStat { .unwrap(); for row in rows { - let name: String = row.get("name"); + let id: i32 = row.get("id"); + let boat = Boat::find_by_id(db, id).await.unwrap(); + let owner = if let Some(owner) = boat.owner(db).await { + owner.name + } else { + String::from("Verein") + }; + let name = format!("{} ({})", boat.name, owner); let location: String = row.get("location"); let year: i32 = row.get("year"); if year == 0 { -- 2.45.2