Commit 4abba9b2 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Allow artists to be sorted

parent e0a6a71f
......@@ -138,7 +138,14 @@ class IMediaLibrary
virtual ShowPtr show( const std::string& name ) = 0;
virtual MoviePtr movie( const std::string& title ) = 0;
virtual ArtistPtr artist( unsigned int id ) = 0;
virtual std::vector<ArtistPtr> artists() const = 0;
/**
* @brief artists List all artists that have at least an album.
* Artists that only appear on albums as guests won't be listed from here, but will be
* returned when querying an album for all its appearing artists
* @param sort A sorting criteria. So far, this is ignored, and artists are sorted by lexial order
* @param desc If true, the provided sorting criteria will be reversed.
*/
virtual std::vector<ArtistPtr> artists( medialibrary::SortingCriteria sort, bool desc ) const = 0;
virtual std::vector<GenrePtr> genres() const = 0;
/***
......
......@@ -269,3 +269,17 @@ std::vector<ArtistPtr> Artist::search( MediaLibraryPtr ml, const std::string& na
return fetchAll<IArtist>( ml, req, name + "*" );
}
std::vector<ArtistPtr> Artist::listAll(MediaLibraryPtr ml, medialibrary::SortingCriteria sort, bool desc)
{
std::string req = "SELECT * FROM " + policy::ArtistTable::Name +
" WHERE nb_albums > 0 AND is_present = 1 ORDER BY ";
switch ( sort )
{
default:
req += "name";
}
if ( desc == true )
req += " DESC";
return fetchAll<IArtist>( ml, req );
}
......@@ -65,6 +65,7 @@ public:
static bool createDefaultArtists( DBConnection dbConnection );
static std::shared_ptr<Artist> create( MediaLibraryPtr ml, const std::string& name );
static std::vector<ArtistPtr> search( MediaLibraryPtr ml, const std::string& name );
static std::vector<ArtistPtr> listAll( MediaLibraryPtr ml, medialibrary::SortingCriteria sort, bool desc );
private:
MediaLibraryPtr m_ml;
......
......@@ -373,11 +373,9 @@ std::shared_ptr<Artist> MediaLibrary::createArtist( const std::string& name )
}
}
std::vector<ArtistPtr> MediaLibrary::artists() const
std::vector<ArtistPtr> MediaLibrary::artists(medialibrary::SortingCriteria sort, bool desc) const
{
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name +
" WHERE nb_albums > 0 AND is_present = 1";
return Artist::fetchAll<IArtist>( this, req );
return Artist::listAll( this, sort, desc );
}
PlaylistPtr MediaLibrary::createPlaylist( const std::string& name )
......
......@@ -82,7 +82,7 @@ class MediaLibrary : public IMediaLibrary
virtual ArtistPtr artist( unsigned int id ) override;
ArtistPtr artist( const std::string& name );
std::shared_ptr<Artist> createArtist( const std::string& name );
virtual std::vector<ArtistPtr> artists() const override;
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;
......
......@@ -80,7 +80,7 @@ TEST_P( Tests, Parse )
}
if ( expected.HasMember( "artists" ) )
{
checkArtists( expected["artists"], m_ml->artists() );
checkArtists( expected["artists"], m_ml->artists( medialibrary::SortingCriteria::Default, false ) );
}
}
......
......@@ -135,7 +135,7 @@ TEST_F( Artists, AllSongs )
TEST_F( Artists, GetAll )
{
auto artists = ml->artists();
auto artists = ml->artists( medialibrary::SortingCriteria::Default, false );
// Ensure we don't include Unknown Artist // Various Artists
ASSERT_EQ( artists.size(), 0u );
......@@ -147,12 +147,12 @@ TEST_F( Artists, GetAll )
alb->setAlbumArtist( a.get() );
ASSERT_NE( a, nullptr );
}
artists = ml->artists();
artists = ml->artists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( artists.size(), 5u );
Reload();
auto artists2 = ml->artists();
auto artists2 = ml->artists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( artists2.size(), 5u );
}
......@@ -280,3 +280,24 @@ TEST_F( Artists, SortAlbum )
ASSERT_EQ( album2->id(), albums[1]->id() );
ASSERT_EQ( album1->id(), albums[2]->id() );
}
TEST_F( Artists, Sort )
{
// Keep in mind that artists are only listed when they are marked as album artist at least once
auto a1 = ml->createArtist( "A" );
auto alb1 = ml->createAlbum( "albumA" );
alb1->setAlbumArtist( a1.get() );
auto a2 = ml->createArtist( "B" );
auto alb2 = ml->createAlbum( "albumB" );
alb2->setAlbumArtist( a2.get() );
auto artists = ml->artists( medialibrary::SortingCriteria::Alpha, false );
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( a1->id(), artists[0]->id() );
ASSERT_EQ( a2->id(), artists[1]->id() );
artists = ml->artists( medialibrary::SortingCriteria::Alpha, true );
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( a1->id(), artists[1]->id() );
ASSERT_EQ( a2->id(), artists[0]->id() );
}
......@@ -272,7 +272,7 @@ TEST_F( DeviceFs, RemoveAlbum )
auto albums = ml->albums( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 2u, albums.size() );
auto artists = ml->artists();
auto artists = ml->artists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 2u, artists.size() );
auto device = fsMock->removeDevice( RemovableDeviceUuid );
......@@ -284,7 +284,7 @@ TEST_F( DeviceFs, RemoveAlbum )
albums = ml->albums( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
artists = ml->artists();
artists = ml->artists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, artists.size() );
}
......@@ -309,7 +309,7 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
auto albums = ml->albums( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
auto artists = ml->artists();
auto artists = ml->artists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, artists.size() );
auto artist = artists[0];
ASSERT_EQ( 2u, artist->media( medialibrary::SortingCriteria::Default, false ).size() );
......@@ -322,7 +322,7 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
albums = ml->albums( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
artists = ml->artists();
artists = ml->artists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, artists.size() );
ASSERT_EQ( 1u, albums[0]->tracks( medialibrary::SortingCriteria::Default, false ).size() );
ASSERT_EQ( 1u, artists[0]->media( medialibrary::SortingCriteria::Default, false ).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