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

Media: Split search results per subtype

parent 1fb8c5a6
......@@ -34,11 +34,19 @@ namespace medialibrary
static constexpr auto UnknownArtistID = 1u;
static constexpr auto VariousArtistID = 2u;
struct MediaSearchAggregate
{
std::vector<MediaPtr> episodes;
std::vector<MediaPtr> movies;
std::vector<MediaPtr> others;
std::vector<MediaPtr> tracks;
};
struct SearchAggregate
{
std::vector<AlbumPtr> albums;
std::vector<ArtistPtr> artists;
std::vector<MediaPtr> media;
MediaSearchAggregate media;
std::vector<PlaylistPtr> playlists;
};
}
......@@ -123,7 +131,7 @@ class IMediaLibrary
/**
* Search
*/
virtual std::vector<MediaPtr> searchMedia( const std::string& pattern ) const = 0;
virtual medialibrary::MediaSearchAggregate searchMedia( const std::string& pattern ) 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<GenrePtr> searchGenre( const std::string& genre ) const = 0;
......
......@@ -405,11 +405,31 @@ std::vector<HistoryPtr> MediaLibrary::history() const
return History::fetch( m_dbConnection.get() );
}
std::vector<MediaPtr> MediaLibrary::searchMedia( const std::string& title ) const
medialibrary::MediaSearchAggregate MediaLibrary::searchMedia( const std::string& title ) const
{
if ( validateSearchPattern( title ) == false )
return {};
return Media::search( m_dbConnection.get(), title );
auto tmp = Media::search( m_dbConnection.get(), title );
medialibrary::MediaSearchAggregate res;
for ( auto& m : tmp )
{
switch ( m->subType() )
{
case IMedia::SubType::AlbumTrack:
res.tracks.emplace_back( std::move( m ) );
break;
case IMedia::SubType::Movie:
res.movies.emplace_back( std::move( m ) );
break;
case IMedia::SubType::ShowEpisode:
res.episodes.emplace_back( std::move( m ) );
break;
default:
res.others.emplace_back( std::move( m ) );
break;
}
}
return res;
}
std::vector<PlaylistPtr> MediaLibrary::searchPlaylists( const std::string& name ) const
......
......@@ -89,7 +89,7 @@ class MediaLibrary : public IMediaLibrary
virtual bool addToHistory( const std::string& mrl );
virtual std::vector<HistoryPtr> history() const;
virtual std::vector<MediaPtr> searchMedia( const std::string& title ) const override;
virtual medialibrary::MediaSearchAggregate searchMedia( const std::string& title ) 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<GenrePtr> searchGenre( const std::string& genre ) const override;
......
......@@ -151,16 +151,16 @@ TEST_F( Medias, Search )
{
auto m = ml->addFile( "track " + std::to_string( i ) + ".mp3" );
}
auto media = ml->searchMedia( "tra" );
auto media = ml->searchMedia( "tra" ).others;
ASSERT_EQ( 10u, media.size() );
media = ml->searchMedia( "track 1" );
media = ml->searchMedia( "track 1" ).others;
ASSERT_EQ( 2u, media.size() );
media = ml->searchMedia( "grouik" );
media = ml->searchMedia( "grouik" ).others;
ASSERT_EQ( 0u, media.size() );
media = ml->searchMedia( "rack" );
media = ml->searchMedia( "rack" ).others;
ASSERT_EQ( 0u, media.size() );
}
......@@ -168,16 +168,16 @@ TEST_F( Medias, SearchAfterEdit )
{
auto m = ml->addFile( "media.mp3" );
auto media = ml->searchMedia( "media" );
auto media = ml->searchMedia( "media" ).others;
ASSERT_EQ( 1u, media.size() );
m->setTitle( "otters are awesome" );
m->save();
media = ml->searchMedia( "media" );
media = ml->searchMedia( "media" ).others;
ASSERT_EQ( 0u, media.size() );
media = ml->searchMedia( "otters" );
media = ml->searchMedia( "otters" ).others;
ASSERT_EQ( 1u, media.size() );
}
......@@ -185,58 +185,79 @@ TEST_F( Medias, SearchAfterDelete )
{
auto m = ml->addFile( "media.mp3" );
auto media = ml->searchMedia( "media" );
auto media = ml->searchMedia( "media" ).others;
ASSERT_EQ( 1u, media.size() );
auto f = m->files()[0];
m->removeFile( static_cast<File&>( *f ) );
media = ml->searchMedia( "media" );
media = ml->searchMedia( "media" ).others;
ASSERT_EQ( 0u, media.size() );
}
TEST_F( Medias, SearchByLabel )
{
auto m = ml->addFile( "media.mkv" );
auto media = ml->searchMedia( "otter" );
auto media = ml->searchMedia( "otter" ).others;
ASSERT_EQ( 0u, media.size() );
auto l = ml->createLabel( "otter" );
m->addLabel( l );
media = ml->searchMedia( "otter" );
media = ml->searchMedia( "otter" ).others;
ASSERT_EQ( 1u, media.size() );
auto l2 = ml->createLabel( "pangolins" );
m->addLabel( l2 );
media = ml->searchMedia( "otter" );
media = ml->searchMedia( "otter" ).others;
ASSERT_EQ( 1u, media.size() );
media = ml->searchMedia( "pangolin" );
media = ml->searchMedia( "pangolin" ).others;
ASSERT_EQ( 1u, media.size() );
m->removeLabel( l );
media = ml->searchMedia( "otter" );
media = ml->searchMedia( "otter" ).others;
ASSERT_EQ( 0u, media.size() );
media = ml->searchMedia( "pangolin" );
media = ml->searchMedia( "pangolin" ).others;
ASSERT_EQ( 1u, media.size() );
m->addLabel( l );
media = ml->searchMedia( "otter" );
media = ml->searchMedia( "otter" ).others;
ASSERT_EQ( 1u, media.size() );
media = ml->searchMedia( "pangolin" );
media = ml->searchMedia( "pangolin" ).others;
ASSERT_EQ( 1u, media.size() );
ml->deleteLabel( l );
media = ml->searchMedia( "otter" );
media = ml->searchMedia( "otter" ).others;
ASSERT_EQ( 0u, media.size() );
media = ml->searchMedia( "pangolin" );
media = ml->searchMedia( "pangolin" ).others;
ASSERT_EQ( 1u, media.size() );
}
TEST_F( Medias, SearchTracks )
{
auto a = ml->createAlbum( "album" );
for ( auto i = 1u; i <= 10u; ++i )
{
auto m = ml->addFile( "track " + std::to_string( i ) + ".mp3" );
a->addTrack( m, i, 1 );
}
auto tracks = ml->searchMedia( "tra" ).tracks;
ASSERT_EQ( 10u, tracks.size() );
tracks = ml->searchMedia( "track 1" ).tracks;
ASSERT_EQ( 2u, tracks.size() );
tracks = ml->searchMedia( "grouik" ).tracks;
ASSERT_EQ( 0u, tracks.size() );
tracks = ml->searchMedia( "rack" ).tracks;
ASSERT_EQ( 0u, tracks.size() );
}
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