Movie.cpp 4.71 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 "Movie.h"
28
#include "Media.h"
29
#include "database/SqliteTools.h"
30

31 32 33
namespace medialibrary
{

34
const std::string policy::MovieTable::Name = "Movie";
35
const std::string policy::MovieTable::PrimaryKeyColumn = "id_movie";
36
int64_t Movie::* const policy::MovieTable::PrimaryKey = &Movie::m_id;
37

38 39
Movie::Movie(MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
40
{
41
    row >> m_id
42
        >> m_mediaId
43 44
        >> m_title
        >> m_summary
45
        >> m_artworkMrl
46
        >> m_imdbId;
47 48
}

49
Movie::Movie( MediaLibraryPtr ml, int64_t mediaId, const std::string& title )
50 51
    : m_ml( ml )
    , m_id( 0 )
52
    , m_mediaId( mediaId )
53 54 55 56
    , m_title( title )
{
}

57
int64_t Movie::id() const
58 59 60 61 62 63 64 65 66
{
    return m_id;
}

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

67
const std::string& Movie::shortSummary() const
68 69 70 71 72 73 74 75
{
    return m_summary;
}

bool Movie::setShortSummary( const std::string& summary )
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
            + " SET summary = ? WHERE id_movie = ?";
76
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, summary, m_id ) == false )
77 78 79 80 81
        return false;
    m_summary = summary;
    return true;
}

82
const std::string&Movie::artworkMrl() const
83
{
84
    return m_artworkMrl;
85 86
}

87
bool Movie::setArtworkMrl( const std::string& artworkMrl )
88 89
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
90
            + " SET artwork_mrl = ? WHERE id_movie = ?";
91
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artworkMrl, m_id ) == false )
92
        return false;
93
    m_artworkMrl = artworkMrl;
94 95 96 97 98 99 100 101 102 103 104 105
    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 = ?";
106
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, imdbId, m_id ) == false )
107 108 109 110 111
        return false;
    m_imdbId = imdbId;
    return true;
}

112
std::vector<MediaPtr> Movie::files()
113
{
114
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
115
            + " WHERE movie_id = ?";
116
    return Media::fetchAll<IMedia>( m_ml, req, m_id );
117 118
}

119
void Movie::createTable( sqlite::Connection* dbConnection )
120
{
121
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MovieTable::Name
122 123
            + "("
                "id_movie INTEGER PRIMARY KEY AUTOINCREMENT,"
124
                "media_id UNSIGNED INTEGER NOT NULL,"
125 126
                "title TEXT UNIQUE ON CONFLICT FAIL,"
                "summary TEXT,"
127
                "artwork_mrl TEXT,"
128 129 130
                "imdb_id TEXT,"
                "FOREIGN KEY(media_id) REFERENCES " + policy::MediaTable::Name
                + "(id_media) ON DELETE CASCADE"
131
            ")";
132
    const std::string indexReq = "CREATE INDEX IF NOT EXISTS movie_media_idx ON " +
133
            policy::MovieTable::Name + "(media_id)";
134 135
    sqlite::Tools::executeRequest( dbConnection, req );
    sqlite::Tools::executeRequest( dbConnection, indexReq );
136 137
}

138
std::shared_ptr<Movie> Movie::create(MediaLibraryPtr ml, int64_t mediaId, const std::string& title )
139
{
140
    auto movie = std::make_shared<Movie>( ml, mediaId, title );
141
    static const std::string req = "INSERT INTO " + policy::MovieTable::Name
142
            + "(media_id, title) VALUES(?, ?)";
143
    if ( insert( ml, movie, req, mediaId, title ) == false )
144 145 146
        return nullptr;
    return movie;
}
147

148
MoviePtr Movie::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
149 150
{
    static const std::string req = "SELECT * FROM " + policy::MovieTable::Name + " WHERE media_id = ?";
151
    return fetch( ml, req, mediaId );
152
}
153 154

}