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

Add a Destroy Album/Show method

parent 41c283c9
......@@ -20,6 +20,7 @@ class IAlbum
virtual bool setArtworkUrl( const std::string& artworkUrl ) = 0;
virtual bool tracks( std::vector<std::shared_ptr<IAlbumTrack>>& tracks ) const = 0;
virtual AlbumTrackPtr addTrack( const std::string& name, unsigned int trackId ) = 0;
virtual bool destroy() = 0;
};
#endif // IALBUM_H
......@@ -19,6 +19,7 @@ class IShow
virtual bool setTvdbId( const std::string& id ) = 0;
virtual ShowEpisodePtr addEpisode( const std::string& title, unsigned int episodeNumber ) = 0;
virtual bool episodes( std::vector<ShowEpisodePtr>& episodes ) = 0;
virtual bool destroy() = 0;
};
#endif // ISHOW_H
......@@ -109,6 +109,19 @@ AlbumTrackPtr Album::addTrack( const std::string& name, unsigned int trackNb )
return AlbumTrack::create( m_dbConnection, m_id, name, trackNb );
}
bool Album::destroy()
{
std::vector<AlbumTrackPtr> ts;
if ( tracks( ts ) == false )
return false;
//FIXME: Have a single request to fetch all files at once, instead of having one per track
for ( auto& t : ts )
{
t->destroy();
}
return _Cache::destroy( m_dbConnection, this );
}
bool Album::createTable( sqlite3* dbConnection )
{
const char* req = "CREATE TABLE IF NOT EXISTS Album("
......
......@@ -41,6 +41,7 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
virtual time_t lastSyncDate() const;
virtual bool tracks( std::vector<std::shared_ptr<IAlbumTrack>>& tracks ) const;
virtual AlbumTrackPtr addTrack( const std::string& name, unsigned int trackNb );
virtual bool destroy();
static bool createTable( sqlite3* dbConnection );
static AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag );
......
......@@ -113,6 +113,19 @@ bool Show::episodes( std::vector<ShowEpisodePtr>& episodes )
return SqliteTools::fetchAll<ShowEpisode>( m_dbConnection, req, episodes, m_id );
}
bool Show::destroy()
{
std::vector<ShowEpisodePtr> eps;
if ( episodes( eps ) == false )
return false;
//FIXME: This is suboptimal. Each episode::destroy() will fire a SQL request of its own
for ( auto& t : eps )
{
t->destroy();
}
return _Cache::destroy( m_dbConnection, this );
}
bool Show::createTable(sqlite3* dbConnection)
{
const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowTable::Name + "("
......
......@@ -38,6 +38,7 @@ class Show : public IShow, public Cache<Show, IShow, policy::ShowTable>
virtual bool setTvdbId( const std::string& summary );
virtual ShowEpisodePtr addEpisode( const std::string& title, unsigned int episodeNumber );
virtual bool episodes( std::vector<ShowEpisodePtr>& episodes );
virtual bool destroy();
static bool createTable( sqlite3* dbConnection );
static ShowPtr create(sqlite3* dbConnection, const std::string& name );
......
......@@ -198,3 +198,18 @@ TEST_F( Albums, FetchAlbumFromTrack )
ASSERT_NE( a, nullptr );
ASSERT_EQ( a->name(), "album" );
}
TEST_F( Albums, DestroyAlbum )
{
auto a = ml->createAlbum( "album" );
a->setName( "album" );
auto f = ml->addFile( "file" );
auto t = a->addTrack( "track 1", 1 );
f->setAlbumTrack( t );
bool res = a->destroy();
ASSERT_TRUE( res );
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
}
......@@ -238,3 +238,16 @@ TEST_F( Shows, DeleteShowEpisode )
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
}
TEST_F( Shows, DeleteShow )
{
auto show = ml->createShow( "show" );
auto e = show->addEpisode( "episode 1", 1 );
auto f = ml->addFile( "file" );
f->setShowEpisode( e );
bool res = show->destroy();
ASSERT_TRUE( res );
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
}
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