diff --git a/lektor_lib/src/requests.rs b/lektor_lib/src/requests.rs index 47e48c431ea3acd3223287592223600daf68ac2d..6463fca6ff1546a9ea70782b0caa2a7a3d6a1360 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 eda67c686a5515cd3edabf89a26978be5a311c64..fd5a0b5c52aecd3f1eabe32a1a23312f07fa01a3 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 976bd54adacd1f43eec4c4c845deb364331088c5..6fcecb22ba50f3e30f4251954a7f8dae504a78f2 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 45446f9d5f683d340fe8d38be786dc3111315c51..d8f017b53dd6e94052fe71aa835fdb7a09d132d7 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