use super::api_error; use super::to_uuid; use super::vec_to_api; use super::FallibleApiResponse; use super::ToAPI; use fs::NamedFile; use mongodb::bson::doc; use rocket::{fs, get, State}; use crate::cache::RouteCache; use crate::library::artist::Artist; use crate::library::Libary; /// Get all artists #[get("/artists")] pub async fn artists_route(lib: &State) -> FallibleApiResponse { let artists = lib.get_artists().await; Ok(serde_json::to_value(&vec_to_api(&artists).await).unwrap()) } #[get("/artist//image")] pub async fn artist_image_route(id: &str, cache: &State) -> Option { let image = cache .get_option("artist_image_route", id, || async { Artist::get_image_of(&to_uuid(id).ok()?).await }) .await; NamedFile::open(image?).await.ok() } #[get("/artist/")] pub async fn artist_route(id: &str) -> FallibleApiResponse { Ok(Artist::get(&to_uuid(id)?) .await .ok_or_else(|| api_error("No artist with that ID found"))? .api() .await) }