Show.cpp 4.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23
#include "Show.h"
24
#include "ShowEpisode.h"
25
#include "database/SqliteTools.h"
26

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27 28
const std::string policy::ShowTable::Name = "Show";
const std::string policy::ShowTable::CacheColumn = "id_show";
29
unsigned int Show::* const policy::ShowTable::PrimaryKey = &Show::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
30

31
Show::Show( DBConnection dbConnection, sqlite::Row& row )
32 33
    : m_dbConnection( dbConnection )
{
34 35 36 37 38 39 40
    row >> m_id
        >> m_name
        >> m_releaseDate
        >> m_shortSummary
        >> m_artworkUrl
        >> m_lastSyncDate
        >> m_tvdbId;
41 42
}

43 44 45 46 47
Show::Show( const std::string& name )
    : m_id( 0 )
    , m_name( name )
    , m_releaseDate( 0 )
    , m_lastSyncDate( 0 )
48 49 50
{
}

51 52 53 54 55 56
unsigned int Show::id() const
{
    return m_id;
}

const std::string& Show::name() const
57 58 59 60
{
    return m_name;
}

61
time_t Show::releaseDate() const
62
{
63
    return m_releaseDate;
64 65
}

66 67 68 69
bool Show::setReleaseDate( time_t date )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET release_date = ? WHERE id_show = ?";
70
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
71 72 73 74 75 76
        return false;
    m_releaseDate = date;
    return true;
}

const std::string& Show::shortSummary() const
77 78 79 80
{
    return m_shortSummary;
}

81 82 83 84
bool Show::setShortSummary( const std::string& summary )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET short_summary = ? WHERE id_show = ?";
85
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, summary, m_id ) == false )
86 87 88 89 90 91
        return false;
    m_shortSummary = summary;
    return true;
}

const std::string& Show::artworkUrl() const
92 93 94 95
{
    return m_artworkUrl;
}

96 97 98 99
bool Show::setArtworkUrl( const std::string& artworkUrl )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET artwork_url = ? WHERE id_show = ?";
100
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artworkUrl, m_id ) == false )
101 102 103 104
        return false;
    m_artworkUrl = artworkUrl;
    return true;
}
105

106
time_t Show::lastSyncDate() const
107 108 109 110 111 112 113 114 115
{
    return m_lastSyncDate;
}

const std::string& Show::tvdbId()
{
    return m_tvdbId;
}

116 117 118 119
bool Show::setTvdbId( const std::string& tvdbId )
{
    static const std::string& req = "UPDATE " + policy::ShowTable::Name
            + " SET tvdb_id = ? WHERE id_show = ?";
120
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, tvdbId, m_id ) == false )
121 122 123 124 125
        return false;
    m_tvdbId = tvdbId;
    return true;
}

126
std::shared_ptr<ShowEpisode> Show::addEpisode(const std::string& title, unsigned int episodeNumber)
127 128 129 130
{
    return ShowEpisode::create( m_dbConnection, title, episodeNumber, m_id );
}

131
std::vector<ShowEpisodePtr> Show::episodes()
132 133 134
{
    static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name
            + " WHERE show_id = ?";
135
    return ShowEpisode::fetchAll<IShowEpisode>( m_dbConnection, req, m_id );
136 137
}

138
bool Show::createTable(DBConnection dbConnection)
139
{
140
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowTable::Name + "("
141
                        "id_show INTEGER PRIMARY KEY AUTOINCREMENT,"
142 143 144
                        "name TEXT, "
                        "release_date UNSIGNED INTEGER,"
                        "short_summary TEXT,"
145 146 147 148
                        "artwork_url TEXT,"
                        "last_sync_date UNSIGNED INTEGER,"
                        "tvdb_id TEXT"
                    ")";
149
    return sqlite::Tools::executeRequest( dbConnection, req );
150
}
151

152
std::shared_ptr<Show> Show::create(DBConnection dbConnection, const std::string& name )
153 154 155 156 157 158 159 160 161
{
    auto show = std::make_shared<Show>( name );
    static const std::string req = "INSERT INTO " + policy::ShowTable::Name
            + "(name) VALUES(?)";
    if ( _Cache::insert( dbConnection, show, req, name ) == false )
        return nullptr;
    show->m_dbConnection = dbConnection;
    return show;
}