Show.cpp 4.8 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 25

#include "Media.h"
26
#include "ShowEpisode.h"
27
#include "MediaLibrary.h"
28

29
#include "database/SqliteTools.h"
30

31 32 33
namespace medialibrary
{

34
const std::string policy::ShowTable::Name = "Show";
35
const std::string policy::ShowTable::PrimaryKeyColumn = "id_show";
36
int64_t Show::* const policy::ShowTable::PrimaryKey = &Show::m_id;
37

38 39
Show::Show( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
40
{
41 42 43 44
    row >> m_id
        >> m_name
        >> m_releaseDate
        >> m_shortSummary
45
        >> m_artworkMrl
46
        >> m_tvdbId;
47 48
}

49 50 51
Show::Show( MediaLibraryPtr ml, const std::string& name )
    : m_ml( ml )
    , m_id( 0 )
52 53
    , m_name( name )
    , m_releaseDate( 0 )
54 55 56
{
}

57
int64_t Show::id() const
58 59 60 61 62
{
    return m_id;
}

const std::string& Show::name() const
63 64 65 66
{
    return m_name;
}

67
time_t Show::releaseDate() const
68
{
69
    return m_releaseDate;
70 71
}

72 73
bool Show::setReleaseDate( time_t date )
{
74
    static const std::string req = "UPDATE " + policy::ShowTable::Name
75
            + " SET release_date = ? WHERE id_show = ?";
76
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, date, m_id ) == false )
77 78 79 80 81 82
        return false;
    m_releaseDate = date;
    return true;
}

const std::string& Show::shortSummary() const
83 84 85 86
{
    return m_shortSummary;
}

87 88
bool Show::setShortSummary( const std::string& summary )
{
89
    static const std::string req = "UPDATE " + policy::ShowTable::Name
90
            + " SET short_summary = ? WHERE id_show = ?";
91
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, summary, m_id ) == false )
92 93 94 95 96
        return false;
    m_shortSummary = summary;
    return true;
}

97
const std::string& Show::artworkMrl() const
98
{
99
    return m_artworkMrl;
100 101
}

102
bool Show::setArtworkMrl( const std::string& artworkMrl )
103
{
104
    static const std::string req = "UPDATE " + policy::ShowTable::Name
105
            + " SET artwork_mrl = ? WHERE id_show = ?";
106
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artworkMrl, m_id ) == false )
107
        return false;
108
    m_artworkMrl = artworkMrl;
109 110
    return true;
}
111 112 113 114 115 116

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

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

127
std::shared_ptr<ShowEpisode> Show::addEpisode( Media& media, const std::string& title, unsigned int episodeNumber)
128
{
129
    auto episode = ShowEpisode::create( m_ml, media.id(), title, episodeNumber, m_id );
130
    media.setShowEpisode( episode );
131
    media.save();
132
    return episode;
133 134
}

135
std::vector<ShowEpisodePtr> Show::episodes()
136 137 138
{
    static const std::string req = "SELECT * FROM " + policy::ShowEpisodeTable::Name
            + " WHERE show_id = ?";
139
    return ShowEpisode::fetchAll<IShowEpisode>( m_ml, req, m_id );
140 141
}

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

155
std::shared_ptr<Show> Show::create( MediaLibraryPtr ml, const std::string& name )
156
{
157
    auto show = std::make_shared<Show>( ml, name );
158 159
    static const std::string req = "INSERT INTO " + policy::ShowTable::Name
            + "(name) VALUES(?)";
160
    if ( insert( ml, show, req, name ) == false )
161 162 163
        return nullptr;
    return show;
}
164 165

}