synthwave/src/route/artist.rs
2024-10-04 14:38:35 +02:00

39 lines
1 KiB
Rust

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<Libary>) -> FallibleApiResponse {
let artists = lib.get_artists().await;
Ok(serde_json::to_value(&vec_to_api(&artists).await).unwrap())
}
#[get("/artist/<id>/image")]
pub async fn artist_image_route(id: &str, cache: &State<RouteCache>) -> Option<NamedFile> {
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/<id>")]
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)
}