2024-07-26 12:14:08 +00:00
|
|
|
use mongod::{
|
|
|
|
assert_reference_of,
|
|
|
|
derive::{Model, Referencable},
|
|
|
|
Referencable, Reference, Validate,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-08-11 02:07:02 +00:00
|
|
|
use crate::{library::{track::Track, user::User}, route::ToAPI};
|
2024-07-26 12:14:08 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
|
|
|
|
pub struct Playlist {
|
|
|
|
pub _id: String,
|
|
|
|
pub owner: Reference,
|
|
|
|
pub title: String,
|
|
|
|
pub visibility: Visibility,
|
|
|
|
pub tracks: Vec<Reference>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
|
|
pub enum Visibility {
|
|
|
|
Private,
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Validate for Playlist {
|
|
|
|
async fn validate(&self) -> Result<(), String> {
|
|
|
|
assert_reference_of!(self.owner, User);
|
|
|
|
|
|
|
|
for track in &self.tracks {
|
|
|
|
assert_reference_of!(track, Track);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2024-08-11 02:07:02 +00:00
|
|
|
|
|
|
|
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::<Vec<_>>()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|