This commit is contained in:
JMARyA 2024-08-01 16:58:18 +02:00
parent 52482085bc
commit 952129d454
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 22 additions and 6 deletions

View file

@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
use album::Album;
use artist::Artist;
use mongod::{Model, Referencable, Reference};
use mongod::{reference_of, Model, Referencable, Reference};
use mongodb::bson::doc;
use serde_json::json;
use track::Track;
@ -127,8 +127,22 @@ impl Libary {
}
pub async fn get_albums_by_artist(&self, artist: &str) -> Vec<Album> {
let artist = format!("artist::{artist}");
Album::find(doc! { "artist_id": artist}, None)
Album::find(
doc! { "artist_id": reference_of!(Artist, artist).unwrap()},
None,
)
.await
.unwrap()
}
pub async fn get_singles_by_artist(&self, artist: &str) -> Vec<Track> {
Track::find(
doc! {
"album_id": None::<String>,
"artist_id": reference_of!(Artist, artist).unwrap()
},
None,
)
.await
.unwrap()
}

View file

@ -16,10 +16,12 @@ use crate::route::to_api;
#[get("/artist/<artist_id>/albums")]
pub async fn albums_route(artist_id: &str, lib: &State<Libary>) -> FallibleApiResponse {
let albums = lib.get_albums_by_artist(artist_id).await;
let singles = lib.get_singles_by_artist(artist_id).await;
Ok(json!({
"artist": artist_id,
"albums": to_api(&albums).await
"albums": to_api(&albums).await,
"singles": to_api(&singles).await
}))
}