Movie.cpp 4.49 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
Movie::Movie(DBConnection dbConnection, sqlite::Row& row )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
32 33
    : m_dbConnection( dbConnection )
{
34 35 36 37
    row >> m_id
        >> m_title
        >> m_releaseDate
        >> m_summary
38
        >> m_artworkMrl
39
        >> m_imdbId;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
40 41 42
}

Movie::Movie( const std::string& title )
43
    : m_id( 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    , 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 = ?";
69
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
        return false;
    m_releaseDate = date;
    return true;
}

const std::string&Movie::shortSummary() const
{
    return m_summary;
}

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

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

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

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

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

141
std::shared_ptr<Movie> Movie::create(DBConnection dbConnection, const std::string& title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
142 143 144 145
{
    auto movie = std::make_shared<Movie>( title );
    static const std::string req = "INSERT INTO " + policy::MovieTable::Name
            + "(title) VALUES(?)";
146
    if ( insert( dbConnection, movie, req, title ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
147 148 149 150
        return nullptr;
    movie->m_dbConnection = dbConnection;
    return movie;
}