api
This commit is contained in:
parent
95a2a71f25
commit
5d2465353a
8 changed files with 99 additions and 41 deletions
|
@ -3,9 +3,13 @@ use mongod::{
|
|||
derive::{Model, Referencable},
|
||||
Model, Referencable, Reference, Validate,
|
||||
};
|
||||
use mongodb::bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::library::artist::Artist;
|
||||
use crate::{library::artist::Artist, route::ToAPI};
|
||||
|
||||
use super::track::Track;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
|
||||
pub struct Album {
|
||||
|
@ -28,6 +32,44 @@ impl Album {
|
|||
a.insert().await.unwrap();
|
||||
a
|
||||
}
|
||||
|
||||
pub async fn get_tracks_of_album(album: &str) -> Vec<Track> {
|
||||
Track::find(doc! { "album_id": album}, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_cover(&self) -> Option<String> {
|
||||
let track_path = Self::get_tracks_of_album(&format!("album::{}", self._id))
|
||||
.await
|
||||
.first()?
|
||||
.path
|
||||
.clone();
|
||||
let track_path = std::path::Path::new(&track_path);
|
||||
|
||||
for ext in ["png", "jpg", "jpeg", "avif"] {
|
||||
let cover_file = track_path.parent()?.join(format!("cover.{ext}"));
|
||||
|
||||
if cover_file.exists() {
|
||||
return Some(cover_file.to_str().unwrap().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl ToAPI for Album {
|
||||
async fn api(&self) -> serde_json::Value {
|
||||
json!({
|
||||
"id": &self._id,
|
||||
"title": &self.title,
|
||||
"artist": self.artist_id.as_ref().map(|x| x.id()),
|
||||
"cover_url": if self.get_cover().await.is_some() {
|
||||
Some(format!("/album/{}/cover", self._id))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Validate for Album {
|
||||
|
|
|
@ -3,6 +3,9 @@ use mongod::{
|
|||
Model, Validate,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::route::ToAPI;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
|
||||
pub struct Artist {
|
||||
|
@ -21,6 +24,15 @@ impl Artist {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToAPI for Artist {
|
||||
async fn api(&self) -> serde_json::Value {
|
||||
json!({
|
||||
"id": &self._id,
|
||||
"name": &self.name
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Validate for Artist {
|
||||
async fn validate(&self) -> Result<(), String> {
|
||||
Ok(())
|
||||
|
|
|
@ -106,6 +106,7 @@ impl Libary {
|
|||
.insert("meta".into(), metadata.0);
|
||||
}
|
||||
|
||||
// if no title in metadata use file name
|
||||
if entry.as_object().unwrap().get("title").is_none() {
|
||||
entry.as_object_mut().unwrap().insert(
|
||||
"title".into(),
|
||||
|
@ -125,10 +126,6 @@ impl Libary {
|
|||
Artist::find(doc! {}, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_artist_by_id(&self, id: &str) -> Option<Artist> {
|
||||
Artist::get(id).await
|
||||
}
|
||||
|
||||
pub async fn get_albums_by_artist(&self, artist: &str) -> Vec<Album> {
|
||||
let artist = format!("artist::{artist}");
|
||||
Album::find(doc! { "artist_id": artist}, None)
|
||||
|
@ -140,10 +137,6 @@ impl Libary {
|
|||
Album::get(album).await
|
||||
}
|
||||
|
||||
pub async fn get_tracks_of_album(&self, album: &str) -> Vec<Track> {
|
||||
Track::find(doc! { "album_id": album}, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_track_by_id(&self, track_id: &str) -> Option<Track> {
|
||||
Track::get(track_id).await
|
||||
}
|
||||
|
|
|
@ -6,7 +6,10 @@ use mongod::{
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::library::{album::Album, artist::Artist};
|
||||
use crate::{
|
||||
library::{album::Album, artist::Artist},
|
||||
route::ToAPI,
|
||||
};
|
||||
|
||||
use super::metadata::AudioMetadata;
|
||||
|
||||
|
@ -59,8 +62,10 @@ impl Track {
|
|||
|
||||
transcoded
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn api(&self) -> serde_json::Value {
|
||||
impl ToAPI for Track {
|
||||
async fn api(&self) -> serde_json::Value {
|
||||
let album_title = if let Some(album_ref) = &self.album_id {
|
||||
album_ref
|
||||
.get_partial::<Album>(json!({"title": 1}))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue