Commit a21b8aa3 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Provide playlists sorting

parent b4f18e72
......@@ -157,7 +157,7 @@ class IMediaLibrary
* Playlists
*/
virtual PlaylistPtr createPlaylist( const std::string& name ) = 0;
virtual std::vector<PlaylistPtr> playlists() = 0;
virtual std::vector<PlaylistPtr> playlists( medialibrary::SortingCriteria sort, bool desc ) = 0;
virtual bool deletePlaylist( unsigned int playlistId ) = 0;
/**
......
......@@ -383,9 +383,9 @@ PlaylistPtr MediaLibrary::createPlaylist( const std::string& name )
return Playlist::create( this, name );
}
std::vector<PlaylistPtr> MediaLibrary::playlists()
std::vector<PlaylistPtr> MediaLibrary::playlists(medialibrary::SortingCriteria sort, bool desc)
{
return Playlist::fetchAll<IPlaylist>( this );
return Playlist::listAll( this, sort, desc );
}
bool MediaLibrary::deletePlaylist( unsigned int playlistId )
......
......@@ -85,7 +85,7 @@ class MediaLibrary : public IMediaLibrary
virtual std::vector<ArtistPtr> artists( medialibrary::SortingCriteria sort, bool desc ) const override;
virtual PlaylistPtr createPlaylist( const std::string& name ) override;
virtual std::vector<PlaylistPtr> playlists() override;
virtual std::vector<PlaylistPtr> playlists( medialibrary::SortingCriteria sort, bool desc ) override;
virtual bool deletePlaylist( unsigned int playlistId ) override;
virtual bool addToHistory( const std::string& mrl );
......
......@@ -204,3 +204,20 @@ std::vector<PlaylistPtr> Playlist::search( MediaLibraryPtr ml, const std::string
"(SELECT rowid FROM " + policy::PlaylistTable::Name + "Fts WHERE name MATCH ?)";
return fetchAll<IPlaylist>( ml, req, name + "*" );
}
std::vector<PlaylistPtr> Playlist::listAll( MediaLibraryPtr ml, medialibrary::SortingCriteria sort, bool desc )
{
std::string req = "SELECT * FROM " + policy::PlaylistTable::Name + " ORDER BY ";
switch ( sort )
{
case medialibrary::SortingCriteria::InsertionDate:
req += "creation_date";
break;
default:
req += "name";
break;
}
if ( desc == true )
req += " DESC";
return fetchAll<IPlaylist>( ml, req );
}
......@@ -61,6 +61,7 @@ public:
static bool createTable( DBConnection dbConn );
static bool createTriggers( DBConnection dbConn );
static std::vector<PlaylistPtr> search( MediaLibraryPtr ml, const std::string& name );
static std::vector<PlaylistPtr> listAll( MediaLibraryPtr ml, medialibrary::SortingCriteria sort, bool desc );
private:
MediaLibraryPtr m_ml;
......
......@@ -52,7 +52,7 @@ TEST_F( Playlists, Fetch )
ASSERT_NE( nullptr, pl2 );
ASSERT_EQ( pl->id(), pl2->id() );
auto playlists = ml->playlists();
auto playlists = ml->playlists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, playlists.size() );
ASSERT_EQ( pl->id(), playlists[0]->id() );
}
......@@ -62,7 +62,7 @@ TEST_F( Playlists, DeletePlaylist )
{
auto res = ml->deletePlaylist( pl->id() );
ASSERT_TRUE( res );
auto playlists = ml->playlists();
auto playlists = ml->playlists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 0u, playlists.size() );
}
......@@ -87,7 +87,7 @@ TEST_F( Playlists, FetchAll )
ml->createPlaylist( "pl 3" );
ml->createPlaylist( "pl 4" );
auto playlists = ml->playlists();
auto playlists = ml->playlists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 4u, playlists.size() );
for ( auto& p : playlists )
{
......@@ -276,3 +276,18 @@ TEST_F( Playlists, SearchAfterUpdate )
pls = ml->searchPlaylists( "pangolins" );
ASSERT_EQ( 1u, pls.size() );
}
TEST_F( Playlists, Sort )
{
auto pl2 = ml->createPlaylist( "A playlist" );
auto pls = ml->playlists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 2u, pls.size() );
ASSERT_EQ( pl2->id(), pls[0]->id() );
ASSERT_EQ( pl->id(), pls[1]->id() );
pls = ml->playlists( medialibrary::SortingCriteria::Default, true );
ASSERT_EQ( 2u, pls.size() );
ASSERT_EQ( pl2->id(), pls[1]->id() );
ASSERT_EQ( pl->id(), pls[0]->id() );
}
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