From 8354eeff1d530b7e560e6bfee0b2f6e900c23be7 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sun, 28 Oct 2018 16:13:00 -0700 Subject: [PATCH] Implemented playlist endpoints --- src/rocket_api.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/rocket_api.rs b/src/rocket_api.rs index f39d506..d496290 100644 --- a/src/rocket_api.rs +++ b/src/rocket_api.rs @@ -11,6 +11,7 @@ use config::{self, Config, Preferences}; use db::DB; use errors; use index; +use playlist; use serve; use thumbnails; use user; @@ -40,6 +41,10 @@ pub fn get_routes() -> Vec { search_root, search, serve, + list_playlists, + save_playlist, + read_playlist, + delete_playlist, ] } @@ -278,3 +283,43 @@ fn serve(db: State, _auth: Auth, path: VFSPathBuf) -> Result, auth: Auth) -> Result>, errors::Error> { + + let playlist_names = playlist::list_playlists(&auth.username, db.deref())?; + let playlists: Vec = playlist_names + .into_iter() + .map(|p| ListPlaylistsEntry { name: p }) + .collect(); + + Ok(Json(playlists)) +} + +#[derive(Deserialize)] +struct SavePlaylistInput { + tracks: Vec, +} + +#[put("/playlist/", data = "")] +fn save_playlist(db: State, auth: Auth, name: String, playlist: Json) -> Result<(), errors::Error> { + playlist::save_playlist(&name, &auth.username, &playlist.tracks, db.deref())?; + Ok(()) +} + +#[get("/playlist/")] +fn read_playlist(db: State, auth: Auth, name: String) -> Result>, errors::Error> { + let songs = playlist::read_playlist(&name, &auth.username, db.deref())?; + Ok(Json(songs)) +} + +#[delete("/playlist/")] +fn delete_playlist(db: State, auth: Auth, name: String) -> Result<(), errors::Error> { + playlist::delete_playlist(&name, &auth.username, db.deref())?; + Ok(()) +}