Commit d49b4401 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Handle AlbumTrack creation

parent 082592f2
#ifndef IALBUM_H #ifndef IALBUM_H
#define IALBUM_H #define IALBUM_H
#include <memory> #include "IMediaLibrary.h"
#include <string>
#include <vector>
class IAlbumTrack; class IAlbumTrack;
...@@ -16,7 +14,8 @@ class IAlbum ...@@ -16,7 +14,8 @@ class IAlbum
virtual unsigned int releaseYear() = 0; virtual unsigned int releaseYear() = 0;
virtual const std::string& shortSummary() = 0; virtual const std::string& shortSummary() = 0;
virtual const std::string& artworkUrl() = 0; virtual const std::string& artworkUrl() = 0;
virtual const std::vector<std::shared_ptr<IAlbumTrack>>& tracks() = 0; virtual bool tracks( std::vector<std::shared_ptr<IAlbumTrack>>& tracks ) = 0;
virtual AlbumTrackPtr addTrack( const std::string& name, unsigned int trackId ) = 0;
}; };
#endif // IALBUM_H #endif // IALBUM_H
...@@ -11,6 +11,7 @@ class IAlbumTrack ...@@ -11,6 +11,7 @@ class IAlbumTrack
public: public:
virtual ~IAlbumTrack() {} virtual ~IAlbumTrack() {}
virtual unsigned int id() const = 0;
virtual const std::string& genre() = 0; virtual const std::string& genre() = 0;
virtual const std::string& title() = 0; virtual const std::string& title() = 0;
virtual unsigned int trackNumber() = 0; virtual unsigned int trackNumber() = 0;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <memory> #include <memory>
class IAlbum; class IAlbum;
class IAlbumTrack;
class IFile; class IFile;
class ILabel; class ILabel;
class IMetadataService; class IMetadataService;
...@@ -13,6 +14,7 @@ class IMetadataService; ...@@ -13,6 +14,7 @@ class IMetadataService;
typedef std::shared_ptr<IFile> FilePtr; typedef std::shared_ptr<IFile> FilePtr;
typedef std::shared_ptr<ILabel> LabelPtr; typedef std::shared_ptr<ILabel> LabelPtr;
typedef std::shared_ptr<IAlbum> AlbumPtr; typedef std::shared_ptr<IAlbum> AlbumPtr;
typedef std::shared_ptr<IAlbumTrack> AlbumTrackPtr;
class IMediaLibrary class IMediaLibrary
{ {
......
...@@ -58,15 +58,15 @@ time_t Album::lastSyncDate() ...@@ -58,15 +58,15 @@ time_t Album::lastSyncDate()
return m_lastSyncDate; return m_lastSyncDate;
} }
const std::vector<std::shared_ptr<IAlbumTrack> >& Album::tracks() bool Album::tracks( std::vector<std::shared_ptr<IAlbumTrack> >& tracks )
{ {
if ( m_tracks == NULL ) const char* req = "SELECT * FROM AlbumTrack WHERE album_id = ?";
{ return SqliteTools::fetchAll<AlbumTrack>( m_dbConnection, req, tracks, m_id );
m_tracks = new std::vector<std::shared_ptr<IAlbumTrack>>; }
const char* req = "SELECT * FROM AlbumTrack WHERE id_album = ?";
SqliteTools::fetchAll<AlbumTrack>( m_dbConnection, req, *m_tracks, m_id ); AlbumTrackPtr Album::addTrack( const std::string& name, unsigned int trackNb )
} {
return *m_tracks; return AlbumTrack::create( m_dbConnection, m_id, name, trackNb );
} }
bool Album::createTable( sqlite3* dbConnection ) bool Album::createTable( sqlite3* dbConnection )
......
...@@ -35,7 +35,9 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable> ...@@ -35,7 +35,9 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
virtual const std::string& shortSummary(); virtual const std::string& shortSummary();
virtual const std::string& artworkUrl(); virtual const std::string& artworkUrl();
virtual time_t lastSyncDate(); virtual time_t lastSyncDate();
virtual const std::vector<std::shared_ptr<IAlbumTrack>>& tracks(); virtual bool tracks( std::vector<std::shared_ptr<IAlbumTrack>>& tracks );
virtual AlbumTrackPtr addTrack( const std::string& name, unsigned int trackNb );
static bool createTable( sqlite3* dbConnection ); static bool createTable( sqlite3* dbConnection );
static AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag ); static AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag );
...@@ -50,8 +52,6 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable> ...@@ -50,8 +52,6 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
time_t m_lastSyncDate; time_t m_lastSyncDate;
std::string m_id3tag; std::string m_id3tag;
std::vector<std::shared_ptr<IAlbumTrack>>* m_tracks;
friend class Cache<Album, IAlbum, policy::AlbumTable>; friend class Cache<Album, IAlbum, policy::AlbumTable>;
friend class policy::AlbumTable; friend class policy::AlbumTable;
}; };
......
...@@ -8,13 +8,28 @@ unsigned int AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTra ...@@ -8,13 +8,28 @@ unsigned int AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTra
AlbumTrack::AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt ) AlbumTrack::AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection ) : m_dbConnection( dbConnection )
, m_album( NULL ) , m_album( nullptr )
{ {
m_id = sqlite3_column_int( stmt, 0 ); m_id = Traits<unsigned int>::Load( stmt, 0 );
m_title = (const char*)sqlite3_column_text( stmt, 1 ); m_title = Traits<std::string>::Load( stmt, 1 );
m_genre = (const char*)sqlite3_column_text( stmt, 2 ); m_genre = Traits<std::string>::Load( stmt, 2 );
m_trackNumber = sqlite3_column_int( stmt, 3 ); m_trackNumber = Traits<unsigned int>::Load( stmt, 3 );
m_albumId = sqlite3_column_int( stmt, 4 ); m_albumId = Traits<unsigned int>::Load( stmt, 4 );
}
AlbumTrack::AlbumTrack( const std::string& title, unsigned int trackNumber, unsigned int albumId )
: m_dbConnection( nullptr )
, m_id( 0 )
, m_title( title )
, m_trackNumber( trackNumber )
, m_albumId( albumId )
, m_album( nullptr )
{
}
unsigned int AlbumTrack::id() const
{
return m_id;
} }
bool AlbumTrack::createTable(sqlite3* dbConnection) bool AlbumTrack::createTable(sqlite3* dbConnection)
...@@ -30,6 +45,17 @@ bool AlbumTrack::createTable(sqlite3* dbConnection) ...@@ -30,6 +45,17 @@ bool AlbumTrack::createTable(sqlite3* dbConnection)
return SqliteTools::executeRequest( dbConnection, req ); return SqliteTools::executeRequest( dbConnection, req );
} }
AlbumTrackPtr AlbumTrack::create(sqlite3* dbConnection, unsigned int albumId, const std::string& name, unsigned int trackNb)
{
auto self = std::make_shared<AlbumTrack>( name, trackNb, albumId );
static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
+ "(title, track_number, album_id) VALUES(?, ?, ?)";
if ( _Cache::insert( dbConnection, self, req.c_str(), name, trackNb, albumId ) == false )
return false;
self->m_dbConnection = dbConnection;
return self;
}
const std::string& AlbumTrack::genre() const std::string& AlbumTrack::genre()
{ {
return m_genre; return m_genre;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <string> #include <string>
#include "IAlbumTrack.h" #include "IAlbumTrack.h"
#include "IMediaLibrary.h"
#include "Cache.h" #include "Cache.h"
class Album; class Album;
...@@ -22,15 +23,21 @@ struct AlbumTrackTable ...@@ -22,15 +23,21 @@ struct AlbumTrackTable
class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable> class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable>
{ {
private:
typedef Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable> _Cache;
public: public:
AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt ); AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt );
AlbumTrack( const std::string& title, unsigned int trackNumber, unsigned int albumId );
virtual unsigned int id() const;
virtual const std::string& genre(); virtual const std::string& genre();
virtual const std::string& title(); virtual const std::string& title();
virtual unsigned int trackNumber(); virtual unsigned int trackNumber();
virtual std::shared_ptr<IAlbum> album(); virtual std::shared_ptr<IAlbum> album();
static bool createTable( sqlite3* dbConnection ); static bool createTable( sqlite3* dbConnection );
static AlbumTrackPtr create(sqlite3* dbConnection, unsigned int albumId,
const std::string& name, unsigned int trackNb );
private: private:
sqlite3* m_dbConnection; sqlite3* m_dbConnection;
......
...@@ -105,7 +105,13 @@ class SqliteTools ...@@ -105,7 +105,13 @@ class SqliteTools
{ {
res = sqlite3_step( stmt.get() ); res = sqlite3_step( stmt.get() );
} while ( res == SQLITE_ROW ); } while ( res == SQLITE_ROW );
return res == SQLITE_DONE; if ( res != SQLITE_DONE )
{
std::cerr << "Invalid result: " << res << ": " <<
sqlite3_errmsg( dbConnection ) << std::endl;
return false;
}
return true;
} }
/** /**
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "IMediaLibrary.h"
#include "IAlbum.h" #include "IAlbum.h"
#include "IAlbumTrack.h"
#include "IMediaLibrary.h"
class Albums : public testing::Test class Albums : public testing::Test
{ {
...@@ -49,3 +50,23 @@ TEST_F( Albums, Fetch ) ...@@ -49,3 +50,23 @@ TEST_F( Albums, Fetch )
ASSERT_EQ( a->id(), a2->id() ); ASSERT_EQ( a->id(), a2->id() );
} }
TEST_F( Albums, AddTrack )
{
auto a = ml->createAlbum( "albumtag" );
auto track = a->addTrack( "track", 10 );
ASSERT_NE( track, nullptr );
std::vector<AlbumTrackPtr> tracks;
bool res = a->tracks( tracks );
ASSERT_TRUE( res );
ASSERT_EQ( tracks.size(), 1u );
ASSERT_EQ( tracks[0], track );
delete ml;
SetUp();
a->tracks( tracks );
ASSERT_EQ( tracks.size(), 1u );
ASSERT_EQ( tracks[0]->title(), track->title() );
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment