From 93987e1fc084d56190623d58fbeeca1bd8ab7e37 Mon Sep 17 00:00:00 2001
From: Kubat <maelle.martin@proton.me>
Date: Wed, 4 Dec 2024 14:17:58 +0100
Subject: [PATCH] NETWORK: Update playlist playlist

When creating a playlist with a name, now we returns its id so the
client know which playlist it just created.
---
 lektor_lib/src/requests.rs       | 10 +++++-----
 lektor_nkdb/src/playlists/mod.rs |  4 ++--
 lektord/src/app/routes.rs        |  6 +++---
 lkt/src/lib.rs                   | 10 +++++++---
 4 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/lektor_lib/src/requests.rs b/lektor_lib/src/requests.rs
index 47e48c43..6463fca6 100644
--- a/lektor_lib/src/requests.rs
+++ b/lektor_lib/src/requests.rs
@@ -10,7 +10,7 @@ use reqwest::{
     header::{self, HeaderValue},
     Body, ClientBuilder, Method, Request, Url,
 };
-use std::{sync::Arc, thread, time::Duration};
+use std::{thread, time::Duration};
 
 const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
 
@@ -194,7 +194,7 @@ pub async fn remove_range_from_history(
 
 pub async fn remove_kid_from_playlist(
     config: impl AsRef<ConnectConfig>,
-    name: Arc<str>,
+    name: &str,
     id: KId,
 ) -> Result<()> {
     request!(config; PATCH(PlaylistUpdateAction::Remove(KaraFilter::KId(id))) @ "/playlist/{}", encode_base64(name)?)
@@ -202,7 +202,7 @@ pub async fn remove_kid_from_playlist(
 
 pub async fn add_to_playlist(
     config: impl AsRef<ConnectConfig>,
-    name: Arc<str>,
+    name: &str,
     what: KaraFilter,
 ) -> Result<()> {
     request!(config; PATCH(PlaylistUpdateAction::Add(what)) @ "/playlist/{}", encode_base64(name)?)
@@ -216,8 +216,8 @@ pub async fn remove_from_playlist(
     request!(config; PATCH(PlaylistUpdateAction::Remove(what)) @ "/playlist/{id}")
 }
 
-pub async fn create_playlist(config: impl AsRef<ConnectConfig>, name: Arc<str>) -> Result<()> {
-    request!(config; PUT @ "/playlist/{}", encode_base64(name)?)
+pub async fn create_playlist(config: impl AsRef<ConnectConfig>, name: &str) -> Result<KId> {
+    request!(config; PUT @ "/playlist/{}", encode_base64(name)? => KId)
 }
 
 pub async fn delete_playlist(config: impl AsRef<ConnectConfig>, id: KId) -> Result<()> {
diff --git a/lektor_nkdb/src/playlists/mod.rs b/lektor_nkdb/src/playlists/mod.rs
index eda67c68..fd5a0b5c 100644
--- a/lektor_nkdb/src/playlists/mod.rs
+++ b/lektor_nkdb/src/playlists/mod.rs
@@ -85,14 +85,14 @@ impl<'a, Storage: DatabaseStorage + Sized> PlaylistsHandle<'a, Storage> {
         &self,
         name: impl ToString,
         settings: impl FnOnce(Playlist) -> Playlist,
-    ) -> Result<()> {
+    ) -> Result<KId> {
         let mut this = self.playlists.content.write().await;
         let next_id = KId(this.keys().map(|KId(id)| id + 1).max().unwrap_or(1));
         let plt = settings(Playlist::new(next_id, name)).updated_now();
         self.playlists.epoch.fetch_add(1, Ordering::AcqRel);
         self.storage.write_playlist(&plt).await?;
         this.insert(next_id, plt);
-        Ok(())
+        Ok(next_id)
     }
 
     /// Delete a [Playlist].
diff --git a/lektord/src/app/routes.rs b/lektord/src/app/routes.rs
index 976bd54a..6fcecb22 100644
--- a/lektord/src/app/routes.rs
+++ b/lektord/src/app/routes.rs
@@ -246,18 +246,18 @@ pub(crate) async fn create_playlist(
     State(state): State<LektorStatePtr>,
     user_infos: LektorUser,
     Path(name): Path<String>,
-) -> Result<(), LektordError> {
+) -> Result<Json<KId>, LektordError> {
     let user = state
         .verify_user(user_infos)
         .maybe_user()
         .map(|user| user.into_parts().0);
-    (state.database.playlists())
+    let id = (state.database.playlists())
         .create(name, |plt| match user {
             Ok(user) => plt.with_owner_sync(user),
             _ => plt,
         })
         .await?;
-    Ok(())
+    Ok(Json(id))
 }
 
 /// Update the database from the remotes. The user must be the super user to do that. We can't use
diff --git a/lkt/src/lib.rs b/lkt/src/lib.rs
index 45446f9d..d8f017b5 100644
--- a/lkt/src/lib.rs
+++ b/lkt/src/lib.rs
@@ -258,7 +258,11 @@ pub async fn exec_lkt(config: LktConfig, cmd: SubCommand) -> Result<()> {
         // Create a playlist
         Playlist {
             create: Some(n), ..
-        } => requests::create_playlist(config, n.into()).await,
+        } => {
+            let id = requests::create_playlist(config, n.as_str()).await?;
+            println!("created playlist `{n}` with id: {id}");
+            Ok(())
+        }
 
         // Delete a playlist and all its content.
         Playlist {
@@ -320,11 +324,11 @@ pub async fn exec_lkt(config: LktConfig, cmd: SubCommand) -> Result<()> {
             add: Some(mut args),
             ..
         } => {
-            let name = args.remove(0).into();
+            let name = args.remove(0);
             let rgx = args.join(" ").parse()?;
             let ids = requests::search_karas(config, SearchFrom::Database, [rgx]).await?;
             let add = KaraFilter::List(true, ids);
-            requests::add_to_playlist(config, name, add).await
+            requests::add_to_playlist(config, name.as_str(), add).await
         }
 
         // Remove something from a playlist
-- 
GitLab