Commit 53e1a520 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Artist: Allow search results to be sorted

parent c8517b13
......@@ -308,7 +308,9 @@ class IMediaLibrary
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name ) const = 0;
virtual std::vector<AlbumPtr> searchAlbums( const std::string& pattern ) const = 0;
virtual std::vector<GenrePtr> searchGenre( const std::string& genre ) const = 0;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name ) const = 0;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual SearchAggregate search( const std::string& pattern ) const = 0;
/**
......
......@@ -400,11 +400,13 @@ std::shared_ptr<Artist> Artist::create( MediaLibraryPtr ml, const std::string& n
return artist;
}
std::vector<ArtistPtr> Artist::search( MediaLibraryPtr ml, const std::string& name )
std::vector<ArtistPtr> Artist::search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc )
{
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name + " WHERE id_artist IN "
std::string req = "SELECT * FROM " + policy::ArtistTable::Name + " WHERE id_artist IN "
"(SELECT rowid FROM " + policy::ArtistTable::Name + "Fts WHERE name MATCH '*' || ? || '*')"
"AND is_present != 0";
req += sortRequest( sort, desc );
return fetchAll<IArtist>( ml, req, name );
}
......
......@@ -73,7 +73,8 @@ public:
static void createTriggers( sqlite::Connection* dbConnection, uint32_t dbModelVersion );
static bool createDefaultArtists( sqlite::Connection* 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> search( MediaLibraryPtr ml, const std::string& name,
SortingCriteria sort, bool desc );
static std::vector<ArtistPtr> listAll( MediaLibraryPtr ml, bool includeAll,
SortingCriteria sort, bool desc );
......
......@@ -697,18 +697,20 @@ std::vector<GenrePtr> MediaLibrary::searchGenre( const std::string& genre ) cons
return Genre::search( this, genre );
}
std::vector<ArtistPtr> MediaLibrary::searchArtists(const std::string& name ) const
std::vector<ArtistPtr> MediaLibrary::searchArtists( const std::string& name,
SortingCriteria sort,
bool desc ) const
{
if ( validateSearchPattern( name ) == false )
return {};
return Artist::search( this, name );
return Artist::search( this, name, sort, desc );
}
SearchAggregate MediaLibrary::search( const std::string& pattern ) const
{
SearchAggregate res;
res.albums = searchAlbums( pattern );
res.artists = searchArtists( pattern );
res.artists = searchArtists( pattern, SortingCriteria::Default, false );
res.genres = searchGenre( pattern );
res.media = searchMedia( pattern );
res.playlists = searchPlaylists( pattern );
......
......@@ -119,7 +119,9 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name ) const override;
virtual std::vector<AlbumPtr> searchAlbums( const std::string& pattern ) const override;
virtual std::vector<GenrePtr> searchGenre( const std::string& genre ) const override;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name ) const override;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name,
SortingCriteria sort,
bool desc ) const override;
virtual SearchAggregate search( const std::string& pattern ) const override;
virtual void discover( const std::string& entryPoint ) override;
......
......@@ -274,12 +274,19 @@ TEST_F( Artists, MusicBrainzId )
TEST_F( Artists, Search )
{
ml->createArtist( "artist 1" );
ml->createArtist( "artist 2" );
auto a1 = ml->createArtist( "artist 1" );
auto a2 = ml->createArtist( "artist 2" );
ml->createArtist( "dream seaotter" );
auto artists = ml->searchArtists( "artist" );
auto artists = ml->searchArtists( "artist", SortingCriteria::Default, false );
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( artists[0]->id(), a1->id() );
ASSERT_EQ( artists[1]->id(), a2->id() );
artists = ml->searchArtists( "artist", SortingCriteria::Default, true );
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( artists[0]->id(), a2->id() );
ASSERT_EQ( artists[1]->id(), a1->id() );
}
TEST_F( Artists, SearchAfterDelete )
......@@ -288,12 +295,12 @@ TEST_F( Artists, SearchAfterDelete )
ml->createArtist( "artist 2" );
ml->createArtist( "dream seaotter" );
auto artists = ml->searchArtists( "artist" );
auto artists = ml->searchArtists( "artist", SortingCriteria::Default, false );
ASSERT_EQ( 2u, artists.size() );
ml->deleteArtist( a->id() );
artists = ml->searchArtists( "artist" );
artists = ml->searchArtists( "artist", SortingCriteria::Default, false );
ASSERT_EQ( 1u, artists.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