Skip to content

Commit

Permalink
Adds support for offset and count parameters in get_recent_albums
Browse files Browse the repository at this point in the history
  • Loading branch information
agersant committed Sep 18, 2024
1 parent 6bd0c25 commit ae4200c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/app/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/app/index/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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!(
Expand Down
17 changes: 10 additions & 7 deletions src/server/axum/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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>,
Expand All @@ -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(),
};
Expand Down
6 changes: 6 additions & 0 deletions src/server/dto/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ae4200c

Please sign in to comment.