Commit 5bdf7d1c authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Playlist: Allow search results to be sorted

parent fc32781d
......@@ -308,7 +308,9 @@ class IMediaLibrary
virtual MediaSearchAggregate searchMedia( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name ) const = 0;
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual std::vector<AlbumPtr> searchAlbums( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
......
......@@ -677,11 +677,13 @@ MediaSearchAggregate MediaLibrary::searchMedia( const std::string& title,
return res;
}
std::vector<PlaylistPtr> MediaLibrary::searchPlaylists( const std::string& name ) const
std::vector<PlaylistPtr> MediaLibrary::searchPlaylists( const std::string& name,
SortingCriteria sort,
bool desc ) const
{
if ( validateSearchPattern( name ) == false )
return {};
return Playlist::search( this, name );
return Playlist::search( this, name, sort, desc );
}
std::vector<AlbumPtr> MediaLibrary::searchAlbums( const std::string& pattern,
......@@ -715,7 +717,7 @@ SearchAggregate MediaLibrary::search( const std::string& pattern ) const
res.artists = searchArtists( pattern, SortingCriteria::Default, false );
res.genres = searchGenre( pattern );
res.media = searchMedia( pattern, SortingCriteria::Default, false );
res.playlists = searchPlaylists( pattern );
res.playlists = searchPlaylists( pattern, SortingCriteria::Default, false );
return res;
}
......
......@@ -117,7 +117,9 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual MediaSearchAggregate searchMedia( const std::string& title,
SortingCriteria sort, bool desc ) const override;
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name ) const override;
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name,
SortingCriteria sort,
bool desc ) const override;
virtual std::vector<AlbumPtr> searchAlbums( const std::string& pattern,
SortingCriteria sort,
bool desc ) const override;
......
......@@ -260,10 +260,12 @@ void Playlist::createTriggers( sqlite::Connection* dbConn )
sqlite::Tools::executeRequest( dbConn, vtriggerDelete );
}
std::vector<PlaylistPtr> Playlist::search( MediaLibraryPtr ml, const std::string& name )
std::vector<PlaylistPtr> Playlist::search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc )
{
static const std::string req = "SELECT * FROM " + policy::PlaylistTable::Name + " WHERE id_playlist IN "
std::string req = "SELECT * FROM " + policy::PlaylistTable::Name + " WHERE id_playlist IN "
"(SELECT rowid FROM " + policy::PlaylistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')";
req += sortRequest( sort, desc );
return fetchAll<IPlaylist>( ml, req, name );
}
......
......@@ -69,7 +69,8 @@ public:
static void createTable( sqlite::Connection* dbConn );
static void createTriggers( sqlite::Connection* dbConn );
static std::vector<PlaylistPtr> search( MediaLibraryPtr ml, const std::string& name );
static std::vector<PlaylistPtr> search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc );
static std::vector<PlaylistPtr> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc );
/**
......
......@@ -256,34 +256,49 @@ TEST_F( Playlists, Search )
ml->createPlaylist( "playlist 2" );
ml->createPlaylist( "laylist 3" );
auto playlists = ml->searchPlaylists( "play" );
auto playlists = ml->searchPlaylists( "play", SortingCriteria::Default, false );
ASSERT_EQ( 2u, playlists.size() );
}
TEST_F( Playlists, SearchAndSort )
{
auto pl2 = ml->createPlaylist( "playlist 2" );
auto playlists = ml->searchPlaylists( "play", SortingCriteria::Default, false );
ASSERT_EQ( 2u, playlists.size() );
ASSERT_EQ( pl2->id(), playlists[0]->id() );
ASSERT_EQ( pl->id(), playlists[1]->id() );
playlists = ml->searchPlaylists( "play", SortingCriteria::Default, true );
ASSERT_EQ( 2u, playlists.size() );
ASSERT_EQ( pl->id(), playlists[0]->id() );
ASSERT_EQ( pl2->id(), playlists[1]->id() );
}
TEST_F( Playlists, SearchAfterDelete )
{
auto pl = ml->createPlaylist( "sea otters greatest hits" );
auto pls = ml->searchPlaylists( "sea otters" );
auto pls = ml->searchPlaylists( "sea otters", SortingCriteria::Default, false );
ASSERT_EQ( 1u, pls.size() );
ml->deletePlaylist( pl->id() );
pls = ml->searchPlaylists( "sea otters" );
pls = ml->searchPlaylists( "sea otters", SortingCriteria::Default, false );
ASSERT_EQ( 0u, pls.size() );
}
TEST_F( Playlists, SearchAfterUpdate )
{
auto pl = ml->createPlaylist( "sea otters greatest hits" );
auto pls = ml->searchPlaylists( "sea otters" );
auto pls = ml->searchPlaylists( "sea otters", SortingCriteria::Default, false );
ASSERT_EQ( 1u, pls.size() );
pl->setName( "pangolins are cool too" );
pls = ml->searchPlaylists( "sea otters" );
pls = ml->searchPlaylists( "sea otters", SortingCriteria::Default, false );
ASSERT_EQ( 0u, pls.size() );
pls = ml->searchPlaylists( "pangolins" );
pls = ml->searchPlaylists( "pangolins", SortingCriteria::Default, false );
ASSERT_EQ( 1u, pls.size() );
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment