Movie.cpp 4.48 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 "Movie.h"
24
#include "Media.h"
25
#include "database/SqliteTools.h"
26

27 28 29
namespace medialibrary
{

30
const std::string policy::MovieTable::Name = "Movie";
31
const std::string policy::MovieTable::PrimaryKeyColumn = "id_movie";
32
int64_t Movie::* const policy::MovieTable::PrimaryKey = &Movie::m_id;
33

34 35
Movie::Movie(MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
36
{
37
    row >> m_id
38
        >> m_mediaId
39 40
        >> m_title
        >> m_summary
41
        >> m_artworkMrl
42
        >> m_imdbId;
43 44
}

45
Movie::Movie( MediaLibraryPtr ml, int64_t mediaId, const std::string& title )
46 47
    : m_ml( ml )
    , m_id( 0 )
48
    , m_mediaId( mediaId )
49 50 51 52
    , m_title( title )
{
}

53
int64_t Movie::id() const
54 55 56 57 58 59 60 61 62
{
    return m_id;
}

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

63
const std::string& Movie::shortSummary() const
64 65 66 67 68 69 70 71
{
    return m_summary;
}

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

78
const std::string&Movie::artworkMrl() const
79
{
80
    return m_artworkMrl;
81 82
}

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

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

115
bool Movie::createTable( DBConnection dbConnection )
116 117 118 119
{
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MovieTable::Name
            + "("
                "id_movie INTEGER PRIMARY KEY AUTOINCREMENT,"
120
                "media_id UNSIGNED INTEGER NOT NULL,"
121 122
                "title TEXT UNIQUE ON CONFLICT FAIL,"
                "summary TEXT,"
123
                "artwork_mrl TEXT,"
124 125 126
                "imdb_id TEXT,"
                "FOREIGN KEY(media_id) REFERENCES " + policy::MediaTable::Name
                + "(id_media) ON DELETE CASCADE"
127
            ")";
128
    return sqlite::Tools::executeRequest( dbConnection, req );
129 130
}

131
std::shared_ptr<Movie> Movie::create(MediaLibraryPtr ml, int64_t mediaId, const std::string& title )
132
{
133
    auto movie = std::make_shared<Movie>( ml, mediaId, title );
134
    static const std::string req = "INSERT INTO " + policy::MovieTable::Name
135
            + "(media_id, title) VALUES(?, ?)";
136
    if ( insert( ml, movie, req, mediaId, title ) == false )
137 138 139
        return nullptr;
    return movie;
}
140

141
MoviePtr Movie::fromMedia( MediaLibraryPtr ml, int64_t mediaId )
142 143
{
    static const std::string req = "SELECT * FROM " + policy::MovieTable::Name + " WHERE media_id = ?";
144
    return fetch( ml, req, mediaId );
145
}
146 147

}