diff --git a/src/library/playlist.rs b/src/library/playlist.rs index 4679442..05c18a5 100644 --- a/src/library/playlist.rs +++ b/src/library/playlist.rs @@ -5,7 +5,7 @@ use mongod::{ }; use serde::{Deserialize, Serialize}; -use crate::library::{track::Track, user::User}; +use crate::{library::{track::Track, user::User}, route::ToAPI}; #[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] pub struct Playlist { @@ -33,3 +33,14 @@ impl Validate for Playlist { Ok(()) } } + +impl ToAPI for Playlist { + async fn api(&self) -> serde_json::Value { + serde_json::json!({ + "id": self._id, + "owner": self.owner.id(), + "title": self.title, + "tracks": self.tracks.iter().map(|x| x.id()).collect::>() + }) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7df36e6..76a97e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,9 @@ async fn rocket() -> _ { route::user::passwd_route, route::user::user_create_route, route::user::users_route, - route::track::track_audio_opus128_route + route::track::track_audio_opus128_route, + route::playlist::playlists_route, + route::playlist::playlist_route ], ) .manage(lib) diff --git a/src/route/mod.rs b/src/route/mod.rs index c053c15..4a2e01f 100644 --- a/src/route/mod.rs +++ b/src/route/mod.rs @@ -9,6 +9,7 @@ pub mod album; pub mod artist; pub mod track; pub mod user; +pub mod playlist; // todo : rework api diff --git a/src/route/playlist.rs b/src/route/playlist.rs new file mode 100644 index 0000000..cd29fac --- /dev/null +++ b/src/route/playlist.rs @@ -0,0 +1,43 @@ +use super::api_error; +use super::FallibleApiResponse; +use super::ToAPI; +use mongod::Model; +use mongod::Referencable; +use mongodb::bson::doc; +use rocket::*; +use serde_json::json; + +use crate::library::playlist::Playlist; +use crate::library::playlist::Visibility; +use crate::library::user::User; + + +#[get("/playlists")] +pub async fn playlists_route(u: User) -> FallibleApiResponse { + let mut playlists = vec![ + "recent".to_string() + ]; + + let own_playlists = Playlist::find(doc! { "owner": u.reference()}, None).await.unwrap(); + + for playlist in own_playlists { + playlists.push(playlist._id); + } + + Ok(json!(playlists)) +} + +#[get("/playlist/")] +pub async fn playlist_route(id: &str, u: User) -> FallibleApiResponse { + if id == "recents" { + // todo : recently played + } + + let playlist = Playlist::get(id).await.ok_or_else(|| api_error("No playlist with that ID found"))?; + + if matches!(playlist.visibility, Visibility::Private) && u.username != playlist.owner.get::().await.username { + return Err(api_error("Forbidden")); + } + + Ok(playlist.api().await) +} \ No newline at end of file diff --git a/src/route/user.rs b/src/route/user.rs index f878c0f..3dccb40 100644 --- a/src/route/user.rs +++ b/src/route/user.rs @@ -1,7 +1,6 @@ use crate::library::user::Session; use crate::library::user::User; use crate::route::to_api; -use crate::route::ToAPI; use mongod::Model; use mongodb::bson::doc; use rocket::get;