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

Genre: Add an artists() method

It returns all artists with at least a song from the specified musical genre
parent 8cdac92a
......@@ -23,6 +23,7 @@
#include "Genre.h"
#include "AlbumTrack.h"
#include "Artist.h"
namespace policy
{
......@@ -55,7 +56,10 @@ const std::string& Genre::name() const
std::vector<ArtistPtr> Genre::artists() const
{
static const std::string req = "SELECT a.* FROM " + policy::ArtistTable::Name + " a "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist "
"WHERE att.genre_id = ? GROUP BY att.artist_id";
return Artist::fetchAll<IArtist>( m_dbConnection, req, m_id );
}
std::vector<AlbumTrackPtr> Genre::tracks() const
......
......@@ -119,7 +119,6 @@ void MediaLibrary::setFsFactory(std::shared_ptr<factory::IFileSystem> fsFactory)
m_fsFactory = fsFactory;
}
bool MediaLibrary::createAllTables()
{
auto t = m_dbConnection->newTransaction();
......
......@@ -68,3 +68,33 @@ TEST_F( Genres, ListAlbumTracks )
auto tracks = g->tracks();
ASSERT_EQ( 2u, tracks.size() );
}
TEST_F( Genres, ListArtists )
{
auto artists = g->artists();
ASSERT_EQ( 0u, artists.size() );
auto a = ml->createArtist( "artist" );
auto a2 = ml->createArtist( "artist 2" );
// Ensure we're not just returning all the artists:
auto a3 = ml->createArtist( "artist 3" );
auto album = ml->createAlbum( "album" );
auto album2 = ml->createAlbum( "album2" );
for ( auto i = 1u; i <= 5; ++i )
{
auto m = ml->addFile( std::to_string( i ) + ".mp3" );
auto track = album->addTrack( m, i, 1 );
track->setGenre( g );
track->setArtist( a );
}
for ( auto i = 1u; i <= 5; ++i )
{
auto m = ml->addFile( std::to_string( i ) + "_2.mp3" );
auto track = album2->addTrack( m, i, 1 );
track->setGenre( g );
track->setArtist( a2 );
}
artists = g->artists();
ASSERT_EQ( 2u, artists.size() );
}
Supports Markdown
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