From 924698f6fae4bc757d8b8a48d604b9647ec64f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Sun, 17 Sep 2023 23:33:42 +0200 Subject: [PATCH] some --- src/components/mod.rs | 2 ++ src/components/trips/todos/list.rs | 2 +- src/components/trips/todos/mod.rs | 25 ++++++++++--------------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/components/mod.rs b/src/components/mod.rs index 6f2d660..7938d7b 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -11,6 +11,8 @@ pub mod crud { type Higher: Sized + Send + Sync + 'static; type Info: Sized + Send + Sync + 'static; + fn new_id() -> Self::Id; + async fn create( ctx: &Context, pool: &sqlite::Pool, diff --git a/src/components/trips/todos/list.rs b/src/components/trips/todos/list.rs index d4397e3..233d180 100644 --- a/src/components/trips/todos/list.rs +++ b/src/components/trips/todos/list.rs @@ -30,7 +30,7 @@ impl<'a> view::View for List<'a> { { @for todo in self.todos { @let state = input.edit_todo - .map(|id| if todo.id == id { + .map(|id| if todo.id == super::Id::new(id) { super::UiState::Edit } else { super::UiState::Default diff --git a/src/components/trips/todos/mod.rs b/src/components/trips/todos/mod.rs index 659766f..5e7f2db 100644 --- a/src/components/trips/todos/mod.rs +++ b/src/components/trips/todos/mod.rs @@ -59,7 +59,7 @@ impl From for bool { #[derive(Debug)] pub struct Todo { - pub id: Uuid, + pub id: Id, pub description: String, pub state: State, } @@ -75,7 +75,7 @@ impl TryFrom for Todo { fn try_from(row: TodoRow) -> Result { Ok(Todo { - id: Uuid::try_parse(&row.id)?, + id: Id::new(Uuid::try_parse(&row.id)?), description: row.description, state: row.done.into(), }) @@ -92,10 +92,7 @@ impl crud::Higher for Higher { type Reference = Reference; fn with_id(&self, id: Self::Id) -> Self::Reference { - Reference { - id, - higher: self.clone(), - } + Reference { id, higher: *self } } } @@ -114,7 +111,7 @@ impl From<(Uuid, Uuid)> for Reference { } } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct Id(Uuid); impl std::fmt::Display for Id { @@ -129,12 +126,6 @@ impl Id { } } -impl From<(Uuid, Uuid)> for Id { - fn from((_trip_id, todo_id): (Uuid, Uuid)) -> Self { - Self(todo_id) - } -} - impl Todo { pub fn is_done(&self) -> bool { self.state == State::Done @@ -230,6 +221,10 @@ impl crud::Create for Todo { type Higher = Higher; type Info = TodoNew; + fn new_id() -> Self::Id { + Id::new(Uuid::new_v4()) + } + async fn create( ctx: &Context, pool: &sqlite::Pool, @@ -237,7 +232,7 @@ impl crud::Create for Todo { info: Self::Info, ) -> Result { let user_id = ctx.user.id.to_string(); - let id = Uuid::new_v4(); + let id = Self::new_id(); tracing::info!("adding new todo with id {id}"); let id_param = id.to_string(); let trip_id_param = higher.trip_id.to_string(); @@ -263,7 +258,7 @@ impl crud::Create for Todo { ) .await?; - Ok(components::trips::todos::Id(id)) + Ok(id) } }