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

Handle AlbumTrack creation

parent 082592f2
#ifndef IALBUM_H
#define IALBUM_H
#include <memory>
#include <string>
#include <vector>
#include "IMediaLibrary.h"
class IAlbumTrack;
......@@ -16,7 +14,8 @@ class IAlbum
virtual unsigned int releaseYear() = 0;
virtual const std::string& shortSummary() = 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
......@@ -11,6 +11,7 @@ class IAlbumTrack
public:
virtual ~IAlbumTrack() {}
virtual unsigned int id() const = 0;
virtual const std::string& genre() = 0;
virtual const std::string& title() = 0;
virtual unsigned int trackNumber() = 0;
......
......@@ -6,6 +6,7 @@
#include <memory>
class IAlbum;
class IAlbumTrack;
class IFile;
class ILabel;
class IMetadataService;
......@@ -13,6 +14,7 @@ class IMetadataService;
typedef std::shared_ptr<IFile> FilePtr;
typedef std::shared_ptr<ILabel> LabelPtr;
typedef std::shared_ptr<IAlbum> AlbumPtr;
typedef std::shared_ptr<IAlbumTrack> AlbumTrackPtr;
class IMediaLibrary
{
......
......@@ -58,15 +58,15 @@ time_t Album::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 )
{
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 );
}
return *m_tracks;
const char* req = "SELECT * FROM AlbumTrack WHERE album_id = ?";
return SqliteTools::fetchAll<AlbumTrack>( m_dbConnection, req, tracks, m_id );
}
AlbumTrackPtr Album::addTrack( const std::string& name, unsigned int trackNb )
{
return AlbumTrack::create( m_dbConnection, m_id, name, trackNb );
}
bool Album::createTable( sqlite3* dbConnection )
......
......@@ -35,7 +35,9 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
virtual const std::string& shortSummary();
virtual const std::string& artworkUrl();
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 AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag );
......@@ -50,8 +52,6 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
time_t m_lastSyncDate;
std::string m_id3tag;
std::vector<std::shared_ptr<IAlbumTrack>>* m_tracks;
friend class Cache<Album, IAlbum, policy::AlbumTable>;
friend class policy::AlbumTable;
};
......
......@@ -8,13 +8,28 @@ unsigned int AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTra
AlbumTrack::AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection )
, m_album( NULL )
, m_album( nullptr )
{
m_id = sqlite3_column_int( stmt, 0 );
m_title = (const char*)sqlite3_column_text( stmt, 1 );
m_genre = (const char*)sqlite3_column_text( stmt, 2 );
m_trackNumber = sqlite3_column_int( stmt, 3 );
m_albumId = sqlite3_column_int( stmt, 4 );
m_id = Traits<unsigned int>::Load( stmt, 0 );
m_title = Traits<std::string>::Load( stmt, 1 );
m_genre = Traits<std::string>::Load( stmt, 2 );
m_trackNumber = Traits<unsigned int>::Load( stmt, 3 );
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)
......@@ -30,6 +45,17 @@ bool AlbumTrack::createTable(sqlite3* dbConnection)
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()
{
return m_genre;
......
......@@ -5,6 +5,7 @@
#include <string>
#include "IAlbumTrack.h"
#include "IMediaLibrary.h"
#include "Cache.h"
class Album;
......@@ -22,15 +23,21 @@ struct AlbumTrackTable
class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable>
{
private:
typedef Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable> _Cache;
public:
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& title();
virtual unsigned int trackNumber();
virtual std::shared_ptr<IAlbum> album();
static bool createTable( sqlite3* dbConnection );
static AlbumTrackPtr create(sqlite3* dbConnection, unsigned int albumId,
const std::string& name, unsigned int trackNb );
private:
sqlite3* m_dbConnection;
......
......@@ -105,7 +105,13 @@ class SqliteTools
{
res = sqlite3_step( stmt.get() );
} 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 "IMediaLibrary.h"
#include "IAlbum.h"
#include "IAlbumTrack.h"
#include "IMediaLibrary.h"
class Albums : public testing::Test
{
......@@ -49,3 +50,23 @@ TEST_F( Albums, Fetch )
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