From 0487ee7e6bb0cd80985362f7e7d43066e077ac1e Mon Sep 17 00:00:00 2001 From: BinChengZhao Date: Wed, 20 Oct 2021 02:32:24 +0800 Subject: [PATCH] chore: Optimize task update api. --- delicate-scheduler/src/actions/task.rs | 30 +++++++++++--------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/delicate-scheduler/src/actions/task.rs b/delicate-scheduler/src/actions/task.rs index 5830c04..d42427a 100644 --- a/delicate-scheduler/src/actions/task.rs +++ b/delicate-scheduler/src/actions/task.rs @@ -203,14 +203,14 @@ pub async fn pre_update_task_row( .map(|bind_id| model::NewTaskBind { task_id, bind_id }) .collect(); - for model::NewTaskBind { task_id, bind_id } in removed_task_binds.iter() { - diesel::delete( - task_bind::table - .filter(task_bind::task_id.eq(task_id)) - .filter(task_bind::bind_id.eq(bind_id)), - ) - .execute(&conn)?; - } + let removed_task_binds_vec: Vec = + removed_task_binds.iter().map(|b| b.bind_id).collect(); + diesel::delete( + task_bind::table + .filter(task_bind::task_id.eq(task_id)) + .filter(task_bind::bind_id.eq_any(&removed_task_binds_vec[..])), + ) + .execute(&conn)?; let append_task_binds: Vec = current_task_binds .difference(&original_task_binds) @@ -223,15 +223,11 @@ pub async fn pre_update_task_row( .values(&append_task_binds[..]) .execute(&conn)?; - let removed_task_binds_map: HashMap = - removed_task_binds_set.clone().copied().map(|b| (b, ())).collect(); - let removed_task_binds_vec: Vec = removed_task_binds_set.copied().collect(); - - diesel::delete( - task_bind::table - .filter(task_bind::task_id.eq(task_id)) - .filter(task_bind::bind_id.eq_any(&removed_task_binds_vec[..])), - ).execute(&conn)?; + let removed_task_binds_map: HashMap = removed_task_binds_set + .clone() + .copied() + .map(|b| (b, ())) + .collect(); let removed_bind_processors: Vec = original_bind_processors .iter()