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]
pub trait Delete<'c>: Sized {
type Id;
type Filter;
pub trait Delete: Sized {
type Id: Send + Copy;
type Filter: Send + Sync;
async fn delete<T>(
async fn delete<'c, T>(
ctx: &Context,
db: T,
filter: Self::Filter,
filter: &Self::Filter,
id: Self::Id,
) -> Result<bool, Error>
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<Self::Id>,
// ) -> Result<bool, Error> {
// let mut transaction = pool.begin().await?;
async fn delete_all<'c>(
// &self,
ctx: &Context,
pool: &'c sqlite::Pool,
filter: Self::Filter,
ids: Vec<Self::Id>,
) -> Result<bool, Error> {
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)
}
}
}

View File

@@ -60,7 +60,7 @@ impl TryFrom<TodoRow> 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<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
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

View File

@@ -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?;