Commit 18cd5cba authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

IGenre: Allow artists to be searched

parent b91a1388
......@@ -35,6 +35,8 @@ public:
virtual const std::string& name() const = 0;
virtual uint32_t nbTracks() const = 0;
virtual Query<IArtist> artists( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IArtist> searchArtist( const std::string& pattern,
const QueryParameters* params = nullptr ) const = 0;
virtual Query<IMedia> tracks( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IAlbum> albums( const QueryParameters* params = nullptr ) const = 0;
};
......
......@@ -438,6 +438,21 @@ Query<IArtist> Artist::listAll( MediaLibraryPtr ml, bool includeAll,
return make_query<Artist, IArtist>( ml, "*", std::move( req ) );
}
Query<IArtist> Artist::searchByGenre( MediaLibraryPtr ml, const std::string& pattern,
const QueryParameters* params, int64_t genreId )
{
std::string req = "FROM " + policy::ArtistTable::Name + " a "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.artist_id = a.id_artist "
"WHERE id_artist IN "
"(SELECT rowid FROM " + policy::ArtistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')"
"AND att.genre_id = ? "
"GROUP BY att.artist_id "
"ORDER BY a.name";
if ( params != nullptr && params->desc == true )
req += " DESC";
return make_query<Artist, IArtist>( ml, "*", std::move( req ), pattern, genreId );
}
std::string Artist::sortRequest( const QueryParameters* params )
{
std::string req = " ORDER BY name";
......
......@@ -81,6 +81,8 @@ public:
const QueryParameters* params );
static Query<IArtist> listAll( MediaLibraryPtr ml, bool includeAll,
const QueryParameters* params );
static Query<IArtist> searchByGenre( MediaLibraryPtr ml, const std::string& pattern,
const QueryParameters* params, int64_t genreId );
private:
static std::string sortRequest( const QueryParameters* params );
......
......@@ -88,6 +88,12 @@ Query<IArtist> Genre::artists( const QueryParameters* params ) const
return make_query<Artist, IArtist>( m_ml, "a.*", std::move( req ), m_id );
}
Query<IArtist> Genre::searchArtist( const std::string& pattern,
const QueryParameters* params ) const
{
return Artist::searchByGenre( m_ml, pattern, params, m_id );
}
Query<IMedia> Genre::tracks( const QueryParameters* params ) const
{
return AlbumTrack::fromGenre( m_ml, m_id, params );
......
......@@ -51,6 +51,8 @@ public:
virtual uint32_t nbTracks() const override;
void updateCachedNbTracks( int increment );
virtual Query<IArtist> artists( const QueryParameters* params ) const override;
virtual Query<IArtist> searchArtist( const std::string& pattern,
const QueryParameters* params = nullptr ) const override;
virtual Query<IMedia> tracks( const QueryParameters* params ) const override;
virtual Query<IAlbum> albums( const QueryParameters* params ) const override;
......
......@@ -243,3 +243,32 @@ TEST_F( Genres, CaseInsensitive )
auto g2 = Genre::fromName( ml.get(), "GENRE" );
ASSERT_EQ( g->id(), g2->id() );
}
TEST_F( Genres, SearchArtists )
{
auto artists = g->artists( nullptr )->all();
ASSERT_EQ( 0u, artists.size() );
auto a = ml->createArtist( "loutre 1" );
auto a2 = ml->createArtist( "loutre 2" );
auto album = ml->createAlbum( "album" );
auto album2 = ml->createAlbum( "album2" );
for ( auto i = 1u; i <= 5; ++i )
{
auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + ".mp3" ) );
auto track = album->addTrack( m, i, 1, a->id(), nullptr );
track->setGenre( g );
}
for ( auto i = 1u; i <= 5; ++i )
{
auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + "_2.mp3" ) );
auto track = album2->addTrack( m, i, 1, a2->id(), nullptr );
}
artists = ml->searchArtists( "loutre", nullptr )->all();
ASSERT_EQ( 2u, artists.size() );
artists = g->searchArtist( "loutre" )->all();
ASSERT_EQ( 1u, artists.size() );
ASSERT_EQ( a->id(), artists[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