From 4385632f18258f1a89c45475594022bab8e1a357 Mon Sep 17 00:00:00 2001 From: tom1484 Date: Sun, 4 Feb 2024 06:15:49 +0800 Subject: [PATCH] fix update_redis_control and support no-change --- .../src/graphql/mutations/control_map.rs | 85 +++++++++++-------- editor-server/src/utils/data.rs | 27 ++---- 2 files changed, 57 insertions(+), 55 deletions(-) diff --git a/editor-server/src/graphql/mutations/control_map.rs b/editor-server/src/graphql/mutations/control_map.rs index 3b3a5182d..b62f3c187 100644 --- a/editor-server/src/graphql/mutations/control_map.rs +++ b/editor-server/src/graphql/mutations/control_map.rs @@ -133,19 +133,19 @@ impl ControlMapMutation { let raw_dancer_data: Vec = sqlx::query_as!( DancerData, r#" - SELECT - Dancer.id AS "dancer_id", - Part.type AS "part_type: PartType", - Part.id AS "part_id", - ControlData.frame_id AS "control_data_frame_id" - FROM Dancer - INNER JOIN Part - ON Dancer.id = Part.dancer_id - INNER JOIN ControlData - ON Part.id = ControlData.part_id - WHERE ControlData.frame_id = ? - ORDER BY Dancer.id ASC, Part.id ASC; - "#, + SELECT + Dancer.id AS "dancer_id", + Part.type AS "part_type: PartType", + Part.id AS "part_id", + ControlData.frame_id AS "control_data_frame_id" + FROM Dancer + INNER JOIN Part + ON Dancer.id = Part.dancer_id + INNER JOIN ControlData + ON Part.id = ControlData.part_id + WHERE ControlData.frame_id = ? + ORDER BY Dancer.id ASC, Part.id ASC; + "#, frame_id ) .fetch_all(mysql) @@ -282,7 +282,7 @@ impl ControlMapMutation { let effect_id = _data[0]; // check if the effect is valid - if !all_led_effect_ids.contains(&effect_id) { + if effect_id > 0 && !all_led_effect_ids.contains(&effect_id) { let error_message = format!( "Effect of dancer #{} part #{} is not a valid effect", index, _index @@ -318,9 +318,9 @@ impl ControlMapMutation { sqlx::query!( r#" - UPDATE ControlData - SET color_id = ?, alpha = ? - WHERE frame_id = ? AND part_id = ?; + UPDATE ControlData + SET color_id = ?, alpha = ? + WHERE frame_id = ? AND part_id = ?; "#, color_id, alpha, @@ -335,19 +335,34 @@ impl ControlMapMutation { let effect_id = _data[0]; let alpha = _data[1]; - sqlx::query!( - r#" - UPDATE ControlData - SET effect_id = ?, alpha = ? - WHERE frame_id = ? AND part_id = ?; - "#, - effect_id, - alpha, - frame_id, - part.part_id, - ) - .execute(mysql) - .await?; + if effect_id > 0 { + sqlx::query!( + r#" + UPDATE ControlData + SET effect_id = ?, alpha = ? + WHERE frame_id = ? AND part_id = ?; + "#, + effect_id, + alpha, + frame_id, + part.part_id, + ) + .execute(mysql) + .await?; + } else { + sqlx::query!( + r#" + UPDATE ControlData + SET effect_id = NULL, alpha = ? + WHERE frame_id = ? AND part_id = ?; + "#, + alpha, + frame_id, + part.part_id, + ) + .execute(mysql) + .await?; + } } }; } @@ -364,11 +379,11 @@ impl ControlMapMutation { Some(fade) => { sqlx::query!( r#" - UPDATE ControlFrame - SET - fade = ?, - meta_rev = meta_rev + 1 - WHERE id = ?; + UPDATE ControlFrame + SET + fade = ?, + meta_rev = meta_rev + 1 + WHERE id = ?; "#, fade, frame_id, diff --git a/editor-server/src/utils/data.rs b/editor-server/src/utils/data.rs index 1a3875bdf..24941d91a 100644 --- a/editor-server/src/utils/data.rs +++ b/editor-server/src/utils/data.rs @@ -262,13 +262,7 @@ pub async fn update_redis_control( .await .map_err(|e| e.to_string())?; - let dancer_controls = - partition_by_field(|dancer_control| dancer_control.id, dancer_controls); - - dancer_controls - .into_iter() - .map(|dancer_control| partition_by_field(|part| part.id, dancer_control)) - .collect_vec() + partition_by_field(|dancer_control| dancer_control.id, dancer_controls) }; let redis_key = format!("{}{}", envs.redis_ctrl_prefix, frame.id); @@ -279,19 +273,12 @@ pub async fn update_redis_control( .map(|dancer_control| { dancer_control .iter() - .map(|part_controls| { - let part_control = part_controls - .iter() - .find(|part_control| part_control.frame_id == frame.id) - .unwrap_or_else(|| panic!("ControlData {} not found", frame.id)); - - match part_control.part_type { - PartType::LED => { - PartControl(part_control.effect_id.unwrap_or(-1), part_control.alpha) - } - PartType::FIBER => { - PartControl(part_control.color_id.unwrap(), part_control.alpha) - } + .map(|part_control| match part_control.part_type { + PartType::LED => { + PartControl(part_control.effect_id.unwrap_or(-1), part_control.alpha) + } + PartType::FIBER => { + PartControl(part_control.color_id.unwrap(), part_control.alpha) } }) .collect_vec()