Implemented search

This commit is contained in:
Antoine Gersant 2017-11-03 13:42:42 -07:00
parent 24c6fcb4f4
commit 62688a8402
2 changed files with 41 additions and 4 deletions

View file

@ -435,7 +435,10 @@ fn recent(_: &mut Request, db: &DB) -> IronResult<Response> {
}
fn search(request: &mut Request, db: &DB) -> IronResult<Response> {
let query = "Stratovarius"; // TODO
let query = request
.url
.path()
.join(&::std::path::MAIN_SEPARATOR.to_string());
let search_result = index::search(db, &query)?;
let result_json = serde_json::to_string(&search_result);
let result_json = match result_json {

View file

@ -579,12 +579,46 @@ pub fn get_recent_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>>
pub fn search<T>(db: &T, query: &str) -> Result<Vec<CollectionFile>>
where T: ConnectionSource + VFSSource
{
Ok(vec![])
let vfs = db.get_vfs()?;
let connection = db.get_connection();
let like_test = format!("%{}%", query);
let mut output = Vec::new();
// Find dirs with matching path and parent not matching
{
use self::directories::dsl::*;
let real_directories: Vec<Directory> = directories
.filter(path.like(&like_test))
.filter(parent.not_like(&like_test))
.load(connection.deref())?;
let virtual_directories = real_directories
.into_iter()
.filter_map(|s| virtualize_directory(&vfs, s));
// Find songs with matching title/album/artist
// Remove songs within a matched directory
output.extend(virtual_directories.map(|d| CollectionFile::Directory(d)));
}
// Find songs with matching title/album/artist and non-matching parent
{
use self::songs::dsl::*;
let real_songs: Vec<Song> = songs
.filter(path.like(&like_test)
.or(title.like(&like_test))
.or(album.like(&like_test))
.or(artist.like(&like_test))
.or(album_artist.like(&like_test)))
.filter(parent.not_like(&like_test))
.load(connection.deref())?;
let virtual_songs = real_songs
.into_iter()
.filter_map(|s| virtualize_song(&vfs, s));
output.extend(virtual_songs.map(|s| CollectionFile::Song(s)));
}
Ok(output)
}
#[test]