use super::api_error; use super::to_api; use super::FallibleApiResponse; use super::ToAPI; use fs::NamedFile; use mongod::Model; 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(&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(id).await }) .await; NamedFile::open(image?).await.ok() } #[get("/artist/")] pub async fn artist_route(id: &str) -> FallibleApiResponse { Ok(Artist::get(id) .await .ok_or_else(|| api_error("No artist with that ID found"))? .api() .await) }