Movie.cpp 4.95 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 "Movie.h"
24
#include "Media.h"
25
#include "database/SqliteTools.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
26 27

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

31 32
Movie::Movie(MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
33
{
34
    row >> m_id
35
        >> m_mediaId
36 37 38
        >> m_title
        >> m_releaseDate
        >> m_summary
39
        >> m_artworkMrl
40
        >> m_imdbId;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
41 42
}

43 44 45
Movie::Movie( MediaLibraryPtr ml, unsigned int mediaId, const std::string& title )
    : m_ml( ml )
    , m_id( 0 )
46
    , m_mediaId( mediaId )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    , m_title( title )
    , m_releaseDate( 0 )
{
}

unsigned int Movie::id() const
{
    return m_id;
}

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

time_t Movie::releaseDate() const
{
    return m_releaseDate;
}

bool Movie::setReleaseDate( time_t date )
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
            + " SET release_date = ? WHERE id_movie = ?";
71
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, date, m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
72 73 74 75 76
        return false;
    m_releaseDate = date;
    return true;
}

77
const std::string& Movie::shortSummary() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
78 79 80 81 82 83 84 85
{
    return m_summary;
}

bool Movie::setShortSummary( const std::string& summary )
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
            + " SET summary = ? WHERE id_movie = ?";
86
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, summary, m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
87 88 89 90 91
        return false;
    m_summary = summary;
    return true;
}

92
const std::string&Movie::artworkMrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
93
{
94
    return m_artworkMrl;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
95 96
}

97
bool Movie::setArtworkMrl( const std::string& artworkMrl )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
98 99
{
    static const std::string req = "UPDATE " + policy::MovieTable::Name
100
            + " SET artwork_mrl = ? WHERE id_movie = ?";
101
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, artworkMrl, m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
102
        return false;
103
    m_artworkMrl = artworkMrl;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
104 105 106 107 108 109 110 111 112 113 114 115
    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 = ?";
116
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, imdbId, m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
117 118 119 120 121
        return false;
    m_imdbId = imdbId;
    return true;
}

122
std::vector<MediaPtr> Movie::files()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
123
{
124
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
125
            + " WHERE movie_id = ?";
126
    return Media::fetchAll<IMedia>( m_ml, req, m_id );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
127 128
}

129
bool Movie::createTable( DBConnection dbConnection )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
130 131 132 133
{
    static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MovieTable::Name
            + "("
                "id_movie INTEGER PRIMARY KEY AUTOINCREMENT,"
134
                "media_id UNSIGNED INTEGER NOT NULL,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
135 136 137
                "title TEXT UNIQUE ON CONFLICT FAIL,"
                "release_date UNSIGNED INTEGER,"
                "summary TEXT,"
138
                "artwork_mrl TEXT,"
139 140 141
                "imdb_id TEXT,"
                "FOREIGN KEY(media_id) REFERENCES " + policy::MediaTable::Name
                + "(id_media) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
142
            ")";
143
    return sqlite::Tools::executeRequest( dbConnection, req );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
144 145
}

146
std::shared_ptr<Movie> Movie::create( MediaLibraryPtr ml, unsigned int mediaId, const std::string& title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
147
{
148
    auto movie = std::make_shared<Movie>( ml, mediaId, title );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
149
    static const std::string req = "INSERT INTO " + policy::MovieTable::Name
150
            + "(media_id, title) VALUES(?, ?)";
151
    if ( insert( ml, movie, req, mediaId, title ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
152 153 154
        return nullptr;
    return movie;
}
155

156
MoviePtr Movie::fromMedia( MediaLibraryPtr ml, unsigned int mediaId )
157 158
{
    static const std::string req = "SELECT * FROM " + policy::MovieTable::Name + " WHERE media_id = ?";
159
    return fetch( ml, req, mediaId );
160
}