Commit 7abc3677 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Album: Allow sort results to be sorted

parent 9d17df61
......@@ -309,7 +309,9 @@ class IMediaLibrary
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
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<AlbumPtr> searchAlbums( const std::string& pattern,
SortingCriteria sort = SortingCriteria::Default,
bool desc = false ) const = 0;
virtual std::vector<GenrePtr> searchGenre( const std::string& genre ) const = 0;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name,
SortingCriteria sort = SortingCriteria::Default,
......
......@@ -491,13 +491,15 @@ std::shared_ptr<Album> Album::createUnknownAlbum( MediaLibraryPtr ml, const Arti
return album;
}
std::vector<AlbumPtr> Album::search( MediaLibraryPtr ml, const std::string& pattern )
std::vector<AlbumPtr> Album::search( MediaLibraryPtr ml, const std::string& pattern,
SortingCriteria sort, bool desc )
{
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " alb "
std::string req = "SELECT * FROM " + policy::AlbumTable::Name + " alb "
"WHERE id_album IN "
"(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " +
policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')"
"AND is_present != 0";
req += orderBy( sort, desc );
return fetchAll<IAlbum>( ml, req, pattern );
}
......
......@@ -109,7 +109,8 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
/// \param pattern A pattern representing the title, or the name of the main artist
/// \return
///
static std::vector<AlbumPtr> search( MediaLibraryPtr ml, const std::string& pattern );
static std::vector<AlbumPtr> search( MediaLibraryPtr ml, const std::string& pattern,
SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> fromArtist( MediaLibraryPtr ml, int64_t artistId, SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> fromGenre( MediaLibraryPtr ml, int64_t genreId, SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> listAll( MediaLibraryPtr ml, SortingCriteria sort, bool desc );
......
......@@ -684,11 +684,12 @@ std::vector<PlaylistPtr> MediaLibrary::searchPlaylists( const std::string& name
return Playlist::search( this, name );
}
std::vector<AlbumPtr> MediaLibrary::searchAlbums( const std::string& pattern ) const
std::vector<AlbumPtr> MediaLibrary::searchAlbums( const std::string& pattern,
SortingCriteria sort, bool desc ) const
{
if ( validateSearchPattern( pattern ) == false )
return {};
return Album::search( this, pattern );
return Album::search( this, pattern, sort, desc );
}
std::vector<GenrePtr> MediaLibrary::searchGenre( const std::string& genre ) const
......@@ -710,7 +711,7 @@ std::vector<ArtistPtr> MediaLibrary::searchArtists( const std::string& name,
SearchAggregate MediaLibrary::search( const std::string& pattern ) const
{
SearchAggregate res;
res.albums = searchAlbums( pattern );
res.albums = searchAlbums( pattern, SortingCriteria::Default, false );
res.artists = searchArtists( pattern, SortingCriteria::Default, false );
res.genres = searchGenre( pattern );
res.media = searchMedia( pattern, SortingCriteria::Default, false );
......
......@@ -118,7 +118,9 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual MediaSearchAggregate searchMedia( const std::string& title,
SortingCriteria sort, bool desc ) const override;
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<AlbumPtr> searchAlbums( const std::string& pattern,
SortingCriteria sort,
bool desc ) const override;
virtual std::vector<GenrePtr> searchGenre( const std::string& genre ) const override;
virtual std::vector<ArtistPtr> searchArtists( const std::string& name,
SortingCriteria sort,
......
......@@ -284,7 +284,7 @@ TEST_F( Albums, SearchByTitle )
ml->createAlbum( "sea otters" );
ml->createAlbum( "pangolins of fire" );
auto albums = ml->searchAlbums( "otte" );
auto albums = ml->searchAlbums( "otte", SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
}
......@@ -294,7 +294,7 @@ TEST_F( Albums, SearchByArtist )
auto artist = ml->createArtist( "pangolins" );
a->setAlbumArtist( artist );
auto albums = ml->searchAlbums( "pangol" );
auto albums = ml->searchAlbums( "pangol", SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
}
......@@ -304,7 +304,7 @@ TEST_F( Albums, SearchNoDuplicate )
auto artist = ml->createArtist( "otters" );
a->setAlbumArtist( artist );
auto albums = ml->searchAlbums( "otters" );
auto albums = ml->searchAlbums( "otters", SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
}
......@@ -314,7 +314,7 @@ TEST_F( Albums, SearchNoUnknownAlbum )
auto album = artist->unknownAlbum();
ASSERT_NE( nullptr, album );
auto albums = ml->searchAlbums( "otters" );
auto albums = ml->searchAlbums( "otters", SortingCriteria::Default, false );
ASSERT_EQ( 0u, albums.size() );
// Can't search by name since there is no name set for unknown albums
}
......@@ -322,12 +322,12 @@ TEST_F( Albums, SearchNoUnknownAlbum )
TEST_F( Albums, SearchAfterDeletion )
{
auto a = ml->createAlbum( "sea otters" );
auto albums = ml->searchAlbums( "sea" );
auto albums = ml->searchAlbums( "sea", SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
ml->deleteAlbum( a->id() );
albums = ml->searchAlbums( "sea" );
albums = ml->searchAlbums( "sea", SortingCriteria::Default, false );
ASSERT_EQ( 0u, albums.size() );
}
......@@ -338,18 +338,18 @@ TEST_F( Albums, SearchAfterArtistUpdate )
auto artist2 = ml->createArtist( "pangolin of ice" );
a->setAlbumArtist( artist );
auto albums = ml->searchAlbums( "fire" );
auto albums = ml->searchAlbums( "fire", SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
albums = ml->searchAlbums( "ice" );
albums = ml->searchAlbums( "ice", SortingCriteria::Default, false );
ASSERT_EQ( 0u, albums.size() );
a->setAlbumArtist( artist2 );
albums = ml->searchAlbums( "fire" );
albums = ml->searchAlbums( "fire", SortingCriteria::Default, false );
ASSERT_EQ( 0u, albums.size() );
albums = ml->searchAlbums( "ice" );
albums = ml->searchAlbums( "ice", SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
}
......@@ -565,3 +565,27 @@ TEST_F( Albums, Duration )
a2 = ml->album( a->id() );
ASSERT_EQ( 100u, a2->duration() );
}
TEST_F( Albums, SearchAndSort )
{
auto alb1 = ml->createAlbum( "Z album" );
auto m = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
alb1->addTrack( m, 1, 0, 0, nullptr );
auto alb2 = ml->createAlbum( "A album" );
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "track2.mp3" ) );
alb2->addTrack( m2, 1, 0, 0, nullptr );
auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "track3.mp3" ) );
alb2->addTrack( m3, 2, 0, 0, nullptr );
auto albs = ml->searchAlbums( "album", SortingCriteria::Alpha, false );
ASSERT_EQ( 2u, albs.size() );
ASSERT_EQ( albs[0]->id(), alb2->id() );
ASSERT_EQ( albs[1]->id(), alb1->id() );
// Sorting by tracknumber is descending by default, so we expect album 2 first
albs = ml->searchAlbums( "album", SortingCriteria::TrackNumber, false );
ASSERT_EQ( 2u, albs.size() );
ASSERT_EQ( albs[0]->id(), alb2->id() );
ASSERT_EQ( albs[1]->id(), alb1->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