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

IGenre: Allow tracks to be searched

parent 18cd5cba
......@@ -38,6 +38,8 @@ public:
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<IMedia> searchTracks( const std::string& pattern,
const QueryParameters* params = nullptr ) const = 0;
virtual Query<IAlbum> albums( const QueryParameters* params = nullptr ) const = 0;
};
......
......@@ -29,6 +29,7 @@
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "Media.h"
#include "database/SqliteQuery.h"
namespace medialibrary
......@@ -99,6 +100,11 @@ Query<IMedia> Genre::tracks( const QueryParameters* params ) const
return AlbumTrack::fromGenre( m_ml, m_id, params );
}
Query<IMedia> Genre::searchTracks( const std::string& pattern, const QueryParameters* params ) const
{
return Media::searchGenreTracks( m_ml, pattern, m_id, params );
}
Query<IAlbum> Genre::albums( const QueryParameters* params ) const
{
return Album::fromGenre( m_ml, m_id, params );
......
......@@ -54,6 +54,8 @@ public:
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<IMedia> searchTracks( const std::string& pattern,
const QueryParameters* params = nullptr ) const override;
virtual Query<IAlbum> albums( const QueryParameters* params ) const override;
static void createTable( sqlite::Connection* dbConn );
......
......@@ -741,6 +741,23 @@ Query<IMedia> Media::searchArtistTracks(MediaLibraryPtr ml, const std::string& p
File::Type::Main, Media::SubType::AlbumTrack );
}
Query<IMedia> Media::searchGenreTracks(MediaLibraryPtr ml, const std::string& pattern, int64_t genreId, 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.genre_id = ?"
" AND f.is_present = 1"
" AND f.type = ?"
" AND m.subtype = ?";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", req, pattern, genreId,
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"
......
......@@ -133,6 +133,8 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
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> searchGenreTracks( MediaLibraryPtr ml, const std::string& pattern,
int64_t genreId, const QueryParameters* params );
static Query<IMedia> fetchHistory( MediaLibraryPtr ml );
static void clearHistory( MediaLibraryPtr ml );
......
......@@ -272,3 +272,25 @@ TEST_F( Genres, SearchArtists )
ASSERT_EQ( 1u, artists.size() );
ASSERT_EQ( a->id(), artists[0]->id() );
}
TEST_F( Genres, SearchTracks )
{
auto a = ml->createAlbum( "album" );
auto m = std::static_pointer_cast<Media>( ml->addMedia( "Hell's Kitchen.mp3" ) );
m->setType( IMedia::Type::Audio );
auto t = a->addTrack( m, 1, 1, 0, g.get() );
m->save();
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "Different genre Hell's Kitchen.mp3" ) );
m2->setType( IMedia::Type::Audio );
auto t2 = a->addTrack( m2, 1, 1, 0, nullptr );
m2->save();
auto tracks = ml->searchAudio( "kitchen", nullptr )->all();
ASSERT_EQ( 2u, tracks.size() );
tracks = g->searchTracks( "kitchen", nullptr )->all();
ASSERT_EQ( 1u, tracks.size() );
ASSERT_EQ( m->id(), tracks[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