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

Handle linking a file to an episode

parent 07c314fe
...@@ -29,6 +29,7 @@ class IFile ...@@ -29,6 +29,7 @@ class IFile
virtual bool setAlbumTrack(AlbumTrackPtr albumTrack ) = 0; virtual bool setAlbumTrack(AlbumTrackPtr albumTrack ) = 0;
virtual unsigned int duration() const = 0; virtual unsigned int duration() const = 0;
virtual std::shared_ptr<IShowEpisode> showEpisode() = 0; virtual std::shared_ptr<IShowEpisode> showEpisode() = 0;
virtual bool setShowEpisode( ShowEpisodePtr showEpisode ) = 0;
virtual int playCount() const = 0; virtual int playCount() const = 0;
virtual const std::string& mrl() const = 0; virtual const std::string& mrl() const = 0;
virtual bool addLabel( LabelPtr label ) = 0; virtual bool addLabel( LabelPtr label ) = 0;
......
...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
class IShow; class IShow;
#include <memory> #include "IMediaLibrary.h"
#include <string>
class IShowEpisode class IShowEpisode
{ {
...@@ -24,6 +23,11 @@ class IShowEpisode ...@@ -24,6 +23,11 @@ class IShowEpisode
virtual const std::string& tvdbId() const = 0; virtual const std::string& tvdbId() const = 0;
virtual bool setTvdbId( const std::string& tvdbId ) = 0; virtual bool setTvdbId( const std::string& tvdbId ) = 0;
virtual std::shared_ptr<IShow> show() = 0; virtual std::shared_ptr<IShow> show() = 0;
virtual bool files( std::vector<FilePtr>& files ) = 0;
/**
* @brief destroy Deletes the album track and the file(s) associated
*/
virtual bool destroy() = 0;
}; };
#endif // ISHOWEPISODE_H #endif // ISHOWEPISODE_H
...@@ -42,7 +42,6 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable> ...@@ -42,7 +42,6 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
virtual bool tracks( std::vector<std::shared_ptr<IAlbumTrack>>& tracks ) const; virtual bool tracks( std::vector<std::shared_ptr<IAlbumTrack>>& tracks ) const;
virtual AlbumTrackPtr addTrack( const std::string& name, unsigned int trackNb ); virtual AlbumTrackPtr addTrack( const std::string& name, unsigned int trackNb );
static bool createTable( sqlite3* dbConnection ); static bool createTable( sqlite3* dbConnection );
static AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag ); static AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag );
......
...@@ -101,11 +101,9 @@ bool AlbumTrack::destroy() ...@@ -101,11 +101,9 @@ bool AlbumTrack::destroy()
std::cerr << "No files found for AlbumTrack " << m_id << std::endl; std::cerr << "No files found for AlbumTrack " << m_id << std::endl;
for ( auto& f : fs ) for ( auto& f : fs )
{ {
if ( File::discard( std::static_pointer_cast<File>( f ) ) == false ) // Ignore failures to discard from cache, we might want to discard records from
{ // cache in a near future to avoid running out of memory on mobile devices
std::cerr << "Failed to discard a file from cache"; File::discard( std::static_pointer_cast<File>( f ) );
return false;
}
} }
return _Cache::destroy( m_dbConnection, this ); return _Cache::destroy( m_dbConnection, this );
} }
......
...@@ -83,6 +83,17 @@ std::shared_ptr<IShowEpisode> File::showEpisode() ...@@ -83,6 +83,17 @@ std::shared_ptr<IShowEpisode> File::showEpisode()
return m_showEpisode; return m_showEpisode;
} }
bool File::setShowEpisode(ShowEpisodePtr showEpisode)
{
static const std::string req = "UPDATE " + policy::FileTable::Name
+ " SET show_episode_id = ? WHERE id_file = ?";
if ( SqliteTools::executeUpdate( m_dbConnection, req, showEpisode->id(), m_id ) == false )
return false;
m_showEpisodeId = showEpisode->id();
m_showEpisode = showEpisode;
return true;
}
std::vector<std::shared_ptr<ILabel> > File::labels() std::vector<std::shared_ptr<ILabel> > File::labels()
{ {
std::vector<std::shared_ptr<ILabel> > labels; std::vector<std::shared_ptr<ILabel> > labels;
...@@ -119,7 +130,9 @@ bool File::createTable(sqlite3* connection) ...@@ -119,7 +130,9 @@ bool File::createTable(sqlite3* connection)
"show_episode_id UNSIGNED INTEGER," "show_episode_id UNSIGNED INTEGER,"
"mrl TEXT UNIQUE ON CONFLICT FAIL," "mrl TEXT UNIQUE ON CONFLICT FAIL,"
"FOREIGN KEY (album_track_id) REFERENCES " + policy::AlbumTrackTable::Name "FOREIGN KEY (album_track_id) REFERENCES " + policy::AlbumTrackTable::Name
+ "(id_track) ON DELETE CASCADE" + "(id_track) ON DELETE CASCADE,"
"FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
+ "(id_episode) ON DELETE CASCADE"
")"; ")";
return SqliteTools::executeRequest( connection, req ); return SqliteTools::executeRequest( connection, req );
} }
......
...@@ -50,6 +50,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy:: ...@@ -50,6 +50,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy::
virtual bool setAlbumTrack( AlbumTrackPtr albumTrack ); virtual bool setAlbumTrack( AlbumTrackPtr albumTrack );
virtual unsigned int duration() const; virtual unsigned int duration() const;
virtual std::shared_ptr<IShowEpisode> showEpisode(); virtual std::shared_ptr<IShowEpisode> showEpisode();
virtual bool setShowEpisode( ShowEpisodePtr showEpisode );
virtual bool addLabel( LabelPtr label ); virtual bool addLabel( LabelPtr label );
virtual bool removeLabel( LabelPtr label ); virtual bool removeLabel( LabelPtr label );
virtual std::vector<std::shared_ptr<ILabel> > labels(); virtual std::vector<std::shared_ptr<ILabel> > labels();
...@@ -71,7 +72,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy:: ...@@ -71,7 +72,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy::
// Auto fetched related properties // Auto fetched related properties
Album* m_album; Album* m_album;
AlbumTrackPtr m_albumTrack; AlbumTrackPtr m_albumTrack;
std::shared_ptr<ShowEpisode> m_showEpisode; ShowEpisodePtr m_showEpisode;
friend class Cache<File, IFile, policy::FileTable, policy::FileCache>; friend class Cache<File, IFile, policy::FileTable, policy::FileCache>;
friend struct policy::FileTable; friend struct policy::FileTable;
......
#include "ShowEpisode.h" #include "ShowEpisode.h"
#include "SqliteTools.h" #include "SqliteTools.h"
#include "Show.h" #include "Show.h"
#include "File.h"
const std::string policy::ShowEpisodeTable::Name = "ShowEpisode"; const std::string policy::ShowEpisodeTable::Name = "ShowEpisode";
const std::string policy::ShowEpisodeTable::CacheColumn = "id_show"; const std::string policy::ShowEpisodeTable::CacheColumn = "show_id";
unsigned int ShowEpisode::* const policy::ShowEpisodeTable::PrimaryKey = &ShowEpisode::m_id; unsigned int ShowEpisode::* const policy::ShowEpisodeTable::PrimaryKey = &ShowEpisode::m_id;
ShowEpisode::ShowEpisode( sqlite3* dbConnection, sqlite3_stmt* stmt ) ShowEpisode::ShowEpisode( sqlite3* dbConnection, sqlite3_stmt* stmt )
...@@ -119,6 +120,23 @@ std::shared_ptr<IShow> ShowEpisode::show() ...@@ -119,6 +120,23 @@ std::shared_ptr<IShow> ShowEpisode::show()
return m_show; return m_show;
} }
bool ShowEpisode::files( std::vector<FilePtr>& files )
{
static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " WHERE show_episode_id = ?";
return SqliteTools::fetchAll<File>( m_dbConnection, req, files, m_id );
}
bool ShowEpisode::destroy()
{
std::vector<FilePtr> fs;
if ( files( fs ) == false )
return false;
for ( auto& f : fs )
File::discard( std::static_pointer_cast<File>( f ) );
return _Cache::destroy( m_dbConnection, this );
}
bool ShowEpisode::createTable(sqlite3* dbConnection) bool ShowEpisode::createTable(sqlite3* dbConnection)
{ {
const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowEpisodeTable::Name const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowEpisodeTable::Name
......
...@@ -40,6 +40,8 @@ class ShowEpisode : public IShowEpisode, public Cache<ShowEpisode, IShowEpisode, ...@@ -40,6 +40,8 @@ class ShowEpisode : public IShowEpisode, public Cache<ShowEpisode, IShowEpisode,
virtual const std::string& tvdbId() const; virtual const std::string& tvdbId() const;
virtual bool setTvdbId( const std::string& tvdbId ); virtual bool setTvdbId( const std::string& tvdbId );
virtual std::shared_ptr<IShow> show(); virtual std::shared_ptr<IShow> show();
virtual bool files( std::vector<FilePtr>& files );
virtual bool destroy();
static bool createTable( sqlite3* dbConnection ); static bool createTable( sqlite3* dbConnection );
static ShowEpisodePtr create(sqlite3* dbConnection, const std::string& title, unsigned int episodeNumber, unsigned int showId ); static ShowEpisodePtr create(sqlite3* dbConnection, const std::string& title, unsigned int episodeNumber, unsigned int showId );
......
...@@ -196,3 +196,45 @@ TEST_F( Shows, SetEpisodeTvdbId ) ...@@ -196,3 +196,45 @@ TEST_F( Shows, SetEpisodeTvdbId )
show->episodes( episodes ); show->episodes( episodes );
ASSERT_EQ( episodes[0]->tvdbId(), e->tvdbId() ); ASSERT_EQ( episodes[0]->tvdbId(), e->tvdbId() );
} }
////////////////////////////////////////////////////
// Files links:
////////////////////////////////////////////////////
TEST_F( Shows, FileSetShowEpisode )
{
auto show = ml->createShow( "show" );
auto e = show->addEpisode( "episode 1", 1 );
auto f = ml->addFile( "file" );
ASSERT_EQ( f->showEpisode(), nullptr );
f->setShowEpisode( e );
ASSERT_EQ( f->showEpisode(), e );
delete ml;
SetUp();
f = ml->file( "file" );
e = f->showEpisode();
ASSERT_NE( e, nullptr );
ASSERT_EQ( e->name(), "episode 1" );
}
TEST_F( Shows, DeleteShowEpisode )
{
auto show = ml->createShow( "show" );
auto e = show->addEpisode( "episode 1", 1 );
auto f = ml->addFile( "file" );
f->setShowEpisode( e );
e->destroy();
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
delete ml;
SetUp();
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