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

IPlaylist: Allow media to be searched

parent 3cd602b9
......@@ -28,6 +28,8 @@
namespace medialibrary
{
class QueryParameters;
class IPlaylist
{
public:
......@@ -38,6 +40,8 @@ public:
virtual unsigned int creationDate() const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual Query<IMedia> media() const = 0;
virtual Query<IMedia> searchMedia( const std::string& pattern,
const QueryParameters* params ) const = 0;
///
/// \brief append Appends a media to a playlist
/// The media will be the last element of a subsequent call to media()
......
......@@ -776,6 +776,19 @@ Query<IMedia> Media::searchShowEpisodes(MediaLibraryPtr ml, const std::string& p
File::Type::Main, Media::SubType::ShowEpisode );
}
Query<IMedia> Media::searchInPlaylist( MediaLibraryPtr ml, const std::string& pattern,
int64_t playlistId, const QueryParameters* params )
{
std::string req = "FROM " + policy::MediaTable::Name + " m "
"INNER JOIN " + policy::FileTable::Name + " f ON m.id_media = f.media_id "
"LEFT JOIN PlaylistMediaRelation pmr ON pmr.media_id = m.id_media "
"WHERE pmr.playlist_id = ? AND m.is_present != 0 AND "
"m.id_media IN (SELECT rowid FROM " + policy::MediaTable::Name + "Fts "
"WHERE " + policy::MediaTable::Name + "Fts MATCH '*' || ? || '*')";
req += sortRequest( params );
return make_query<Media, IMedia>( ml, "m.*", std::move( req ), playlistId, pattern );
}
Query<IMedia> Media::fetchHistory( MediaLibraryPtr ml )
{
static const std::string req = "FROM " + policy::MediaTable::Name + " WHERE last_played_date IS NOT NULL"
......
......@@ -137,6 +137,8 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
int64_t genreId, const QueryParameters* params );
static Query<IMedia> searchShowEpisodes( MediaLibraryPtr ml, const std::string& pattern,
int64_t showId, const QueryParameters* params );
static Query<IMedia> searchInPlaylist( MediaLibraryPtr ml, const std::string& pattern,
int64_t playlistId, const QueryParameters* params );
static Query<IMedia> fetchHistory( MediaLibraryPtr ml );
static void clearHistory( MediaLibraryPtr ml );
......
......@@ -117,6 +117,12 @@ Query<IMedia> Playlist::media() const
return make_query<Media, IMedia>( m_ml, "m.*", req, m_id );
}
Query<IMedia> Playlist::searchMedia( const std::string& pattern,
const QueryParameters* params ) const
{
return Media::searchInPlaylist( m_ml, pattern, m_id, params );
}
bool Playlist::append( int64_t mediaId )
{
return add( mediaId, 0 );
......
......@@ -58,6 +58,8 @@ public:
virtual unsigned int creationDate() const override;
virtual const std::string& artworkMrl() const override;
virtual Query<IMedia> media() const override;
virtual Query<IMedia> searchMedia( const std::string& pattern,
const QueryParameters* params ) const override;
virtual bool append( int64_t mediaId ) override;
virtual bool add( int64_t mediaId, unsigned int position ) override;
virtual bool move( int64_t mediaId, unsigned int position ) override;
......
......@@ -327,3 +327,32 @@ TEST_F( Playlists, AddDuplicate )
res = pl->append( m->id() );
ASSERT_FALSE( res );
}
TEST_F( Playlists, SearchMedia )
{
auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "m1.mp3" ) );
m1->setTitleBuffered( "otter" );
m1->setType( IMedia::Type::Audio );
m1->save();
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "m2.mp3" ) );
// Won't match since it's not on the beginning of a word
m2->setTitleBuffered( "ENOOTTER" );
m2->setType( IMedia::Type::Audio );
m2->save();
auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "m3.mp3" ) );
m3->setTitleBuffered( "otter otter otter" );
m3->setType( IMedia::Type::Audio );
m3->save();
pl->append( m1->id() );
pl->append( m2->id() );
auto media = ml->searchMedia( "otter", nullptr )->all();
ASSERT_EQ( 2u, media.size() );
media = pl->searchMedia( "otter", nullptr )->all();
ASSERT_EQ( 1u, media.size() );
ASSERT_EQ( m1->id(), media[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