Show.cpp 4.97 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 24 25 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "Show.h"
28 29

#include "Media.h"
30
#include "ShowEpisode.h"
31
#include "MediaLibrary.h"
32

33
#include "database/SqliteTools.h"
34
#include "database/SqliteQuery.h"
35

36 37 38
namespace medialibrary
{

39
const std::string policy::ShowTable::Name = "Show";
40
const std::string policy::ShowTable::PrimaryKeyColumn = "id_show";
41
int64_t Show::* const policy::ShowTable::PrimaryKey = &Show::m_id;
42

43 44
Show::Show( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
45
{
46 47 48 49
    row >> m_id
        >> m_name
        >> m_releaseDate
        >> m_shortSummary
50
        >> m_artworkMrl
51
        >> m_tvdbId;
52 53
}

54 55 56
Show::Show( MediaLibraryPtr ml, const std::string& name )
    : m_ml( ml )
    , m_id( 0 )
57 58
    , m_name( name )
    , m_releaseDate( 0 )
59 60 61
{
}

62
int64_t Show::id() const
63 64 65 66 67
{
    return m_id;
}

const std::string& Show::name() const
68 69 70 71
{
    return m_name;
}

72
time_t Show::releaseDate() const
73
{
74
    return m_releaseDate;
75 76
}

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

const std::string& Show::shortSummary() const
88 89 90 91
{
    return m_shortSummary;
}

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

102
const std::string& Show::artworkMrl() const
103
{
104
    return m_artworkMrl;
105 106
}

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

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

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

132
std::shared_ptr<ShowEpisode> Show::addEpisode( Media& media, const std::string& title, unsigned int episodeNumber)
133
{
134
    auto episode = ShowEpisode::create( m_ml, media.id(), title, episodeNumber, m_id );
135
    media.setShowEpisode( episode );
136
    media.save();
137
    return episode;
138 139
}

140
Query<IShowEpisode> Show::episodes()
141
{
142
    static const std::string req = "FROM " + policy::ShowEpisodeTable::Name
143
            + " WHERE show_id = ?";
144
    return make_query<ShowEpisode, IShowEpisode>( m_ml, "*", req, m_id );
145 146
}

147 148 149 150 151 152 153 154 155 156
uint32_t Show::nbSeasons() const
{
    return 0;
}

uint32_t Show::nbEpisodes() const
{
    return 0;
}

157
void Show::createTable( sqlite::Connection* dbConnection )
158
{
159
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ShowTable::Name + "("
160
                        "id_show INTEGER PRIMARY KEY AUTOINCREMENT,"
161 162 163
                        "name TEXT, "
                        "release_date UNSIGNED INTEGER,"
                        "short_summary TEXT,"
164
                        "artwork_mrl TEXT,"
165 166
                        "tvdb_id TEXT"
                    ")";
167
    sqlite::Tools::executeRequest( dbConnection, req );
168
}
169

170
std::shared_ptr<Show> Show::create( MediaLibraryPtr ml, const std::string& name )
171
{
172
    auto show = std::make_shared<Show>( ml, name );
173 174
    static const std::string req = "INSERT INTO " + policy::ShowTable::Name
            + "(name) VALUES(?)";
175
    if ( insert( ml, show, req, name ) == false )
176 177 178
        return nullptr;
    return show;
}
179 180

}