2024-07-26 12:14:08 +00:00
|
|
|
use super::api_error;
|
|
|
|
use super::FallibleApiResponse;
|
2024-08-07 21:07:08 +00:00
|
|
|
use fs::NamedFile;
|
2024-09-19 14:44:44 +00:00
|
|
|
use mongod::vec_to_api;
|
2024-08-01 14:36:54 +00:00
|
|
|
use mongod::Model;
|
2024-09-19 14:44:44 +00:00
|
|
|
use mongod::ToAPI;
|
2024-07-24 09:07:24 +00:00
|
|
|
use mongodb::bson::doc;
|
2024-08-12 16:48:33 +00:00
|
|
|
use rocket::{fs, get, State};
|
2024-07-24 09:07:24 +00:00
|
|
|
|
2024-08-08 11:06:02 +00:00
|
|
|
use crate::cache::RouteCache;
|
2024-08-01 14:36:54 +00:00
|
|
|
use crate::library::artist::Artist;
|
2024-07-24 09:07:24 +00:00
|
|
|
use crate::library::Libary;
|
|
|
|
|
|
|
|
/// Get all artists
|
|
|
|
#[get("/artists")]
|
|
|
|
pub async fn artists_route(lib: &State<Libary>) -> FallibleApiResponse {
|
|
|
|
let artists = lib.get_artists().await;
|
2024-09-19 14:44:44 +00:00
|
|
|
Ok(serde_json::to_value(&vec_to_api(&artists).await).unwrap())
|
2024-07-24 09:07:24 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 21:07:08 +00:00
|
|
|
#[get("/artist/<id>/image")]
|
2024-08-08 11:06:02 +00:00
|
|
|
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(id).await
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
NamedFile::open(image?).await.ok()
|
2024-08-07 21:07:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 09:07:24 +00:00
|
|
|
#[get("/artist/<id>")]
|
2024-08-01 14:36:54 +00:00
|
|
|
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)
|
2024-07-24 09:07:24 +00:00
|
|
|
}
|