diff --git a/src/components/mod.rs b/src/components/mod.rs index d7ee06d..c1c27c5 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -52,14 +52,14 @@ pub mod crud { } #[async_trait] - pub trait Delete<'c>: Sized { - type Id; - type Filter; + pub trait Delete: Sized { + type Id: Send + Copy; + type Filter: Send + Sync; - async fn delete( + async fn delete<'c, T>( ctx: &Context, db: T, - filter: Self::Filter, + filter: &Self::Filter, id: Self::Id, ) -> Result where @@ -74,19 +74,29 @@ pub mod crud { // * A transaction will begin() (a noop) and then acquire() a new connection T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug; - // async fn delete_all( - // ctx: &Context, - // pool: &sqlite::Pool, - // filter: Self::Filter, - // ids: Vec, - // ) -> Result { - // let mut transaction = pool.begin().await?; + async fn delete_all<'c>( + // &self, + ctx: &Context, + pool: &'c sqlite::Pool, + filter: Self::Filter, + ids: Vec, + ) -> Result { + use sqlx::Acquire as _; - // for id in ids { - // Self::delete(ctx, Box::new(&mut *transaction), filter, id).await?; - // } - // unimplemented!() - // } + let mut transaction = pool.begin().await?; + let conn = transaction.acquire().await?; + + for id in ids { + if !Self::delete(ctx, &mut *conn, &filter, id).await? { + // transaction will rollback on drop + return Ok(false); + } + } + + transaction.commit().await?; + + Ok(true) + } } } diff --git a/src/models/trips/todos.rs b/src/models/trips/todos.rs index 256d7df..7b1bb76 100644 --- a/src/models/trips/todos.rs +++ b/src/models/trips/todos.rs @@ -60,7 +60,7 @@ impl TryFrom for Todo { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct TodoFilter { pub trip_id: Uuid, } @@ -294,30 +294,30 @@ impl crud::Update for Todo { } #[async_trait] -impl<'c> crud::Delete<'c> for Todo { +impl crud::Delete for Todo { type Id = Uuid; type Filter = TodoFilter; #[tracing::instrument] - async fn delete(ctx: &Context, db: T, filter: TodoFilter, id: Uuid) -> Result + async fn delete<'c, T>( + ctx: &Context, + db: T, + filter: &TodoFilter, + id: Uuid, + ) -> Result where T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug, { - use sqlx::Acquire; - let id_param = id.to_string(); let user_id = ctx.user.id.to_string(); let trip_id_param = filter.trip_id.to_string(); - let mut transaction = db.begin().await?; - let conn = transaction.acquire().await?; - let results = crate::execute!( &sqlite::QueryClassification { query_type: sqlite::QueryType::Delete, component: sqlite::Component::Todo, }, - conn, + &mut *(db.acquire().await?), r#" DELETE FROM trip_todos WHERE diff --git a/src/routing/routes.rs b/src/routing/routes.rs index 613883f..abd9ccb 100644 --- a/src/routing/routes.rs +++ b/src/routing/routes.rs @@ -440,7 +440,7 @@ pub async fn trip( let deleted = models::trips::todos::Todo::delete( &ctx, &state.database_pool, - TodoFilter { trip_id: id }, + &TodoFilter { trip_id: id }, delete_todo, ) .await?; @@ -1539,7 +1539,7 @@ pub async fn trip_todo_delete( let deleted = models::trips::todos::Todo::delete( &ctx, &state.database_pool, - TodoFilter { trip_id }, + &TodoFilter { trip_id }, todo_id, ) .await?;