Movie.cpp 3.82 KB
Newer Older
1 2
#include "Movie.h"
#include "File.h"
3
#include "database/SqliteTools.h"
4 5 6 7 8

const std::string policy::MovieTable::Name = "Movie";
const std::string policy::MovieTable::CacheColumn = "id_movie";
unsigned int Movie::* const policy::MovieTable::PrimaryKey = &Movie::m_id;

9
Movie::Movie( DBConnection dbConnection, sqlite3_stmt* stmt )
10 11
    : m_dbConnection( dbConnection )
{
12 13 14 15 16 17
    m_id = sqlite::Traits<unsigned int>::Load( stmt, 0 );
    m_title = sqlite::Traits<std::string>::Load( stmt, 1 );
    m_releaseDate = sqlite::Traits<time_t>::Load( stmt, 2 );
    m_summary = sqlite::Traits<std::string>::Load( stmt, 3 );
    m_artworkUrl = sqlite::Traits<std::string>::Load( stmt, 4 );
    m_imdbId = sqlite::Traits<std::string>::Load( stmt, 5 );
18 19 20
}

Movie::Movie( const std::string& title )
21
    : m_id( 0 )
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    , m_title( title )
    , m_releaseDate( 0 )
{
}


unsigned int Movie::id() const
{
    return m_id;
}

const std::string&Movie::title() const
{
    return m_title;
}

time_t Movie::releaseDate() const
{
    return m_releaseDate;
}

bool Movie::setReleaseDate( time_t date )
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
            + " SET release_date = ? WHERE id_movie = ?";
47
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
48 49 50 51 52 53 54 55 56 57 58 59 60 61
        return false;
    m_releaseDate = date;
    return true;
}

const std::string&Movie::shortSummary() const
{
    return m_summary;
}

bool Movie::setShortSummary( const std::string& summary )
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
            + " SET summary = ? WHERE id_movie = ?";
62
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, summary, m_id ) == false )
63 64 65 66 67 68 69 70 71 72 73 74 75 76
        return false;
    m_summary = summary;
    return true;
}

const std::string&Movie::artworkUrl() const
{
    return m_artworkUrl;
}

bool Movie::setArtworkUrl( const std::string& artworkUrl )
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
            + " SET artwork_url = ? WHERE id_movie = ?";
77
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artworkUrl, m_id ) == false )
78 79 80 81 82 83 84 85 86 87 88 89 90 91
        return false;
    m_artworkUrl = artworkUrl;
    return true;
}

const std::string& Movie::imdbId() const
{
    return m_imdbId;
}

bool Movie::setImdbId( const std::string& imdbId )
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
            + " SET imdb_id = ? WHERE id_movie = ?";
92
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, imdbId, m_id ) == false )
93 94 95 96 97 98 99
        return false;
    m_imdbId = imdbId;
    return true;
}

bool Movie::destroy()
{
100
    auto fs = files();
101 102 103 104 105 106 107
    for ( auto& f : fs )
    {
        File::discard( std::static_pointer_cast<File>( f ) );
    }
    return _Cache::destroy( m_dbConnection, this );
}

108
std::vector<FilePtr> Movie::files()
109 110 111
{
    static const std::string req = "SELECT * FROM " + policy::FileTable::Name
            + " WHERE movie_id = ?";
112
    return File::fetchAll( m_dbConnection, req, m_id );
113 114
}

115
bool Movie::createTable( DBConnection dbConnection )
116 117 118 119 120 121 122 123 124 125
{
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MovieTable::Name
            + "("
                "id_movie INTEGER PRIMARY KEY AUTOINCREMENT,"
                "title TEXT UNIQUE ON CONFLICT FAIL,"
                "release_date UNSIGNED INTEGER,"
                "summary TEXT,"
                "artwork_url TEXT,"
                "imdb_id TEXT"
            ")";
126
    return sqlite::Tools::executeRequest( dbConnection, req );
127 128
}

129
MoviePtr Movie::create(DBConnection dbConnection, const std::string& title )
130 131 132 133 134 135 136 137 138
{
    auto movie = std::make_shared<Movie>( title );
    static const std::string req = "INSERT INTO " + policy::MovieTable::Name
            + "(title) VALUES(?)";
    if ( _Cache::insert( dbConnection, movie, req, title ) == false )
        return nullptr;
    movie->m_dbConnection = dbConnection;
    return movie;
}