From e3fb452b28bdfea408246ce64caea25717ada1f5 Mon Sep 17 00:00:00 2001 From: Philipp Hofer Date: Sun, 3 Aug 2025 10:07:55 +0200 Subject: [PATCH] show sightings from own client --- src/game.rs | 17 ++++++++++++-- src/main.rs | 8 +------ src/model/camera.rs | 2 +- src/model/highscore.rs | 1 - src/model/sighting.rs | 51 +++++++++++++++++++++++++++++++++--------- 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/game.rs b/src/game.rs index 2f2533c..bf3c151 100644 --- a/src/game.rs +++ b/src/game.rs @@ -48,9 +48,22 @@ async fn index(State(backend): State>, cookies: CookieJar) -> Respo (sightings.len()) "/" (amount_total_cameras) - " cameras." + " cameras:" progress value=(sightings.len()) max=(amount_total_cameras); } + p { + ul.iterated { + @for (idx, sighting) in sightings.iter().enumerate() { + li.card { + span { + span.font-headline.rank.text-muted { (idx+1) } + (sighting.camera.name) + } + } + } + } + + } p { h2 { "Highscore" } ul.iterated { @@ -83,7 +96,7 @@ async fn game( return Err(not_found().await.into_response()); }; - let Some(camera) = backend.camera_by_uuid(uuid).await else { + let Some(camera) = backend.get_camera(&uuid).await else { return Err(not_found().await.into_response()); }; diff --git a/src/main.rs b/src/main.rs index bdd86c5..27bea15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,13 +72,7 @@ impl Backend { async fn client_full(&self, cookies: CookieJar, headers: &HeaderMap) -> (CookieJar, Req) { let (cookies, client) = self.client(cookies).await; let lang = language::language(&cookies, headers); - ( - cookies, - Req { - client, - lang: lang.into(), - }, - ) + (cookies, Req { client, lang }) } } diff --git a/src/model/camera.rs b/src/model/camera.rs index 1e51804..176ac88 100644 --- a/src/model/camera.rs +++ b/src/model/camera.rs @@ -11,7 +11,7 @@ pub struct Camera { } impl Backend { - pub(crate) async fn camera_by_uuid(&self, uuid: Uuid) -> Option { + pub(crate) async fn get_camera(&self, uuid: &Uuid) -> Option { let uuid = uuid.to_string(); match self { Backend::Sqlite(db) => sqlx::query_as!( diff --git a/src/model/highscore.rs b/src/model/highscore.rs index a726686..30841f6 100644 --- a/src/model/highscore.rs +++ b/src/model/highscore.rs @@ -34,7 +34,6 @@ impl Backend { }, amount: row.amount, }) - .map(|r| r.into()) .collect() } } diff --git a/src/model/sighting.rs b/src/model/sighting.rs index 49644aa..0901a86 100644 --- a/src/model/sighting.rs +++ b/src/model/sighting.rs @@ -6,24 +6,55 @@ use serde::{Deserialize, Serialize}; use sqlx::{types::chrono::NaiveDateTime, FromRow}; #[derive(FromRow, Debug, Serialize, Deserialize)] -pub struct Sighting { +pub struct SightingDb { pub client_uuid: String, pub sighted_at: NaiveDateTime, - pub camera_id: String, // Changed from i64 to String to match TEXT/UUID in schema + pub camera_id: String, +} + +impl SightingDb { + async fn to_sighting(&self, backend: &Backend) -> Sighting { + let client_uuid = uuid::Uuid::parse_str(&self.client_uuid).expect("db constraints"); + let camera_uuid = uuid::Uuid::parse_str(&self.camera_id).expect("db constraints"); + Sighting { + client: backend.get_client(&client_uuid).await, + sighted_at: self.sighted_at, + camera: backend + .get_camera(&camera_uuid) + .await + .expect("db constraints"), + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Sighting { + pub client: Client, + pub sighted_at: NaiveDateTime, + pub camera: Camera, } impl Backend { pub(crate) async fn sightings_for_client(&self, client: &Client) -> Vec { let uuid = client.uuid.to_string(); match self { - Backend::Sqlite(db) => sqlx::query_as!( - Sighting, - "SELECT client_uuid, sighted_at, camera_id FROM sightings WHERE client_uuid = ?", - uuid - ) - .fetch_all(db) - .await - .unwrap(), + Backend::Sqlite(db) => { + let sighting_dbs = sqlx::query_as!( + SightingDb, + "SELECT client_uuid, sighted_at, camera_id FROM sightings WHERE client_uuid = ?", + uuid + ) + .fetch_all(db) + .await + .unwrap(); + + // Convert SightingDb to Sighting + let mut sightings = Vec::new(); + for sighting_db in sighting_dbs { + sightings.push(sighting_db.to_sighting(self).await); + } + sightings + } } }