Commit 8b6eae5d authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Artist: List all albums the artist appears on when calling IArtist::albums

Clarify the difference between nbAlbums & albums().size()
parent e8cf924c
......@@ -37,10 +37,21 @@ public:
virtual int64_t id() const = 0;
virtual const std::string& name() const = 0;
virtual const std::string& shortBio() const = 0;
/**
* @brief albums List the albums this artist appears on.
*
* This will return all albums by this artist, and all album the artist
* appeared on, even if they are not the main artist (or AlbumArtist)
*/
virtual std::vector<AlbumPtr> albums( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual std::vector<MediaPtr> media( SortingCriteria sort = SortingCriteria::Default, bool desc = false ) const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual const std::string& musicBrainzId() const = 0;
/**
* @brief nbAlbums
* @return The number of albums *by* this artist. This doesn't include the
* albums an artist appears on.
*/
virtual unsigned int nbAlbums() const = 0;
virtual unsigned int nbTracks() const = 0;
};
......
......@@ -454,7 +454,11 @@ std::vector<AlbumPtr> Album::search( MediaLibraryPtr ml, const std::string& patt
std::vector<AlbumPtr> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc )
{
std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " alb "
"WHERE artist_id = ? AND is_present != 0 ORDER BY ";
"INNER JOIN " + policy::AlbumTrackTable::Name + " att "
"ON att.album_id = alb.id_album "
"WHERE (att.artist_id = ? OR alb.artist_id = ?) "
"AND att.is_present != 0 "
"GROUP BY att.album_id ORDER BY ";
switch ( sort )
{
case SortingCriteria::Alpha:
......@@ -473,7 +477,7 @@ std::vector<AlbumPtr> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, S
break;
}
return fetchAll<IAlbum>( ml, req, artistId );
return fetchAll<IAlbum>( ml, req, artistId, artistId );
}
std::vector<AlbumPtr> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc)
......
......@@ -410,16 +410,15 @@ void Tests::checkArtists(const rapidjson::Value& expectedArtists, std::vector<Ar
if ( expectedArtist["id"].GetUint() != artist->id() )
return false;
}
if ( expectedArtist.HasMember( "nbAlbums" ) || expectedArtist.HasMember( "albums" ) )
if ( expectedArtist.HasMember( "nbAlbums" ) )
{
if ( artist->nbAlbums() != expectedArtist["nbAlbums"].GetUint() )
return false;
}
if ( expectedArtist.HasMember( "albums" ) )
{
auto albums = artist->albums( SortingCriteria::Default, false );
if ( expectedArtist.HasMember( "nbAlbums" ) )
{
if ( albums.size() != expectedArtist["nbAlbums"].GetUint() )
return false;
if ( expectedArtist.HasMember( "albums" ) )
checkAlbums( expectedArtist["albums"], albums );
}
checkAlbums( expectedArtist["albums"], albums );
}
if ( expectedArtist.HasMember( "nbTracks" ) )
{
......
......@@ -92,6 +92,7 @@ TEST_F( Artists, ArtworkMrl )
ASSERT_EQ( a2->artworkMrl(), artwork );
}
// Test the number of albums based on the artist tracks
TEST_F( Artists, Albums )
{
auto artist = ml->createArtist( "Cannibal Otters" );
......@@ -102,6 +103,13 @@ TEST_F( Artists, Albums )
ASSERT_NE( album1, nullptr );
ASSERT_NE( album2, nullptr );
auto media1 = ml->addFile( "track1.mp3" );
ASSERT_NE( nullptr, media1 );
album1->addTrack( media1, 1, 0, artist->id(), nullptr );
auto media2 = ml->addFile( "track2.mp3" );
ASSERT_NE( nullptr, media2 );
album2->addTrack( media2, 1, 0, artist->id(), nullptr );
album1->setAlbumArtist( artist );
album2->setAlbumArtist( artist );
......@@ -112,7 +120,31 @@ TEST_F( Artists, Albums )
auto artist2 = ml->artist( "Cannibal Otters" );
auto albums2 = artist2->albums( SortingCriteria::Default, false );
ASSERT_EQ( albums.size(), 2u );
ASSERT_EQ( albums2.size(), 2u );
}
// Test the nb_album DB field (ie. we don't need to create tracks for this test)
TEST_F( Artists, NbAlbums )
{
auto artist = ml->createArtist( "Cannibal Otters" );
auto album1 = ml->createAlbum( "album1" );
auto album2 = ml->createAlbum( "album2" );
ASSERT_NE( artist, nullptr );
ASSERT_NE( album1, nullptr );
ASSERT_NE( album2, nullptr );
album1->setAlbumArtist( artist );
album2->setAlbumArtist( artist );
auto nbAlbums = artist->nbAlbums();
ASSERT_EQ( nbAlbums, 2u );
Reload();
auto artist2 = ml->artist( "Cannibal Otters" );
nbAlbums = artist2->nbAlbums();
ASSERT_EQ( nbAlbums, 2u );
}
TEST_F( Artists, AllSongs )
......@@ -284,10 +316,16 @@ TEST_F( Artists, SortAlbum )
{
auto artist = ml->createArtist( "Dream Seaotter" );
auto album1 = ml->createAlbum( "album1" );
auto media1 = ml->addFile( "track1.mp3" );
album1->addTrack( media1, 1, 0, artist->id(), nullptr );
album1->setReleaseYear( 2000, false );
auto album2 = ml->createAlbum( "album2" );
auto media2 = ml->addFile( "track2.mp3" );
album2->addTrack( media2, 1, 0, artist->id(), nullptr );
album2->setReleaseYear( 1000, false );
auto album3 = ml->createAlbum( "album3" );
auto media3 = ml->addFile( "track2.mp3" );
album3->addTrack( media3, 1, 0, artist->id(), nullptr );
album3->setReleaseYear( 2000, false );
album1->setAlbumArtist( artist );
......
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