works properly

This commit is contained in:
2023-09-15 19:24:42 +02:00
parent a2ff43cd05
commit 7e62acf91a
3 changed files with 38 additions and 28 deletions

View File

@@ -52,14 +52,14 @@ pub mod crud {
} }
#[async_trait] #[async_trait]
pub trait Delete<'c>: Sized { pub trait Delete: Sized {
type Id; type Id: Send + Copy;
type Filter; type Filter: Send + Sync;
async fn delete<T>( async fn delete<'c, T>(
ctx: &Context, ctx: &Context,
db: T, db: T,
filter: Self::Filter, filter: &Self::Filter,
id: Self::Id, id: Self::Id,
) -> Result<bool, Error> ) -> Result<bool, Error>
where where
@@ -74,19 +74,29 @@ pub mod crud {
// * A transaction will begin() (a noop) and then acquire() a new connection // * A transaction will begin() (a noop) and then acquire() a new connection
T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug; T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug;
// async fn delete_all( async fn delete_all<'c>(
// ctx: &Context, // &self,
// pool: &sqlite::Pool, ctx: &Context,
// filter: Self::Filter, pool: &'c sqlite::Pool,
// ids: Vec<Self::Id>, filter: Self::Filter,
// ) -> Result<bool, Error> { ids: Vec<Self::Id>,
// let mut transaction = pool.begin().await?; ) -> Result<bool, Error> {
use sqlx::Acquire as _;
// for id in ids { let mut transaction = pool.begin().await?;
// Self::delete(ctx, Box::new(&mut *transaction), filter, id).await?; let conn = transaction.acquire().await?;
// }
// unimplemented!() 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)
}
} }
} }

View File

@@ -60,7 +60,7 @@ impl TryFrom<TodoRow> for Todo {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct TodoFilter { pub struct TodoFilter {
pub trip_id: Uuid, pub trip_id: Uuid,
} }
@@ -294,30 +294,30 @@ impl crud::Update for Todo {
} }
#[async_trait] #[async_trait]
impl<'c> crud::Delete<'c> for Todo { impl crud::Delete for Todo {
type Id = Uuid; type Id = Uuid;
type Filter = TodoFilter; type Filter = TodoFilter;
#[tracing::instrument] #[tracing::instrument]
async fn delete<T>(ctx: &Context, db: T, filter: TodoFilter, id: Uuid) -> Result<bool, Error> async fn delete<'c, T>(
ctx: &Context,
db: T,
filter: &TodoFilter,
id: Uuid,
) -> Result<bool, Error>
where where
T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug, T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug,
{ {
use sqlx::Acquire;
let id_param = id.to_string(); let id_param = id.to_string();
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
let trip_id_param = filter.trip_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!( let results = crate::execute!(
&sqlite::QueryClassification { &sqlite::QueryClassification {
query_type: sqlite::QueryType::Delete, query_type: sqlite::QueryType::Delete,
component: sqlite::Component::Todo, component: sqlite::Component::Todo,
}, },
conn, &mut *(db.acquire().await?),
r#" r#"
DELETE FROM trip_todos DELETE FROM trip_todos
WHERE WHERE

View File

@@ -440,7 +440,7 @@ pub async fn trip(
let deleted = models::trips::todos::Todo::delete( let deleted = models::trips::todos::Todo::delete(
&ctx, &ctx,
&state.database_pool, &state.database_pool,
TodoFilter { trip_id: id }, &TodoFilter { trip_id: id },
delete_todo, delete_todo,
) )
.await?; .await?;
@@ -1539,7 +1539,7 @@ pub async fn trip_todo_delete(
let deleted = models::trips::todos::Todo::delete( let deleted = models::trips::todos::Todo::delete(
&ctx, &ctx,
&state.database_pool, &state.database_pool,
TodoFilter { trip_id }, &TodoFilter { trip_id },
todo_id, todo_id,
) )
.await?; .await?;