add latest albums

This commit is contained in:
JMARyA 2024-08-13 01:26:55 +02:00
parent c93f0ea403
commit a5d3c14f0c
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 49 additions and 4 deletions

View file

@ -9,6 +9,11 @@ macro_rules! use_api_cache {
return Ok(serde_json::from_str(&ret).unwrap());
}
};
($route:literal, $id:literal, $cache:ident) => {
if let Some(ret) = $cache.get_only($route, $id).await {
return Ok(serde_json::from_str(&ret).unwrap());
}
};
}
pub struct RouteCache {

View file

@ -8,6 +8,8 @@ use serde_json::json;
use track::Track;
use walkdir::WalkDir;
use crate::cache::RouteCache;
pub mod album;
pub mod artist;
pub mod metadata;
@ -164,7 +166,9 @@ impl Libary {
Track::get(track_id).await
}
pub async fn rescan(&self) {
pub async fn rescan(&self, cache: &RouteCache) {
cache.invalidate("albums", "latest").await;
log::info!("Rescanning library");
for entry in WalkDir::new(self.root_dir.clone())
.follow_links(true)

View file

@ -28,8 +28,9 @@ async fn rocket() -> _ {
.expect("error creating CORS options");
let lib = Libary::new("./media".into());
let cache = cache::RouteCache::new();
lib.rescan().await;
lib.rescan(&cache).await;
// create initial admin user
if User::find(doc! { "username": "admin" }, None)
@ -39,8 +40,6 @@ async fn rocket() -> _ {
User::create("admin", "admin", UserRole::Admin).await;
}
let cache = cache::RouteCache::new();
rocket::build()
.mount(
"/",
@ -52,6 +51,7 @@ async fn rocket() -> _ {
route::artist::artist_image_route,
route::album::albums_route,
route::album::album_route,
route::album::latest_albums_route,
route::track::track_route,
route::track::track_audio_route,
route::album::album_cover_route,

View file

@ -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,