From a1cc272697820fd47ee77b7044d60bd26513963d Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Wed, 21 Aug 2024 12:18:15 +0200 Subject: [PATCH] update user channels, fix #721 --- ffplayout/src/api/routes.rs | 32 +++++++++++++------------------- ffplayout/src/db/handles.rs | 14 ++++++++++++++ frontend | 2 +- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/ffplayout/src/api/routes.rs b/ffplayout/src/api/routes.rs index 39fca012..06d0c9f2 100644 --- a/ffplayout/src/api/routes.rs +++ b/ffplayout/src/api/routes.rs @@ -309,6 +309,7 @@ async fn update_user( role: AuthDetails, user: web::ReqData, ) -> Result { + let channel_ids = data.channel_ids.clone().unwrap_or_default(); let mut fields = String::new(); if let Some(mail) = data.mail.clone() { @@ -319,21 +320,6 @@ async fn update_user( fields.push_str(&format!("mail = '{mail}'")); } - if let Some(channel_ids) = &data.channel_ids { - if !fields.is_empty() { - fields.push_str(", "); - } - - fields.push_str(&format!( - "channel_ids = '{}'", - channel_ids - .iter() - .map(|i| i.to_string()) - .collect::>() - .join(",") - )); - } - if !data.password.is_empty() { if !fields.is_empty() { fields.push_str(", "); @@ -354,11 +340,19 @@ async fn update_user( fields.push_str(&format!("password = '{password_hash}'")); } - if handles::update_user(&pool, *id, fields).await.is_ok() { - return Ok("Update Success"); - }; + handles::update_user(&pool, *id, fields).await?; - Err(ServiceError::InternalServerError) + let related_channels = handles::select_related_channels(&pool, Some(*id)).await?; + + for channel in related_channels { + if !channel_ids.contains(&channel.id) { + handles::delete_user_channel(&pool, *id, channel.id).await?; + } + } + + handles::insert_user_channel(&pool, *id, channel_ids).await?; + + Ok("Update Success") } /// **Add User** diff --git a/ffplayout/src/db/handles.rs b/ffplayout/src/db/handles.rs index 9cfa90b0..f9aedc28 100644 --- a/ffplayout/src/db/handles.rs +++ b/ffplayout/src/db/handles.rs @@ -94,6 +94,20 @@ pub async fn select_related_channels( Ok(results) } +pub async fn delete_user_channel( + conn: &Pool, + user_id: i32, + channel_id: i32, +) -> Result { + let query = "DELETE FROM user_channels WHERE user_id = $1 AND channel_id = $2"; + + sqlx::query(query) + .bind(user_id) + .bind(channel_id) + .execute(conn) + .await +} + pub async fn update_channel( conn: &Pool, id: i32, diff --git a/frontend b/frontend index 2045e47a..e9eff701 160000 --- a/frontend +++ b/frontend @@ -1 +1 @@ -Subproject commit 2045e47ab5e9f42ff73e03a7c4c604821a73f7ca +Subproject commit e9eff70158431a69080bb192a5bb793b0475938a