From 952129d45412896dc812a64070315e2a76f0f28e Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 1 Aug 2024 16:58:18 +0200 Subject: [PATCH] singles --- src/library/mod.rs | 24 +++++++++++++++++++----- src/route/album.rs | 4 +++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/library/mod.rs b/src/library/mod.rs index db55af6..38a8dbc 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -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,10 +127,24 @@ impl Libary { } pub async fn get_albums_by_artist(&self, artist: &str) -> Vec { - let artist = format!("artist::{artist}"); - Album::find(doc! { "artist_id": artist}, None) - .await - .unwrap() + 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::find( + doc! { + "album_id": None::, + "artist_id": reference_of!(Artist, artist).unwrap() + }, + None, + ) + .await + .unwrap() } pub async fn get_album_by_id(&self, album: &str) -> Option { diff --git a/src/route/album.rs b/src/route/album.rs index 014e9f8..02adc45 100644 --- a/src/route/album.rs +++ b/src/route/album.rs @@ -16,10 +16,12 @@ use crate::route::to_api; #[get("/artist//albums")] pub async fn albums_route(artist_id: &str, lib: &State) -> 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 })) }