update
This commit is contained in:
parent
abaa9f12c9
commit
b00424471c
15 changed files with 52 additions and 68 deletions
|
@ -1,5 +1,6 @@
|
|||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use mongod::vec_to_api;
|
||||
use mongod::Model;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::{get, State};
|
||||
|
@ -9,7 +10,6 @@ use crate::check_admin;
|
|||
use crate::library::track::Track;
|
||||
use crate::library::user::User;
|
||||
use crate::library::Libary;
|
||||
use crate::route::to_api;
|
||||
|
||||
#[get("/library/clean")]
|
||||
pub async fn clean_library(lib: &State<Libary>, u: User) -> FallibleApiResponse {
|
||||
|
@ -29,12 +29,12 @@ pub async fn get_singles_route(u: User) -> FallibleApiResponse {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(json!(to_api(&singles).await))
|
||||
Ok(json!(vec_to_api(&singles).await))
|
||||
}
|
||||
|
||||
#[get("/library/orphans")]
|
||||
pub async fn get_orphans_route(u: User) -> FallibleApiResponse {
|
||||
check_admin!(u);
|
||||
let orphans = Track::get_orphans().await;
|
||||
Ok(json!(to_api(&orphans).await))
|
||||
Ok(json!(vec_to_api(&orphans).await))
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::cmp::Ordering;
|
|||
|
||||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use mongod::vec_to_api;
|
||||
use mongod::Model;
|
||||
use mongod::Referencable;
|
||||
use mongodb::bson::doc;
|
||||
|
@ -14,8 +14,8 @@ use crate::cache::RouteCache;
|
|||
use crate::library::album::Album;
|
||||
use crate::library::track::Track;
|
||||
use crate::library::Libary;
|
||||
use crate::route::to_api;
|
||||
use crate::use_api_cache;
|
||||
use mongod::ToAPI;
|
||||
|
||||
#[get("/artist/<artist_id>/albums")]
|
||||
pub async fn albums_route(artist_id: &str, lib: &State<Libary>) -> FallibleApiResponse {
|
||||
|
@ -24,8 +24,8 @@ pub async fn albums_route(artist_id: &str, lib: &State<Libary>) -> FallibleApiRe
|
|||
|
||||
Ok(json!({
|
||||
"artist": artist_id,
|
||||
"albums": to_api(&albums).await,
|
||||
"singles": to_api(&singles).await
|
||||
"albums": vec_to_api(&albums).await,
|
||||
"singles": vec_to_api(&singles).await
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ pub async fn latest_albums_route(cache: &State<RouteCache>) -> FallibleApiRespon
|
|||
|
||||
let (albums, _): (Vec<_>, Vec<_>) = joined.into_iter().unzip();
|
||||
|
||||
let albums = to_api(&albums).await;
|
||||
let albums = vec_to_api(&albums).await;
|
||||
|
||||
cache
|
||||
.insert("albums", "latest", serde_json::to_string(&albums).unwrap())
|
||||
|
@ -106,7 +106,7 @@ pub async fn album_route(
|
|||
|
||||
let tracks = Album::get_tracks_of_album(&format!("album::{}", album._id)).await;
|
||||
|
||||
let mut tracks = to_api(&tracks).await;
|
||||
let mut tracks = vec_to_api(&tracks).await;
|
||||
|
||||
tracks.sort_by(sort_by_tracknumber);
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use super::api_error;
|
||||
use super::to_api;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use fs::NamedFile;
|
||||
use mongod::vec_to_api;
|
||||
use mongod::Model;
|
||||
use mongod::ToAPI;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::{fs, get, State};
|
||||
|
||||
|
@ -15,7 +15,7 @@ use crate::library::Libary;
|
|||
#[get("/artists")]
|
||||
pub async fn artists_route(lib: &State<Libary>) -> FallibleApiResponse {
|
||||
let artists = lib.get_artists().await;
|
||||
Ok(serde_json::to_value(&to_api(&artists).await).unwrap())
|
||||
Ok(serde_json::to_value(&vec_to_api(&artists).await).unwrap())
|
||||
}
|
||||
|
||||
#[get("/artist/<id>/image")]
|
||||
|
|
|
@ -25,21 +25,6 @@ pub fn api_error(msg: &str) -> ApiError {
|
|||
}))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
pub fn index_redir() -> Redirect {
|
||||
Redirect::to(uri!("/web"))
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::library::track::Track;
|
||||
use crate::library::user::User;
|
||||
use mongod::reference_of;
|
||||
use mongod::vec_to_api;
|
||||
use mongod::Model;
|
||||
use mongod::Referencable;
|
||||
use mongodb::bson::doc;
|
||||
|
@ -14,8 +15,7 @@ use crate::library::playlist::Visibility;
|
|||
use crate::route::FallibleApiResponse;
|
||||
|
||||
use super::api_error;
|
||||
use super::to_api;
|
||||
use super::ToAPI;
|
||||
use mongod::ToAPI;
|
||||
|
||||
#[get("/playlists")]
|
||||
pub async fn playlists_route(u: User) -> FallibleApiResponse {
|
||||
|
@ -42,7 +42,7 @@ pub async fn recently_added_playlist() -> FallibleApiResponse {
|
|||
let tracks = Track::find(doc! {}, Some(90), Some(doc! { "date_added": -1 }))
|
||||
.await
|
||||
.unwrap();
|
||||
Ok(json!(to_api(&tracks).await))
|
||||
Ok(json!(vec_to_api(&tracks).await))
|
||||
}
|
||||
|
||||
#[get("/playlist/<id>")]
|
||||
|
@ -88,7 +88,7 @@ pub async fn playlist_route(id: &str, u: User) -> FallibleApiResponse {
|
|||
pub async fn playlist_tracks_route(id: &str, u: User) -> FallibleApiResponse {
|
||||
if id == "recents" {
|
||||
let tracks = Track::get_latest_of_user(&u).await;
|
||||
return Ok(json!(to_api(&tracks).await));
|
||||
return Ok(json!(vec_to_api(&tracks).await));
|
||||
}
|
||||
|
||||
if id == "recentlyAdded" {
|
||||
|
@ -111,7 +111,7 @@ pub async fn playlist_tracks_route(id: &str, u: User) -> FallibleApiResponse {
|
|||
tracks.push(track.get().await);
|
||||
}
|
||||
|
||||
Ok(json!(to_api(&tracks).await))
|
||||
Ok(json!(vec_to_api(&tracks).await))
|
||||
}
|
||||
|
||||
#[derive(rocket::serde::Deserialize)]
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use crate::library::user::User;
|
||||
use fs::NamedFile;
|
||||
use mongod::ToAPI;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::{fs, get, State};
|
||||
use serde_json::json;
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use crate::library::user::Session;
|
||||
use crate::library::user::User;
|
||||
use crate::route::to_api;
|
||||
use mongod::vec_to_api;
|
||||
use mongod::Model;
|
||||
use mongod::ToAPI;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::get;
|
||||
use rocket::http::Status;
|
||||
|
@ -83,7 +84,7 @@ pub async fn passwd_route(passwd: Json<PasswdData>, u: User) -> FallibleApiRespo
|
|||
pub async fn users_route(u: User) -> FallibleApiResponse {
|
||||
check_admin!(u);
|
||||
|
||||
let users: Vec<_> = to_api(&User::find_all().await.unwrap()).await;
|
||||
let users: Vec<_> = vec_to_api(&User::find_all().await.unwrap()).await;
|
||||
|
||||
Ok(json!({"users": users}))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue