add latest albums
This commit is contained in:
parent
c93f0ea403
commit
a5d3c14f0c
4 changed files with 49 additions and 4 deletions
|
@ -3,6 +3,8 @@ use std::cmp::Ordering;
|
|||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use mongod::Model;
|
||||
use mongod::Referencable;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::fs::NamedFile;
|
||||
use rocket::{get, State};
|
||||
|
@ -10,6 +12,7 @@ use serde_json::json;
|
|||
|
||||
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;
|
||||
|
@ -55,6 +58,39 @@ pub async fn album_cover_route(
|
|||
.ok()
|
||||
}
|
||||
|
||||
#[get("/albums/latest")]
|
||||
pub async fn latest_albums_route(cache: &State<RouteCache>) -> FallibleApiResponse {
|
||||
use_api_cache!("albums", "latest", cache);
|
||||
|
||||
let albums = Album::find(doc! {}, None).await.unwrap();
|
||||
|
||||
let mut albums_tracks = vec![];
|
||||
|
||||
for album in &albums {
|
||||
albums_tracks.push(
|
||||
Track::find_one(doc! { "album_id": album.reference()})
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
let mut joined: Vec<(_, _)> = albums.into_iter().zip(albums_tracks).collect();
|
||||
|
||||
joined.sort_by(|a, b| a.1.date_added.cmp(&b.1.date_added));
|
||||
|
||||
joined.reverse();
|
||||
|
||||
let (albums, _): (Vec<_>, Vec<_>) = joined.into_iter().unzip();
|
||||
|
||||
let albums = to_api(&albums).await;
|
||||
|
||||
cache
|
||||
.insert("albums", "latest", serde_json::to_string(&albums).unwrap())
|
||||
.await;
|
||||
|
||||
Ok(json!(albums))
|
||||
}
|
||||
|
||||
#[get("/album/<album_id>")]
|
||||
pub async fn album_route(
|
||||
album_id: &str,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue