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

IArtist: Allow artist tracks to be search

parent 83f0e680
......@@ -46,6 +46,8 @@ public:
virtual Query<IAlbum> searchAlbums( const std::string& pattern,
const QueryParameters* params = nullptr ) const = 0;
virtual Query<IMedia> tracks( const QueryParameters* params = nullptr ) const = 0;
virtual Query<IMedia> searchTracks( const std::string& pattern,
const QueryParameters* params = nullptr ) const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual const std::string& musicBrainzId() const = 0;
/**
......
......@@ -151,6 +151,11 @@ Query<IMedia> Artist::tracks( const QueryParameters* params ) const
return make_query<Media, IMedia>( m_ml, "med.*", std::move( req ), m_id );
}
Query<IMedia> Artist::searchTracks( const std::string& pattern, const QueryParameters* params ) const
{
return Media::searchArtistTracks( m_ml, pattern, m_id, params );
}
bool Artist::addMedia( Media& media )
{
static const std::string req = "INSERT INTO MediaArtistRelation VALUES(?, ?)";
......
......@@ -59,6 +59,8 @@ public:
virtual Query<IAlbum> searchAlbums( const std::string& pattern,
const QueryParameters* params = nullptr ) const override;
virtual Query<IMedia> tracks( const QueryParameters* params ) const override;
virtual Query<IMedia> searchTracks( const std::string& pattern,
const QueryParameters* params = nullptr ) const override;
bool addMedia( Media& tracks );
virtual const std::string& artworkMrl() const override;
std::shared_ptr<Thumbnail> thumbnail();
......
......@@ -724,6 +724,23 @@ Query<IMedia> Media::searchAlbumTracks(MediaLibraryPtr ml, const std::string& pa
File::Type::Main, Media::SubType::AlbumTrack );
}
Query<IMedia> Media::searchArtistTracks(MediaLibraryPtr ml, const std::string& pattern, int64_t artistId, const QueryParameters* params)
{
std::string req = "FROM " + policy::MediaTable::Name + " m "
" INNER JOIN " + policy::FileTable::Name + " f ON m.id_media = f.media_id"
" INNER JOIN " + policy::AlbumTrackTable::Name + " tra ON tra.media_id = m.id_media "
" WHERE"
" m.id_media IN (SELECT rowid FROM " + policy::MediaTable::Name + "Fts"
" WHERE " + policy::MediaTable::Name + "Fts MATCH '*' || ? || '*')"
" AND tra.artist_id = ?"
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.subtype = ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, pattern, artistId,
File::Type::Main, Media::SubType::AlbumTrack );
}
Query<IMedia> Media::fetchHistory( MediaLibraryPtr ml )
{
static const std::string req = "FROM " + policy::MediaTable::Name + " WHERE last_played_date IS NOT NULL"
......
......@@ -131,6 +131,8 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
Media::Type subType, const QueryParameters* params );
static Query<IMedia> searchAlbumTracks( MediaLibraryPtr ml, const std::string& pattern,
int64_t albumId, const QueryParameters* params );
static Query<IMedia> searchArtistTracks( MediaLibraryPtr ml, const std::string& pattern,
int64_t artistId, const QueryParameters* params );
static Query<IMedia> fetchHistory( MediaLibraryPtr ml );
static void clearHistory( MediaLibraryPtr ml );
......
......@@ -562,3 +562,29 @@ TEST_F( Artists, SearchAlbums )
ASSERT_EQ( 1u, artistAlbums.size() );
ASSERT_EQ( alb1->id(), artistAlbums[0]->id() );
}
TEST_F( Artists, SearchTracks )
{
auto artist1 = ml->createArtist( "artist" );
auto album1 = ml->createAlbum( "album" );
auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
m1->setTitleBuffered( "sea otter" );
m1->setType( Media::Type::Audio );
auto track1 = album1->addTrack( m1, 1, 0, artist1->id(), nullptr );
m1->save();
auto artist2 = ml->createArtist( "artist2" );
auto album2 = ml->createAlbum( "album2" );
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "track2.mp3" ) );
m2->setTitleBuffered( "sea cucumber" );
m2->setType( IMedia::Type::Audio );
album2->addTrack( m2, 1, 0, artist2->id(), nullptr );
m2->save();
auto allTracks = ml->searchAudio( "sea" )->all();
ASSERT_EQ( 2u, allTracks.size() );
auto artistTracks = artist1->searchTracks( "sea", nullptr )->all();
ASSERT_EQ( 1u, artistTracks.size() );
ASSERT_EQ( track1->id(), artistTracks[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