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

Add album creation & fetching

parent 3c019a2b
......@@ -5,12 +5,14 @@
#include <string>
#include <memory>
class IAlbum;
class IFile;
class ILabel;
class IMetadataService;
typedef std::shared_ptr<IFile> FilePtr;
typedef std::shared_ptr<ILabel> LabelPtr;
typedef std::shared_ptr<IAlbum> AlbumPtr;
class IMediaLibrary
{
......@@ -25,6 +27,9 @@ class IMediaLibrary
virtual bool deleteLabel( const std::string& label ) = 0;
virtual bool deleteLabel( LabelPtr label ) = 0;
virtual bool files( std::vector<FilePtr>& res ) = 0;
virtual AlbumPtr album( const std::string& id3Tag ) = 0;
virtual AlbumPtr createAlbum( const std::string& id3Tag ) = 0;
virtual void addMetadataService( IMetadataService* service ) = 0;
};
......
......@@ -3,6 +3,12 @@
#include "IMediaLibrary.h"
class IMetadataServiceCb
{
public:
virtual void updated( FilePtr file ) = 0;
};
class IMetadataService
{
public:
......
......@@ -10,11 +10,26 @@ Album::Album(sqlite3* dbConnection, sqlite3_stmt* stmt)
: m_dbConnection( dbConnection )
{
m_id = sqlite3_column_int( stmt, 0 );
m_name = (const char*)sqlite3_column_text( stmt, 1 );
m_name = Traits<std::string>::Load( stmt, 1 );
m_releaseYear = sqlite3_column_int( stmt, 2 );
m_shortSummary = (const char*)sqlite3_column_text( stmt, 3 );
m_artworkUrl = (const char*)sqlite3_column_text( stmt, 4 );
m_shortSummary = Traits<std::string>::Load( stmt, 3 );
m_artworkUrl = Traits<std::string>::Load( stmt, 4 );
m_lastSyncDate = sqlite3_column_int( stmt, 5 );
m_id3tag = Traits<std::string>::Load( stmt, 6 );
}
Album::Album( const std::string& id3tag )
: m_dbConnection( nullptr )
, m_id( 0 )
, m_releaseYear( 0 )
, m_lastSyncDate( 0 )
, m_id3tag( id3tag )
{
}
unsigned int Album::id() const
{
return m_id;
}
const std::string& Album::name()
......@@ -42,7 +57,7 @@ time_t Album::lastSyncDate()
return m_lastSyncDate;
}
const std::vector<std::shared_ptr<IAlbumTrack>>& Album::tracks()
const std::vector<std::shared_ptr<IAlbumTrack> >& Album::tracks()
{
if ( m_tracks == NULL )
{
......@@ -61,7 +76,21 @@ bool Album::createTable( sqlite3* dbConnection )
"release_year UNSIGNED INTEGER,"
"short_summary TEXT,"
"artwork_url TEXT,"
"UNSIGNED INTEGER last_sync_date"
"UNSIGNED INTEGER last_sync_date,"
"id3tag TEXT UNIQUE ON CONFLICT FAIL"
")";
return SqliteTools::executeRequest( dbConnection, req );
return SqliteTools::executeRequest( dbConnection, req );
}
AlbumPtr Album::create( sqlite3* dbConnection, const std::string& id3Tag )
{
auto album = std::make_shared<Album>( id3Tag );
static const std::string& req = "INSERT INTO " + policy::AlbumTable::Name +
"(id_album, id3tag) VALUES(NULL, ?)";
auto pKey = _Cache::insert( dbConnection, album, req.c_str(), id3Tag );
if ( pKey == 0 )
return nullptr;
album->m_id = pKey;
album->m_dbConnection = dbConnection;
return album;
}
......@@ -4,6 +4,8 @@
#include <memory>
#include <sqlite3.h>
#include "IMediaLibrary.h"
#include "Cache.h"
#include "IAlbum.h"
......@@ -18,9 +20,13 @@ struct AlbumTable
class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
{
private:
typedef Cache<Album, IAlbum, policy::AlbumTable> _Cache;
public:
Album( sqlite3* dbConnection, sqlite3_stmt* stmt );
Album( const std::string& id3tag );
unsigned int id() const;
virtual const std::string& name();
virtual unsigned int releaseYear();
virtual const std::string& shortSummary();
......@@ -29,6 +35,7 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
virtual const std::vector<std::shared_ptr<IAlbumTrack>>& tracks();
static bool createTable( sqlite3* dbConnection );
static AlbumPtr create( sqlite3* dbConnection, const std::string& id3Tag );
protected:
sqlite3* m_dbConnection;
......@@ -38,6 +45,7 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
std::string m_shortSummary;
std::string m_artworkUrl;
time_t m_lastSyncDate;
std::string m_id3tag;
std::vector<std::shared_ptr<IAlbumTrack>>* m_tracks;
......
......@@ -82,6 +82,19 @@ bool MediaLibrary::deleteLabel( LabelPtr label )
return Label::destroy( m_dbConnection, std::static_pointer_cast<Label>( label ) );
}
AlbumPtr MediaLibrary::album( const std::string& id3Tag )
{
// We can't use Cache helper, since albums are cached by primary keys
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" WHERE id3tag = ?";
return SqliteTools::fetchOne<Album>( m_dbConnection, req.c_str(), id3Tag );
}
AlbumPtr MediaLibrary::createAlbum( const std::string& id3Tag )
{
return Album::create( m_dbConnection, id3Tag );
}
void MediaLibrary::addMetadataService(IMetadataService* service)
{
typedef std::unique_ptr<IMetadataService> MdsPtr;
......
......@@ -11,14 +11,20 @@ class MediaLibrary : public IMediaLibrary
MediaLibrary();
~MediaLibrary();
virtual bool initialize( const std::string& dbPath );
virtual bool files( std::vector<FilePtr>& res );
virtual FilePtr file( const std::string& path );
virtual FilePtr addFile( const std::string& path );
virtual bool deleteFile( const std::string& mrl );
virtual bool deleteFile( FilePtr file );
virtual LabelPtr createLabel( const std::string& label );
virtual bool deleteLabel(const std::string& text );
virtual bool deleteLabel( LabelPtr label );
virtual AlbumPtr album( const std::string& id3Tag );
virtual AlbumPtr createAlbum( const std::string& id3Tag );
virtual void addMetadataService( IMetadataService* service );
private:
sqlite3* m_dbConnection;
......
#include "gtest/gtest.h"
#include "IMediaLibrary.h"
#include "IFile.h"
class Albums : public testing::Test
{
public:
static IMediaLibrary* ml;
protected:
virtual void SetUp()
{
ml = MediaLibraryFactory::create();
bool res = ml->initialize( "test.db" );
ASSERT_TRUE( res );
}
virtual void TearDown()
{
delete ml;
ml = nullptr;
unlink("test.db");
}
};
IMediaLibrary* Albums::ml;
TEST_F( Albums, Create )
{
auto a = ml->createAlbum( "mytag" );
ASSERT_NE( a, nullptr );
auto a2 = ml->album( "mytag" );
ASSERT_EQ( a, a2 );
}
......@@ -33,6 +33,7 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
list(APPEND TEST_SRCS
Files.cpp
Labels.cpp
Albums.cpp
)
add_executable(unittest ${TEST_SRCS})
......
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