Added API endpoint to save playlist

This commit is contained in:
Antoine Gersant 2017-07-16 13:22:08 -07:00
parent f6e871bb03
commit c582c17f39
3 changed files with 50 additions and 6 deletions

View file

@ -22,9 +22,10 @@ use config::MiscSettings;
use db::{ConnectionSource, DB};
use db::misc_settings;
use errors::*;
use thumbnails::*;
use index;
use playlist;
use user;
use thumbnails::*;
use utils::*;
use vfs::VFSSource;
@ -149,6 +150,19 @@ fn get_endpoints(db: Arc<DB>, index_channel: Arc<Mutex<Sender<index::Command>>>)
auth_api_mount.mount("/trigger_index/", reindex_api_chain);
}
{
let mut playlist_router = Router::new();
let put_db = db.clone();
playlist_router.put("/",
move |request: &mut Request| self::save_playlist(request, put_db.deref()),
"save_playlist");
let mut playlist_api_chain = Chain::new(playlist_router);
let admin_req = AdminRequirement { db: db.clone() };
playlist_api_chain.link_around(admin_req);
auth_api_mount.mount("/playlist/", playlist_api_chain);
}
let mut auth_api_chain = Chain::new(auth_api_mount);
let auth = AuthRequirement { db: db.clone() };
@ -471,3 +485,32 @@ fn trigger_index(channel: &Mutex<Sender<index::Command>>) -> IronResult<Response
};
Ok(Response::with(status::Ok))
}
fn save_playlist(request: &mut Request, db: &DB) -> IronResult<Response> {
let username = match request.extensions.get::<SessionKey>() {
Some(s) => s.username.clone(),
None => return Err(Error::from(ErrorKind::AuthenticationRequired).into()),
};
let input = request.get_ref::<params::Params>().unwrap();
let playlist = match input.find(&["playlist"]) {
Some(&params::Value::String(ref playlist)) => playlist,
_ => return Err(Error::from(ErrorKind::MissingPlaylist).into()),
};
#[derive(Deserialize)]
struct SavePlaylistInput {
name: String,
tracks: Vec<String>,
}
let playlist = match serde_json::from_str::<SavePlaylistInput>(playlist) {
Ok(p) => p,
Err(e) => return Err(IronError::new(e, status::BadRequest)),
};
playlist::save_playlist(&playlist.name, &username, &playlist.tracks, db)?;
Ok(Response::with(status::Ok))
}

View file

@ -38,9 +38,10 @@ error_chain! {
DaemonError {}
AuthenticationRequired {}
AdminPrivilegeRequired {}
MissingConfig {}
MissingUsername {}
MissingPassword {}
MissingConfig {}
MissingPlaylist {}
IncorrectCredentials {}
CannotServeDirectory {}
UnsupportedFileType {}

View file

@ -51,7 +51,7 @@ pub struct NewPlaylistSong {
ordering: i32,
}
fn list_playlists<T>(owner: &str, db: &T) -> Result<Vec<String>>
pub fn list_playlists<T>(owner: &str, db: &T) -> Result<Vec<String>>
where T: ConnectionSource + VFSSource
{
let connection = db.get_connection();
@ -74,7 +74,7 @@ fn list_playlists<T>(owner: &str, db: &T) -> Result<Vec<String>>
}
}
fn save_playlist<T>(name: &str, owner: &str, content: &Vec<String>, db: &T) -> Result<()>
pub fn save_playlist<T>(name: &str, owner: &str, content: &Vec<String>, db: &T) -> Result<()>
where T: ConnectionSource + VFSSource
{
let user: User;
@ -148,7 +148,7 @@ fn save_playlist<T>(name: &str, owner: &str, content: &Vec<String>, db: &T) -> R
Ok(())
}
fn read_playlist<T>(playlist_name: &str, owner: &str, db: &T) -> Result<Vec<Song>>
pub fn read_playlist<T>(playlist_name: &str, owner: &str, db: &T) -> Result<Vec<Song>>
where T: ConnectionSource + VFSSource
{
let vfs = db.get_vfs()?;
@ -207,7 +207,7 @@ fn read_playlist<T>(playlist_name: &str, owner: &str, db: &T) -> Result<Vec<Song
Ok(songs)
}
fn delete_playlist<T>(playlist_name: &str, owner: &str, db: &T) -> Result<()>
pub fn delete_playlist<T>(playlist_name: &str, owner: &str, db: &T) -> Result<()>
where T: ConnectionSource + VFSSource
{
let connection = db.get_connection();