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

IPlaylist: Pass the entire media to insertion functions

parent ff3852b2
......@@ -49,7 +49,7 @@ public:
/// \param media The media to add
/// \return true on success, false on failure.
///
virtual bool append( int64_t mediaId ) = 0;
virtual bool append( const IMedia& media ) = 0;
///
/// \brief add Add a media to the playlist at the given position.
/// Valid positions start at 1. 0 means appending.
......@@ -57,7 +57,7 @@ public:
/// \param position The position of this new media
/// \return true on success, false on failure
///
virtual bool add( int64_t mediaId, unsigned int position ) = 0;
virtual bool add( const IMedia& media, unsigned int position ) = 0;
///
/// \brief move Change the position of a media
/// \param mediaId The media to move reorder
......
......@@ -123,19 +123,19 @@ Query<IMedia> Playlist::searchMedia( const std::string& pattern,
return Media::searchInPlaylist( m_ml, pattern, m_id, params );
}
bool Playlist::append( int64_t mediaId )
bool Playlist::append( const IMedia& media )
{
return add( mediaId, 0 );
return add( media, 0 );
}
bool Playlist::add( int64_t mediaId, unsigned int position )
bool Playlist::add( const IMedia& media, unsigned int position )
{
static const std::string req = "INSERT INTO PlaylistMediaRelation(media_id, playlist_id, position) VALUES(?, ?, ?)";
// position isn't a foreign key, but we want it to be passed as NULL if it equals to 0
// When the position is NULL, the insertion triggers takes care of counting the number of records to auto append.
try
{
return sqlite::Tools::executeInsert( m_ml->getConn(), req, mediaId, m_id, sqlite::ForeignKey{ position } );
return sqlite::Tools::executeInsert( m_ml->getConn(), req, media.id(), m_id, sqlite::ForeignKey{ position } );
}
catch (const sqlite::errors::ConstraintViolation& ex)
{
......
......@@ -60,8 +60,8 @@ public:
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 append( const IMedia& media ) override;
virtual bool add( const IMedia& media, unsigned int position ) override;
virtual bool move( int64_t mediaId, unsigned int position ) override;
virtual bool remove( int64_t mediaId ) override;
std::shared_ptr<File> addFile( const fs::IFile& fileFs, int64_t parentFolderId,
......
......@@ -169,7 +169,7 @@ Status MetadataAnalyzer::run( IItem& item )
auto media = std::static_pointer_cast<Media>( item.media() );
if ( item.parentPlaylist() != nullptr )
item.parentPlaylist()->add( media->id(), item.parentPlaylistIndex() );
item.parentPlaylist()->add( *media, item.parentPlaylistIndex() );
if ( alreadyInParser == true )
return Status::Discarded;
......@@ -295,7 +295,7 @@ void MetadataAnalyzer::addPlaylistElement( IItem& item,
if ( media != nullptr )
{
LOG_INFO( "Media for ", mrl, " already exists, adding it to the playlist ", mrl );
playlistPtr->add( media->id(), subitem.parentPlaylistIndex() );
playlistPtr->add( *media, subitem.parentPlaylistIndex() );
return;
}
// Create Media, etc.
......@@ -315,7 +315,7 @@ void MetadataAnalyzer::addPlaylistElement( IItem& item,
auto externalFile = externalMedia->addExternalMrl( mrl, IFile::Type::Main );
if ( externalFile == nullptr )
LOG_ERROR( "Failed to create external file for ", mrl, " in the playlist ", item.mrl() );
playlistPtr->add( externalMedia->id(), subitem.parentPlaylistIndex() );
playlistPtr->add( *externalMedia, subitem.parentPlaylistIndex() );
t2->commit();
return;
}
......
......@@ -109,7 +109,7 @@ TEST_F( Playlists, FetchAll )
TEST_F( Playlists, Add )
{
auto m = ml->addMedia( "file.mkv" );
auto res = pl->append( m->id() );
auto res = pl->append( *m );
ASSERT_TRUE( res );
auto media = pl->media()->all();
ASSERT_EQ( 1u, media.size() );
......@@ -122,7 +122,7 @@ TEST_F( Playlists, Append )
{
auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
ASSERT_NE( nullptr, m );
pl->append( m->id() );
pl->append( *m );
}
auto media = pl->media()->all();
ASSERT_EQ( 5u, media.size() );
......@@ -139,16 +139,16 @@ TEST_F( Playlists, Insert )
{
auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
ASSERT_NE( nullptr, m );
auto res = pl->append( m->id() );
auto res = pl->append( *m );
ASSERT_TRUE( res );
}
// [<1,1>,<2,2>,<3,3>]
auto firstMedia = ml->addMedia( "first.mkv" );
pl->add( firstMedia->id(), 1 );
pl->add( *firstMedia, 1 );
// [<4,1>,<1,2>,<2,3>,<3,4>]
auto middleMedia = ml->addMedia( "middle.mkv" );
pl->add( middleMedia->id(), 3 );
pl->add( *middleMedia, 3 );
// [<4,1>,<1,2>,<5,3>,<2,4>,<3,5>]
auto media = pl->media()->all();
ASSERT_EQ( 5u, media.size() );
......@@ -166,7 +166,7 @@ TEST_F( Playlists, Move )
{
auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
ASSERT_NE( nullptr, m );
auto res = pl->append( m->id() );
auto res = pl->append( *m );
ASSERT_TRUE( res );
}
// [<1,1>,<2,2>,<3,3>,<4,4>,<5,5>]
......@@ -188,7 +188,7 @@ TEST_F( Playlists, Remove )
{
auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
ASSERT_NE( nullptr, m );
auto res = pl->append( m->id() );
auto res = pl->append( *m );
ASSERT_TRUE( res );
}
// [<1,1>,<2,2>,<3,3>,<4,4>,<5,5>]
......@@ -213,7 +213,7 @@ TEST_F( Playlists, DeleteFile )
{
auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
ASSERT_NE( nullptr, m );
auto res = pl->append( m->id() );
auto res = pl->append( *m );
ASSERT_TRUE( res );
}
// [<1,1>,<2,2>,<3,3>,<4,4>,<5,5>]
......@@ -322,9 +322,9 @@ TEST_F( Playlists, Sort )
TEST_F( Playlists, AddDuplicate )
{
auto m = ml->addMedia( "file.mkv" );
auto res = pl->append( m->id() );
auto res = pl->append( *m );
ASSERT_TRUE( res );
res = pl->append( m->id() );
res = pl->append( *m );
ASSERT_TRUE( res );
auto media = pl->media()->all();
ASSERT_EQ( 2u, media.size() );
......@@ -350,8 +350,8 @@ TEST_F( Playlists, SearchMedia )
m3->setType( IMedia::Type::Audio );
m3->save();
pl->append( m1->id() );
pl->append( m2->id() );
pl->append( *m1 );
pl->append( *m2 );
auto media = ml->searchMedia( "otter", nullptr )->all();
ASSERT_EQ( 2u, media.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