Commit 3ec9e091 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

IGenre: Allow albums to be searched

parent 6cfec3cb
......@@ -41,6 +41,8 @@ public:
virtual Query<IMedia> searchTracks( const std::string& pattern,
const QueryParameters* params = nullptr ) const = 0;
virtual Query<IAlbum> albums( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IAlbum> searchAlbums( const std::string& pattern,
const QueryParameters* params = nullptr ) const = 0;
};
}
......@@ -567,6 +567,19 @@ Query<IAlbum> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, const Query
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ), genreId );
}
Query<IAlbum> Album::searchFromGenre( MediaLibraryPtr ml, const std::string& pattern,
int64_t genreId, const QueryParameters* params )
{
std::string req = "FROM " + policy::AlbumTable::Name + " alb "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.album_id = alb.id_album "
"WHERE id_album IN "
"(SELECT rowid FROM " + policy::AlbumTable::Name + "Fts WHERE " +
policy::AlbumTable::Name + "Fts MATCH '*' || ? || '*')"
"AND att.genre_id = ? GROUP BY att.album_id";
req += orderBy( params );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ), pattern, genreId );
}
Query<IAlbum> Album::listAll( MediaLibraryPtr ml, const QueryParameters* params )
{
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
......
......@@ -118,6 +118,8 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
int64_t artistId, const QueryParameters* params );
static Query<IAlbum> fromArtist( MediaLibraryPtr ml, int64_t artistId, const QueryParameters* params );
static Query<IAlbum> fromGenre( MediaLibraryPtr ml, int64_t genreId, const QueryParameters* params );
static Query<IAlbum> searchFromGenre( MediaLibraryPtr ml, const std::string& pattern,
int64_t genreId, const QueryParameters* params );
static Query<IAlbum> listAll( MediaLibraryPtr ml, const QueryParameters* params );
private:
......
......@@ -110,6 +110,12 @@ Query<IAlbum> Genre::albums( const QueryParameters* params ) const
return Album::fromGenre( m_ml, m_id, params );
}
Query<IAlbum> Genre::searchAlbums( const std::string& pattern,
const QueryParameters* params ) const
{
return Album::searchFromGenre( m_ml, pattern, m_id, params );
}
void Genre::createTable( sqlite::Connection* dbConn )
{
const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::GenreTable::Name +
......
......@@ -57,6 +57,8 @@ public:
virtual Query<IMedia> searchTracks( const std::string& pattern,
const QueryParameters* params = nullptr ) const override;
virtual Query<IAlbum> albums( const QueryParameters* params ) const override;
virtual Query<IAlbum> searchAlbums( const std::string& pattern,
const QueryParameters* params = nullptr ) const override;
static void createTable( sqlite::Connection* dbConn );
static void createTriggers( sqlite::Connection* dbConn );
......
......@@ -294,3 +294,26 @@ TEST_F( Genres, SearchTracks )
ASSERT_EQ( 1u, tracks.size() );
ASSERT_EQ( m->id(), tracks[0]->id() );
}
TEST_F( Genres, SearchAlbums )
{
auto a1 = ml->createAlbum( "an album" );
auto m = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
m->setType( IMedia::Type::Audio );
auto t = a1->addTrack( m, 1, 1, 0, g.get() );
m->save();
auto a2 = ml->createAlbum( "another album" );
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "Different genre Hell's Kitchen.mp3" ) );
m2->setType( IMedia::Type::Audio );
auto t2 = a2->addTrack( m2, 1, 1, 0, nullptr );
m2->save();
auto albums = ml->searchAlbums( "album", nullptr)->all();
ASSERT_EQ( 2u, albums.size() );
albums = g->searchAlbums( "album", nullptr )->all();
ASSERT_EQ( 1u, albums.size() );
ASSERT_EQ( a1->id(), albums[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