Adds support for offset and count parameters in get_recent_albums
This commit is contained in:
parent
6bd0c25d7d
commit
ae4200c6ce
4 changed files with 32 additions and 11 deletions
|
@ -190,12 +190,18 @@ impl Manager {
|
||||||
.unwrap()
|
.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({
|
spawn_blocking({
|
||||||
let index_manager = self.clone();
|
let index_manager = self.clone();
|
||||||
move || {
|
move || {
|
||||||
let index = index_manager.index.read().unwrap();
|
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
|
.await
|
||||||
|
|
|
@ -149,9 +149,15 @@ impl Collection {
|
||||||
.collect()
|
.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
|
self.recent_albums
|
||||||
.iter()
|
.iter()
|
||||||
|
.skip(offset)
|
||||||
.take(count)
|
.take(count)
|
||||||
.filter_map(|k| self.get_album(strings, k.clone()))
|
.filter_map(|k| self.get_album(strings, k.clone()))
|
||||||
.collect()
|
.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!(albums.len(), 2);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
@ -54,13 +54,13 @@ pub fn router() -> Router<App> {
|
||||||
.route("/flatten/*path", get(get_flatten))
|
.route("/flatten/*path", get(get_flatten))
|
||||||
// Semantic
|
// Semantic
|
||||||
.route("/albums", get(get_albums))
|
.route("/albums", get(get_albums))
|
||||||
.route("/albums/recent", get(get_recent))
|
.route("/albums/recent", get(get_recent_albums))
|
||||||
.route("/albums/random", get(get_random))
|
.route("/albums/random", get(get_random_albums))
|
||||||
.route("/artists", get(get_artists))
|
.route("/artists", get(get_artists))
|
||||||
.route("/artists/:artist", get(get_artist))
|
.route("/artists/:artist", get(get_artist))
|
||||||
.route("/artists/:artists/albums/:name", get(get_album))
|
.route("/artists/:artists/albums/:name", get(get_album))
|
||||||
.route("/random", get(get_random)) // Deprecated
|
.route("/random", get(get_random_albums)) // Deprecated
|
||||||
.route("/recent", get(get_recent)) // Deprecated
|
.route("/recent", get(get_recent_albums)) // Deprecated
|
||||||
// Search
|
// Search
|
||||||
.route("/search", get(get_search_root))
|
.route("/search", get(get_search_root))
|
||||||
.route("/search/*query", get(get_search))
|
.route("/search/*query", get(get_search))
|
||||||
|
@ -474,7 +474,7 @@ async fn get_peaks(
|
||||||
Ok(peaks.interleaved)
|
Ok(peaks.interleaved)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_random(
|
async fn get_random_albums(
|
||||||
_auth: Auth,
|
_auth: Auth,
|
||||||
api_version: APIMajorVersion,
|
api_version: APIMajorVersion,
|
||||||
State(index_manager): State<index::Manager>,
|
State(index_manager): State<index::Manager>,
|
||||||
|
@ -486,12 +486,15 @@ async fn get_random(
|
||||||
albums_to_response(albums, api_version)
|
albums_to_response(albums, api_version)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_recent(
|
async fn get_recent_albums(
|
||||||
_auth: Auth,
|
_auth: Auth,
|
||||||
api_version: APIMajorVersion,
|
api_version: APIMajorVersion,
|
||||||
State(index_manager): State<index::Manager>,
|
State(index_manager): State<index::Manager>,
|
||||||
|
Query(option): Query<dto::GetRecentAlbumsParameters>,
|
||||||
) -> Response {
|
) -> 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,
|
Ok(d) => d,
|
||||||
Err(e) => return APIError::from(e).into_response(),
|
Err(e) => return APIError::from(e).into_response(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -436,4 +436,10 @@ pub struct GetSongsBulkOutput {
|
||||||
pub not_found: Vec<PathBuf>,
|
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
|
// TODO: Preferences should have dto types
|
||||||
|
|
Loading…
Add table
Reference in a new issue