ShowEpisode.cpp 6.02 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.
 *****************************************************************************/

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
23
#include "ShowEpisode.h"
24
#include "database/SqliteTools.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
25
#include "Show.h"
26
#include "Media.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27

28
const std::string policy::ShowEpisodeTable::Name = "ShowEpisode";
29
const std::string policy::ShowEpisodeTable::PrimaryKeyColumn = "show_id";
30
unsigned int ShowEpisode::* const policy::ShowEpisodeTable::PrimaryKey = &ShowEpisode::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
31

32
ShowEpisode::ShowEpisode(DBConnection dbConnection, sqlite::Row& row )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
33 34
    : m_dbConnection( dbConnection )
{
35
    row >> m_id
36
        >> m_mediaId
37
        >> m_artworkMrl
38 39 40 41 42 43
        >> m_episodeNumber
        >> m_name
        >> m_seasonNumber
        >> m_shortSummary
        >> m_tvdbId
        >> m_showId;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44 45
}

46
ShowEpisode::ShowEpisode( unsigned int mediaId, const std::string& name, unsigned int episodeNumber, unsigned int showId )
47
    : m_id( 0 )
48
    , m_mediaId( mediaId )
49 50 51 52 53 54 55 56 57 58 59
    , m_episodeNumber( episodeNumber )
    , m_name( name )
    , m_seasonNumber( 0 )
    , m_showId( showId )
{
}

unsigned int ShowEpisode::id() const
{
    return m_id;
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60

61
const std::string& ShowEpisode::artworkMrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
62
{
63
    return m_artworkMrl;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64 65
}

66
bool ShowEpisode::setArtworkMrl( const std::string& artworkMrl )
67 68
{
    static const std::string req = "UPDATE " + policy::ShowEpisodeTable::Name
69 70
            + " SET artwork_mrl = ? WHERE id_episode = ?";
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artworkMrl, m_id ) == false )
71
        return false;
72
    m_artworkMrl = artworkMrl;
73 74 75 76
    return true;
}

unsigned int ShowEpisode::episodeNumber() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
77 78 79 80
{
    return m_episodeNumber;
}

81
const std::string& ShowEpisode::name() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
82 83 84 85
{
    return m_name;
}

86
unsigned int ShowEpisode::seasonNumber() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
87 88 89 90
{
    return m_seasonNumber;
}

91 92 93 94
bool ShowEpisode::setSeasonNumber( unsigned int seasonNumber )
{
    static const std::string req = "UPDATE " + policy::ShowEpisodeTable::Name
            + " SET season_number = ? WHERE id_episode = ?";
95
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, seasonNumber, m_id ) == false )
96 97 98 99 100 101
        return false;
    m_seasonNumber = seasonNumber;
    return true;
}

const std::string& ShowEpisode::shortSummary() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
102 103 104 105
{
    return m_shortSummary;
}

106 107 108 109
bool ShowEpisode::setShortSummary( const std::string& summary )
{
    static const std::string req = "UPDATE " + policy::ShowEpisodeTable::Name
            + " SET episode_summary = ? WHERE id_episode = ?";
110
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, summary, m_id ) == false )
111 112 113 114 115 116
        return false;
    m_shortSummary = summary;
    return true;
}

const std::string& ShowEpisode::tvdbId() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
117 118 119 120
{
    return m_tvdbId;
}

121 122 123 124
bool ShowEpisode::setTvdbId( const std::string& tvdbId )
{
    static const std::string req = "UPDATE " + policy::ShowEpisodeTable::Name
            + " SET tvdb_id = ? WHERE id_episode = ?";
125
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, tvdbId, m_id ) == false )
126 127 128 129 130
        return false;
    m_tvdbId = tvdbId;
    return true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
131
std::shared_ptr<IShow> ShowEpisode::show()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
132
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
133
    if ( m_show == nullptr && m_showId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
134
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
135
        m_show = Show::fetch( m_dbConnection, m_showId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
136 137 138 139
    }
    return m_show;
}

140
std::vector<MediaPtr> ShowEpisode::files()
141
{
142
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
143
            + " WHERE show_episode_id = ?";
144
    return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
145 146
}

147
bool ShowEpisode::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
148
{
149 150
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowEpisodeTable::Name
            + "("
151
                "id_episode INTEGER PRIMARY KEY AUTOINCREMENT,"
152
                "media_id UNSIGNED INTEGER NOT NULL,"
153
                "artwork_mrl TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
154
                "episode_number UNSIGNED INT,"
155
                "title TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
156 157 158 159
                "season_number UNSIGNED INT,"
                "episode_summary TEXT,"
                "tvdb_id TEXT,"
                "show_id UNSIGNED INT,"
160 161
                "FOREIGN KEY(media_id) REFERENCES " + policy::MediaTable::Name
                    + "(id_media) ON DELETE CASCADE,"
162
                "FOREIGN KEY(show_id) REFERENCES " + policy::ShowTable::Name + "(id_show)"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
163
            ")";
164
    return sqlite::Tools::executeRequest( dbConnection, req );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
165
}
166

167
std::shared_ptr<ShowEpisode> ShowEpisode::create( DBConnection dbConnection, unsigned int mediaId, const std::string& title, unsigned int episodeNumber, unsigned int showId )
168
{
169
    auto episode = std::make_shared<ShowEpisode>( mediaId, title, episodeNumber, showId );
170
    static const std::string req = "INSERT INTO " + policy::ShowEpisodeTable::Name
171 172
            + "(media_id, episode_number, title, show_id) VALUES(?, ? , ?, ?)";
    if ( insert( dbConnection, episode, req, mediaId, episodeNumber, title, showId ) == false )
173 174 175 176
        return nullptr;
    episode->m_dbConnection = dbConnection;
    return episode;
}
177 178 179 180 181 182

ShowEpisodePtr ShowEpisode::fromMedia( DBConnection dbConnection, unsigned int mediaId )
{
    static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name + " WHERE media_id = ?";
    return fetch( dbConnection, req, mediaId );
}