This commit is contained in:
2023-09-17 23:35:59 +02:00
parent 924698f6fa
commit ffcba0dbe4
4 changed files with 37 additions and 37 deletions

View File

@@ -8,7 +8,7 @@ pub mod crud {
#[async_trait] #[async_trait]
pub trait Create: Sized { pub trait Create: Sized {
type Id: Sized + Send + Sync + 'static; type Id: Sized + Send + Sync + 'static;
type Higher: Sized + Send + Sync + 'static; type Container: Sized + Send + Sync + 'static;
type Info: Sized + Send + Sync + 'static; type Info: Sized + Send + Sync + 'static;
fn new_id() -> Self::Id; fn new_id() -> Self::Id;
@@ -16,7 +16,7 @@ pub mod crud {
async fn create( async fn create(
ctx: &Context, ctx: &Context,
pool: &sqlite::Pool, pool: &sqlite::Pool,
higher: Self::Higher, container: Self::Container,
info: Self::Info, info: Self::Info,
) -> Result<Self::Id, Error>; ) -> Result<Self::Id, Error>;
} }
@@ -24,12 +24,12 @@ pub mod crud {
#[async_trait] #[async_trait]
pub trait Read: Sized { pub trait Read: Sized {
type Reference; type Reference;
type Higher; type Container;
async fn findall( async fn findall(
ctx: &Context, ctx: &Context,
pool: &sqlite::Pool, pool: &sqlite::Pool,
higher: Self::Higher, container: Self::Container,
) -> Result<Vec<Self>, Error>; ) -> Result<Vec<Self>, Error>;
async fn find( async fn find(
@@ -52,7 +52,7 @@ pub mod crud {
) -> Result<Option<Self>, Error>; ) -> Result<Option<Self>, Error>;
} }
pub trait Higher { pub trait Container {
type Id: Copy; type Id: Copy;
type Reference; type Reference;
@@ -62,7 +62,7 @@ pub mod crud {
#[async_trait] #[async_trait]
pub trait Delete: Sized { pub trait Delete: Sized {
type Id: Send + Copy; type Id: Send + Copy;
type Higher: Send + Sync + Higher<Reference = Self::Reference, Id = Self::Id>; type Container: Send + Sync + Container<Reference = Self::Reference, Id = Self::Id>;
type Reference: Send + Sync; type Reference: Send + Sync;
async fn delete<'c, T>( async fn delete<'c, T>(
@@ -85,7 +85,7 @@ pub mod crud {
async fn delete_all<'c>( async fn delete_all<'c>(
ctx: &Context, ctx: &Context,
pool: &'c sqlite::Pool, pool: &'c sqlite::Pool,
higher: Self::Higher, container: Self::Container,
ids: Vec<Self::Id>, ids: Vec<Self::Id>,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
use sqlx::Acquire as _; use sqlx::Acquire as _;
@@ -94,7 +94,7 @@ pub mod crud {
let conn = transaction.acquire().await?; let conn = transaction.acquire().await?;
for id in ids { for id in ids {
if !Self::delete(ctx, &mut *conn, &higher.with_id(id)).await? { if !Self::delete(ctx, &mut *conn, &container.with_id(id)).await? {
// transaction will rollback on drop // transaction will rollback on drop
return Ok(false); return Ok(false);
} }

View File

@@ -83,30 +83,30 @@ impl TryFrom<TodoRow> for Todo {
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Higher { pub struct Container {
pub trip_id: Uuid, pub trip_id: Uuid,
} }
impl crud::Higher for Higher { impl crud::Container for Container {
type Id = Id; type Id = Id;
type Reference = Reference; type Reference = Reference;
fn with_id(&self, id: Self::Id) -> Self::Reference { fn with_id(&self, id: Self::Id) -> Self::Reference {
Reference { id, higher: *self } Reference { id, container: *self }
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Reference { pub struct Reference {
pub id: Id, pub id: Id,
pub higher: Higher, pub container: Container,
} }
impl From<(Uuid, Uuid)> for Reference { impl From<(Uuid, Uuid)> for Reference {
fn from((trip_id, todo_id): (Uuid, Uuid)) -> Self { fn from((trip_id, todo_id): (Uuid, Uuid)) -> Self {
Self { Self {
id: Id::new(todo_id), id: Id::new(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
} }
} }
} }
@@ -135,14 +135,14 @@ impl Todo {
#[async_trait] #[async_trait]
impl crud::Read for Todo { impl crud::Read for Todo {
type Reference = Reference; type Reference = Reference;
type Higher = Higher; type Container = Container;
async fn findall( async fn findall(
ctx: &Context, ctx: &Context,
pool: &sqlite::Pool, pool: &sqlite::Pool,
higher: Higher, container: Container,
) -> Result<Vec<Self>, Error> { ) -> Result<Vec<Self>, Error> {
let trip_id_param = higher.trip_id.to_string(); let trip_id_param = container.trip_id.to_string();
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
let todos: Vec<Todo> = crate::query_all!( let todos: Vec<Todo> = crate::query_all!(
@@ -179,7 +179,7 @@ impl crud::Read for Todo {
pool: &sqlite::Pool, pool: &sqlite::Pool,
reference: Reference, reference: Reference,
) -> Result<Option<Self>, Error> { ) -> Result<Option<Self>, Error> {
let trip_id_param = reference.higher.trip_id.to_string(); let trip_id_param = reference.container.trip_id.to_string();
let todo_id_param = reference.id.0.to_string(); let todo_id_param = reference.id.0.to_string();
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
crate::query_one!( crate::query_one!(
@@ -218,7 +218,7 @@ pub struct TodoNew {
#[async_trait] #[async_trait]
impl crud::Create for Todo { impl crud::Create for Todo {
type Id = Id; type Id = Id;
type Higher = Higher; type Container = Container;
type Info = TodoNew; type Info = TodoNew;
fn new_id() -> Self::Id { fn new_id() -> Self::Id {
@@ -228,14 +228,14 @@ impl crud::Create for Todo {
async fn create( async fn create(
ctx: &Context, ctx: &Context,
pool: &sqlite::Pool, pool: &sqlite::Pool,
higher: Self::Higher, container: Self::Container,
info: Self::Info, info: Self::Info,
) -> Result<Self::Id, Error> { ) -> Result<Self::Id, Error> {
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
let id = Self::new_id(); let id = Self::new_id();
tracing::info!("adding new todo with id {id}"); tracing::info!("adding new todo with id {id}");
let id_param = id.to_string(); let id_param = id.to_string();
let trip_id_param = higher.trip_id.to_string(); let trip_id_param = container.trip_id.to_string();
crate::execute!( crate::execute!(
&sqlite::QueryClassification { &sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert, query_type: sqlite::QueryType::Insert,
@@ -309,7 +309,7 @@ impl crud::Update for Todo {
update_element: Self::UpdateElement, update_element: Self::UpdateElement,
) -> Result<Option<Self>, Error> { ) -> Result<Option<Self>, Error> {
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
let trip_id_param = reference.higher.trip_id.to_string(); let trip_id_param = reference.container.trip_id.to_string();
let todo_id_param = reference.id.to_string(); let todo_id_param = reference.id.to_string();
match update_element { match update_element {
UpdateElement::State(state) => { UpdateElement::State(state) => {
@@ -346,7 +346,7 @@ impl crud::Update for Todo {
} }
UpdateElement::Description(new_description) => { UpdateElement::Description(new_description) => {
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
let trip_id_param = reference.higher.trip_id.to_string(); let trip_id_param = reference.container.trip_id.to_string();
let todo_id_param = reference.id.to_string(); let todo_id_param = reference.id.to_string();
let result = crate::query_one!( let result = crate::query_one!(
@@ -386,7 +386,7 @@ impl crud::Update for Todo {
#[async_trait] #[async_trait]
impl crud::Delete for Todo { impl crud::Delete for Todo {
type Id = Id; type Id = Id;
type Higher = Higher; type Container = Container;
type Reference = Reference; type Reference = Reference;
#[tracing::instrument] #[tracing::instrument]
@@ -396,7 +396,7 @@ impl crud::Delete for Todo {
{ {
let id_param = reference.id.0.to_string(); let id_param = reference.id.0.to_string();
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
let trip_id_param = reference.higher.trip_id.to_string(); let trip_id_param = reference.container.trip_id.to_string();
let results = crate::execute!( let results = crate::execute!(
&sqlite::QueryClassification { &sqlite::QueryClassification {
@@ -658,7 +658,7 @@ impl route::Create for Todo {
let _todo_item = <Self as crud::Create>::create( let _todo_item = <Self as crud::Create>::create(
&ctx, &ctx,
&state.database_pool, &state.database_pool,
Higher { trip_id }, Container { trip_id },
TodoNew { TodoNew {
description: form.description, description: form.description,
}, },
@@ -705,7 +705,7 @@ impl route::Delete for Todo {
&ctx, &ctx,
&state.database_pool, &state.database_pool,
&Reference { &Reference {
higher: Higher { trip_id }, container: Container { trip_id },
id: components::trips::todos::Id(todo_id), id: components::trips::todos::Id(todo_id),
}, },
) )
@@ -765,7 +765,7 @@ pub async fn trip_todo_done(
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
UpdateElement::State(State::Done.into()), UpdateElement::State(State::Done.into()),
) )
@@ -786,7 +786,7 @@ pub async fn trip_todo_undone_htmx(
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
UpdateElement::State(State::Todo.into()), UpdateElement::State(State::Todo.into()),
) )
@@ -797,7 +797,7 @@ pub async fn trip_todo_undone_htmx(
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
) )
.await? .await?
@@ -826,7 +826,7 @@ pub async fn trip_todo_undone(
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
UpdateElement::State(State::Todo.into()), UpdateElement::State(State::Todo.into()),
) )
@@ -855,7 +855,7 @@ pub async fn trip_todo_edit(
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
) )
.await?; .await?;
@@ -887,7 +887,7 @@ pub async fn trip_todo_edit_save(
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
UpdateElement::Description(form.description.into()), UpdateElement::Description(form.description.into()),
) )
@@ -925,7 +925,7 @@ pub async fn trip_todo_edit_cancel(
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
) )
.await?; .await?;
@@ -978,7 +978,7 @@ impl route::ToggleFallback for StateUpdate {
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
value, value,
) )
@@ -1029,7 +1029,7 @@ impl route::ToggleHtmx for StateUpdate {
&state.database_pool, &state.database_pool,
Reference { Reference {
id: Id(todo_id), id: Id(todo_id),
higher: Higher { trip_id }, container: Container { trip_id },
}, },
) )
.await? .await?

View File

@@ -1023,7 +1023,7 @@ impl Trip {
crate::components::trips::todos::Todo::findall( crate::components::trips::todos::Todo::findall(
ctx, ctx,
pool, pool,
crate::components::trips::todos::Higher { trip_id: self.id }, crate::components::trips::todos::Container { trip_id: self.id },
) )
.await?, .await?,
); );

View File

@@ -442,7 +442,7 @@ pub async fn trip(
&state.database_pool, &state.database_pool,
&todos::Reference { &todos::Reference {
id: components::trips::todos::Id::new(delete_todo), id: components::trips::todos::Id::new(delete_todo),
higher: todos::Higher { trip_id: id }, container: todos::Container { trip_id: id },
}, },
) )
.await?; .await?;