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
virtual bool setAlbumTrack(AlbumTrackPtr albumTrack ) = 0;
virtual unsigned int duration() const = 0;
virtual std::shared_ptr<IShowEpisode> showEpisode() = 0;
virtual bool setShowEpisode( ShowEpisodePtr showEpisode ) = 0;
virtual int playCount() const = 0;
virtual const std::string& mrl() const = 0;
virtual bool addLabel( LabelPtr label ) = 0;
......
......@@ -3,8 +3,7 @@
class IShow;
#include <memory>
#include <string>
#include "IMediaLibrary.h"
class IShowEpisode
{
......@@ -24,6 +23,11 @@ class IShowEpisode
virtual const std::string& tvdbId() const = 0;
virtual bool setTvdbId( const std::string& tvdbId ) = 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
......@@ -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 AlbumTrackPtr addTrack( const std::string& name, unsigned int trackNb );
static bool createTable( sqlite3* dbConnection );
static AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag );
......
......@@ -101,11 +101,9 @@ bool AlbumTrack::destroy()
std::cerr << "No files found for AlbumTrack " << m_id << std::endl;
for ( auto& f : fs )
{
if ( File::discard( std::static_pointer_cast<File>( f ) ) == false )
{
std::cerr << "Failed to discard a file from cache";
return 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
File::discard( std::static_pointer_cast<File>( f ) );
}
return _Cache::destroy( m_dbConnection, this );
}
......
......@@ -83,6 +83,17 @@ std::shared_ptr<IShowEpisode> File::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> > labels;
......@@ -119,7 +130,9 @@ bool File::createTable(sqlite3* connection)
"show_episode_id UNSIGNED INTEGER,"
"mrl TEXT UNIQUE ON CONFLICT FAIL,"
"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 );
}
......
......@@ -50,6 +50,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy::
virtual bool setAlbumTrack( AlbumTrackPtr albumTrack );
virtual unsigned int duration() const;
virtual std::shared_ptr<IShowEpisode> showEpisode();
virtual bool setShowEpisode( ShowEpisodePtr showEpisode );
virtual bool addLabel( LabelPtr label );
virtual bool removeLabel( LabelPtr label );
virtual std::vector<std::shared_ptr<ILabel> > labels();
......@@ -71,7 +72,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy::
// Auto fetched related properties
Album* m_album;
AlbumTrackPtr m_albumTrack;
std::shared_ptr<ShowEpisode> m_showEpisode;
ShowEpisodePtr m_showEpisode;
friend class Cache<File, IFile, policy::FileTable, policy::FileCache>;
friend struct policy::FileTable;
......
#include "ShowEpisode.h"
#include "SqliteTools.h"
#include "Show.h"
#include "File.h"
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;
ShowEpisode::ShowEpisode( sqlite3* dbConnection, sqlite3_stmt* stmt )
......@@ -119,6 +120,23 @@ std::shared_ptr<IShow> ShowEpisode::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)
{
const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowEpisodeTable::Name
......
......@@ -40,6 +40,8 @@ class ShowEpisode : public IShowEpisode, public Cache<ShowEpisode, IShowEpisode,
virtual const std::string& tvdbId() const;
virtual bool setTvdbId( const std::string& tvdbId );
virtual std::shared_ptr<IShow> show();
virtual bool files( std::vector<FilePtr>& files );
virtual bool destroy();
static bool createTable( sqlite3* dbConnection );
static ShowEpisodePtr create(sqlite3* dbConnection, const std::string& title, unsigned int episodeNumber, unsigned int showId );
......
......@@ -196,3 +196,45 @@ TEST_F( Shows, SetEpisodeTvdbId )
show->episodes( episodes );
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