diff --git a/src/api.rs b/src/api.rs index 30f1156..92666f1 100644 --- a/src/api.rs +++ b/src/api.rs @@ -19,7 +19,7 @@ use thumbnails::*; use utils::*; const CURRENT_MAJOR_VERSION: i32 = 1; -const CURRENT_MINOR_VERSION: i32 = 0; +const CURRENT_MINOR_VERSION: i32 = 1; #[derive(RustcEncodable)] struct Version { @@ -60,6 +60,12 @@ pub fn get_api_handler(collection: Arc) -> Mount { self::flatten(request, collection.deref()) }); } + { + let collection = collection.clone(); + auth_api_mount.mount("/random/", move |request: &mut Request| { + self::random(request, collection.deref()) + }); + } { let collection = collection.clone(); auth_api_mount.mount("/serve/", move |request: &mut Request| { @@ -154,6 +160,16 @@ fn flatten(request: &mut Request, collection: &Collection) -> IronResult IronResult { + let random_result = collection.get_random_albums(20)?; + let result_json = json::encode(&random_result); + let result_json = match result_json { + Ok(j) => j, + Err(e) => return Err(IronError::new(e, status::InternalServerError)), + }; + Ok(Response::with((status::Ok, result_json))) +} + fn serve(request: &mut Request, collection: &Collection) -> IronResult { let virtual_path = path_from_request(request); let virtual_path = match virtual_path { diff --git a/src/collection.rs b/src/collection.rs index e33c388..abef15c 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -56,6 +56,10 @@ impl Collection { self.index.deref().flatten(virtual_path) } + pub fn get_random_albums(&self, count: u32) -> Result> { + self.index.deref().get_random_albums(count) + } + pub fn locate(&self, virtual_path: &Path) -> Result { self.vfs.virtual_to_real(virtual_path) } diff --git a/src/index.rs b/src/index.rs index 953f83a..0719b3d 100644 --- a/src/index.rs +++ b/src/index.rs @@ -480,18 +480,9 @@ impl Index { Ok(output) } - // List sub-directories within a directory - fn browse_directories(&self, real_path: &Path) -> Result> { - let db = self.connect()?; - let mut output = Vec::new(); - - let path_string = real_path.to_string_lossy(); - let mut select = - db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \ - parent = ? ORDER BY path COLLATE NOCASE ASC")?; - select.bind(1, &Value::String(path_string.deref().to_owned()))?; - - while let State::Row = select.next()? { + fn select_directories(&self, select: &mut Statement) -> Result> { + let mut output = Vec::new(); + while let State::Row = select.next()? { let directory_value: String = select.read(0)?; let artwork_path: Value = select.read(1)?; @@ -521,9 +512,24 @@ impl Index { artist: artist.as_string().map(|s| s.to_owned()), album: album.as_string().map(|s| s.to_owned()), }; - output.push(CollectionFile::Directory(directory)); + output.push(directory); } + Ok(output) + } + + // List sub-directories within a directory + fn browse_directories(&self, real_path: &Path) -> Result> { + let db = self.connect()?; + + let path_string = real_path.to_string_lossy(); + let mut select = + db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \ + parent = ? ORDER BY path COLLATE NOCASE ASC")?; + select.bind(1, &Value::String(path_string.deref().to_owned()))?; + + let output = self.select_directories(&mut select)?; + let output = output.into_iter().map(|d| CollectionFile::Directory(d)).collect(); Ok(output) } @@ -579,6 +585,14 @@ impl Index { select.bind(1, &Value::String(path_string.deref().to_owned()))?; self.select_songs(&mut select) } + + pub fn get_random_albums(&self, count: u32) -> Result> { + let db = self.connect()?; + let mut select = + db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album IS NOT NULL ORDER BY RANDOM() LIMIT ?")?; + select.bind(1, &Value::Integer(count as i64))?; + self.select_directories(&mut select) + } } fn _get_test_index(name: &str) -> Index { @@ -683,3 +697,11 @@ fn test_flatten() { let results = index.flatten(Path::new("root")).unwrap(); assert_eq!(results.len(), 12); } + +#[test] +fn test_random() { + let index = _get_test_index("random.sqlite"); + index.update_index().unwrap(); + let results = index.get_random_albums(1).unwrap(); + assert_eq!(results.len(), 1); +}