api
This commit is contained in:
parent
95a2a71f25
commit
5d2465353a
8 changed files with 99 additions and 41 deletions
|
@ -2,13 +2,16 @@ use std::cmp::Ordering;
|
|||
|
||||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use mongod::Referencable;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::fs::NamedFile;
|
||||
use rocket::*;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::library::album::Album;
|
||||
use crate::library::Libary;
|
||||
use crate::route::to_api;
|
||||
|
||||
#[get("/artist/<artist_id>/albums")]
|
||||
pub async fn albums_route(artist_id: &str, lib: &State<Libary>) -> FallibleApiResponse {
|
||||
|
@ -16,7 +19,7 @@ pub async fn albums_route(artist_id: &str, lib: &State<Libary>) -> FallibleApiRe
|
|||
|
||||
Ok(json!({
|
||||
"artist": artist_id,
|
||||
"albums": &albums
|
||||
"albums": to_api(&albums).await
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -31,24 +34,7 @@ fn sort_by_tracknumber(a: &serde_json::Value, b: &serde_json::Value) -> Ordering
|
|||
#[get("/album/<album_id>/cover")]
|
||||
pub async fn album_cover_route(album_id: &str, lib: &State<Libary>) -> Option<NamedFile> {
|
||||
let album = lib.get_album_by_id(album_id).await?;
|
||||
|
||||
let track_path = lib
|
||||
.get_tracks_of_album(&format!("album::{}", album._id))
|
||||
.await
|
||||
.first()?
|
||||
.path
|
||||
.clone();
|
||||
let track_path = std::path::Path::new(&track_path);
|
||||
|
||||
for ext in ["png", "jpg", "jpeg", "avif"] {
|
||||
let cover_file = track_path.parent()?.join(format!("cover.{ext}"));
|
||||
|
||||
if cover_file.exists() {
|
||||
return NamedFile::open(cover_file).await.ok();
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
NamedFile::open(album.get_cover().await?).await.ok()
|
||||
}
|
||||
|
||||
#[get("/album/<album_id>")]
|
||||
|
@ -58,8 +44,7 @@ pub async fn album_route(album_id: &str, lib: &State<Libary>) -> FallibleApiResp
|
|||
.await
|
||||
.ok_or_else(|| api_error("No album with that ID found"))?;
|
||||
|
||||
let mut tracks = lib
|
||||
.get_tracks_of_album(&format!("album::{}", album._id))
|
||||
let mut tracks = Album::get_tracks_of_album(&format!("album::{}", album._id))
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|x| {
|
||||
|
@ -73,7 +58,7 @@ pub async fn album_route(album_id: &str, lib: &State<Libary>) -> FallibleApiResp
|
|||
|
||||
tracks.sort_by(sort_by_tracknumber);
|
||||
|
||||
let mut album = serde_json::to_value(album).unwrap();
|
||||
let mut album = album.api().await;
|
||||
album
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
|
|
|
@ -1,23 +1,26 @@
|
|||
use super::api_error;
|
||||
use super::to_api;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use mongod::Model;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::*;
|
||||
|
||||
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(&artists).unwrap())
|
||||
Ok(serde_json::to_value(&to_api(&artists).await).unwrap())
|
||||
}
|
||||
|
||||
#[get("/artist/<id>")]
|
||||
pub async fn artist_route(id: &str, lib: &State<Libary>) -> FallibleApiResponse {
|
||||
Ok(serde_json::to_value(
|
||||
&lib.get_artist_by_id(id)
|
||||
.await
|
||||
.ok_or_else(|| api_error("No artist with that ID found"))?,
|
||||
)
|
||||
.unwrap())
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ pub mod artist;
|
|||
pub mod track;
|
||||
pub mod user;
|
||||
|
||||
// todo : rework api
|
||||
|
||||
type ApiError = BadRequest<serde_json::Value>;
|
||||
type FallibleApiResponse = Result<serde_json::Value, ApiError>;
|
||||
|
||||
|
@ -14,3 +16,18 @@ pub fn api_error(msg: &str) -> ApiError {
|
|||
"error": msg
|
||||
}))
|
||||
}
|
||||
|
||||
pub trait ToAPI: Sized {
|
||||
/// Generate public API JSON
|
||||
fn api(&self) -> impl std::future::Future<Output = serde_json::Value>;
|
||||
}
|
||||
|
||||
pub async fn to_api(albums: &[impl ToAPI]) -> Vec<serde_json::Value> {
|
||||
let mut ret = Vec::new();
|
||||
|
||||
for e in albums {
|
||||
ret.push(e.api().await);
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use fs::NamedFile;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue