diff --git a/.sqlx/query-182db3fb919cf345f2b05f1c1325cc939b54336c66890892859c2c0f97930c63.json b/.sqlx/query-182db3fb919cf345f2b05f1c1325cc939b54336c66890892859c2c0f97930c63.json new file mode 100644 index 0000000..9f43b33 --- /dev/null +++ b/.sqlx/query-182db3fb919cf345f2b05f1c1325cc939b54336c66890892859c2c0f97930c63.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "\n DELETE FROM trip_todos\n WHERE\n id = ?\n AND EXISTS (SELECT 1 FROM trips WHERE trip_id = ? AND user_id = ?)\n ", + "describe": { + "columns": [], + "parameters": { + "Right": 3 + }, + "nullable": [] + }, + "hash": "182db3fb919cf345f2b05f1c1325cc939b54336c66890892859c2c0f97930c63" +} diff --git a/.sqlx/query-d47bf74e8aaecdeb3730edab4d267f61ab03356497e777e55493c4f188e14a25.json b/.sqlx/query-d47bf74e8aaecdeb3730edab4d267f61ab03356497e777e55493c4f188e14a25.json deleted file mode 100644 index 9eb5b95..0000000 --- a/.sqlx/query-d47bf74e8aaecdeb3730edab4d267f61ab03356497e777e55493c4f188e14a25.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "\n DELETE FROM trip_todos\n WHERE \n id = ?\n AND EXISTS (SELECT 1 FROM trips WHERE trip_id = ? AND user_id = ?)\n ", - "describe": { - "columns": [], - "parameters": { - "Right": 3 - }, - "nullable": [] - }, - "hash": "d47bf74e8aaecdeb3730edab4d267f61ab03356497e777e55493c4f188e14a25" -} diff --git a/src/components/mod.rs b/src/components/mod.rs index 097d182..d7ee06d 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -52,29 +52,41 @@ pub mod crud { } #[async_trait] - pub trait Delete: Sized { + pub trait Delete<'c>: Sized { type Id; type Filter; - async fn delete( + async fn delete( ctx: &Context, - pool: impl sqlx::Acquire, + db: T, filter: Self::Filter, id: Self::Id, - ) -> Result; + ) -> Result + where + // we require something that allows us to get something that implements + // executor from a Sqlite database + // + // in practice, this will either be a pool or a transaction + // + // * A pool will let us begin() a new transaction directly and then + // acquire() a new conncetion + // + // * 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( + // ctx: &Context, + // pool: &sqlite::Pool, + // filter: Self::Filter, + // ids: Vec, + // ) -> Result { + // let mut transaction = pool.begin().await?; - for id in ids { - Self::delete(ctx, &mut transaction, filter, id).await?; - } - } + // for id in ids { + // Self::delete(ctx, Box::new(&mut *transaction), filter, id).await?; + // } + // unimplemented!() + // } } } diff --git a/src/models/trips/todos.rs b/src/models/trips/todos.rs index f1a12b5..256d7df 100644 --- a/src/models/trips/todos.rs +++ b/src/models/trips/todos.rs @@ -294,29 +294,33 @@ impl crud::Update for Todo { } #[async_trait] -impl crud::Delete for Todo { +impl<'c> crud::Delete<'c> for Todo { type Id = Uuid; type Filter = TodoFilter; #[tracing::instrument] - async fn delete( - ctx: &Context, - pool: &sqlite::Pool, - filter: TodoFilter, - id: Uuid, - ) -> Result { + async fn delete(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, }, - pool, + conn, r#" DELETE FROM trip_todos - WHERE + WHERE id = ? AND EXISTS (SELECT 1 FROM trips WHERE trip_id = ? AND user_id = ?) "#,