Adds support for offset and count parameters in get_recent_albums

This commit is contained in:
Antoine Gersant 2024-09-17 22:27:39 -07:00
parent 6bd0c25d7d
commit ae4200c6ce
4 changed files with 32 additions and 11 deletions
src

View file

@ -190,12 +190,18 @@ impl Manager {
.unwrap()
}
pub async fn get_recent_albums(&self, count: usize) -> Result<Vec<Album>, Error> {
pub async fn get_recent_albums(
&self,
offset: usize,
count: usize,
) -> Result<Vec<Album>, Error> {
spawn_blocking({
let index_manager = self.clone();
move || {
let index = index_manager.index.read().unwrap();
Ok(index.collection.get_recent_albums(&index.strings, count))
Ok(index
.collection
.get_recent_albums(&index.strings, offset, count))
}
})
.await

View file

@ -149,9 +149,15 @@ impl Collection {
.collect()
}
pub fn get_recent_albums(&self, strings: &RodeoReader, count: usize) -> Vec<Album> {
pub fn get_recent_albums(
&self,
strings: &RodeoReader,
offset: usize,
count: usize,
) -> Vec<Album> {
self.recent_albums
.iter()
.skip(offset)
.take(count)
.filter_map(|k| self.get_album(strings, k.clone()))
.collect()
@ -566,7 +572,7 @@ mod test {
},
]));
let albums = collection.get_recent_albums(&strings, 10);
let albums = collection.get_recent_albums(&strings, 0, 10);
assert_eq!(albums.len(), 2);
assert_eq!(

View file

@ -54,13 +54,13 @@ pub fn router() -> Router<App> {
.route("/flatten/*path", get(get_flatten))
// Semantic
.route("/albums", get(get_albums))
.route("/albums/recent", get(get_recent))
.route("/albums/random", get(get_random))
.route("/albums/recent", get(get_recent_albums))
.route("/albums/random", get(get_random_albums))
.route("/artists", get(get_artists))
.route("/artists/:artist", get(get_artist))
.route("/artists/:artists/albums/:name", get(get_album))
.route("/random", get(get_random)) // Deprecated
.route("/recent", get(get_recent)) // Deprecated
.route("/random", get(get_random_albums)) // Deprecated
.route("/recent", get(get_recent_albums)) // Deprecated
// Search
.route("/search", get(get_search_root))
.route("/search/*query", get(get_search))
@ -474,7 +474,7 @@ async fn get_peaks(
Ok(peaks.interleaved)
}
async fn get_random(
async fn get_random_albums(
_auth: Auth,
api_version: APIMajorVersion,
State(index_manager): State<index::Manager>,
@ -486,12 +486,15 @@ async fn get_random(
albums_to_response(albums, api_version)
}
async fn get_recent(
async fn get_recent_albums(
_auth: Auth,
api_version: APIMajorVersion,
State(index_manager): State<index::Manager>,
Query(option): Query<dto::GetRecentAlbumsParameters>,
) -> Response {
let albums = match index_manager.get_recent_albums(20).await {
let offset = option.offset.unwrap_or(0);
let count = option.count.unwrap_or(20);
let albums = match index_manager.get_recent_albums(offset, count).await {
Ok(d) => d,
Err(e) => return APIError::from(e).into_response(),
};

View file

@ -436,4 +436,10 @@ pub struct GetSongsBulkOutput {
pub not_found: Vec<PathBuf>,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct GetRecentAlbumsParameters {
pub offset: Option<usize>,
pub count: Option<usize>,
}
// TODO: Preferences should have dto types