Commit 449f3667 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Album: Provide artists sorting

parent 0ec8ff82
......@@ -55,8 +55,10 @@ public:
virtual ArtistPtr albumArtist() const = 0;
/**
* @brief artists Returns a vector of all additional artists appearing on the album.
* Artists are sorted by name.
* @param desc
*/
virtual std::vector<ArtistPtr> artists() const = 0;
virtual std::vector<ArtistPtr> artists( bool desc ) const = 0;
/**
* @brief nbTracks Returns the amount of track in this album.
* The value is cached, and doesn't require fetching anything.
......
......@@ -256,11 +256,13 @@ bool Album::setAlbumArtist( Artist* artist )
return true;
}
std::vector<ArtistPtr> Album::artists() const
std::vector<ArtistPtr> Album::artists( bool desc ) const
{
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
"INNER JOIN AlbumArtistRelation aar ON aar.artist_id = art.id_artist "
"WHERE aar.album_id = ?";
"WHERE aar.album_id = ? ORDER BY art.name";
if ( desc == true )
req += " DESC";
return Artist::fetchAll<IArtist>( m_ml, req, m_id );
}
......
......@@ -90,7 +90,7 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
virtual ArtistPtr albumArtist() const override;
bool setAlbumArtist( Artist* artist );
virtual std::vector<ArtistPtr> artists() const override;
virtual std::vector<ArtistPtr> artists(bool desc) const override;
bool addArtist( std::shared_ptr<Artist> artist );
bool removeArtist( Artist* artist );
......
......@@ -155,7 +155,7 @@ void Tests::checkAlbums( const rapidjson::Value& expectedAlbums, std::vector<Alb
if ( expectedAlbum.HasMember( "artists" ) )
{
const auto& expectedArtists = expectedAlbum["artists"];
auto artists = a->artists();
auto artists = a->artists( false );
if ( expectedArtists.Size() != artists.size() )
return false;
for ( auto i = 0u; i < expectedArtists.Size(); ++i )
......
......@@ -206,17 +206,37 @@ TEST_F( Albums, Artists )
res = album->addArtist( artist2 );
ASSERT_EQ( res, true );
auto artists = album->artists();
auto artists = album->artists( false );
ASSERT_EQ( artists.size(), 2u );
Reload();
album = std::static_pointer_cast<Album>( ml->album( album->id() ) );
artists = album->artists();
artists = album->artists( false );
ASSERT_EQ( album->albumArtist(), nullptr );
ASSERT_EQ( artists.size(), 2u );
}
TEST_F( Albums, SortArtists )
{
auto album = ml->createAlbum( "album" );
auto artist1 = ml->createArtist( "john" );
auto artist2 = ml->createArtist( "doe" );
album->addArtist( artist1 );
album->addArtist( artist2 );
auto artists = album->artists( false );
ASSERT_EQ( artists.size(), 2u );
ASSERT_EQ( artist1->id(), artists[1]->id() );
ASSERT_EQ( artist2->id(), artists[0]->id() );
artists = album->artists( true );
ASSERT_EQ( artists.size(), 2u );
ASSERT_EQ( artist1->id(), artists[0]->id() );
ASSERT_EQ( artist2->id(), artists[1]->id() );
}
TEST_F( Albums, AlbumArtist )
{
auto album = ml->createAlbum( "test" );
......
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