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

Add an Artist type

parent d7787aaf
#pragma once
#include <string>
#include <vector>
#include "Types.h"
class IArtist
{
public:
virtual ~IArtist() = default;
virtual unsigned int id() const = 0;
virtual const std::string& name() const = 0;
virtual const std::string& shortBio() const = 0;
virtual bool setShortBio( const std::string& shortBio ) = 0;
virtual std::vector<AlbumPtr> albums() const = 0;
};
......@@ -72,6 +72,8 @@ class IMediaLibrary
virtual ShowPtr createShow( const std::string& name ) = 0;
virtual MoviePtr movie( const std::string& title ) = 0;
virtual MoviePtr createMovie( const std::string& title ) = 0;
virtual ArtistPtr artist( const std::string& name ) = 0;
virtual ArtistPtr createArtist( const std::string& name ) = 0;
/**
* @brief discover Launch a discovery on the provided entry point.
......
......@@ -16,6 +16,7 @@ class IShow;
class IShowEpisode;
class IVideoTrack;
class ILogger;
class IArtist;
struct sqlite3;
......@@ -29,6 +30,7 @@ typedef std::shared_ptr<IShowEpisode> ShowEpisodePtr;
typedef std::shared_ptr<IMovie> MoviePtr;
typedef std::shared_ptr<IAudioTrack> AudioTrackPtr;
typedef std::shared_ptr<IVideoTrack> VideoTrackPtr;
typedef std::shared_ptr<IArtist> ArtistPtr;
typedef std::weak_ptr<sqlite3> DBConnection;
......
#include "Artist.h"
#include "database/SqliteTools.h"
const std::string policy::ArtistTable::Name = "artist";
const std::string policy::ArtistTable::CacheColumn = "id_artist";
unsigned int Artist::*const policy::ArtistTable::PrimaryKey = &Artist::m_id;
Artist::Artist( DBConnection dbConnection, sqlite3_stmt *stmt )
: m_dbConnection( dbConnection )
{
m_id = sqlite::Traits<unsigned int>::Load( stmt, 0 );
m_name = sqlite::Traits<std::string>::Load( stmt, 1 );
m_shortBio = sqlite::Traits<std::string>::Load( stmt, 2 );
}
Artist::Artist( const std::string& name )
: m_id( 0 )
, m_name( name )
{
}
unsigned int Artist::id() const
{
return m_id;
}
const std::string &Artist::name() const
{
return m_name;
}
const std::string &Artist::shortBio() const
{
return m_shortBio;
}
bool Artist::setShortBio(const std::string &shortBio)
{
static const std::string req = "UPDATE " + policy::ArtistTable::Name
+ " SET shortbio = ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, shortBio, m_id ) == false )
return false;
m_shortBio = shortBio;
return true;
}
std::vector<AlbumPtr> Artist::albums() const
{
//FIXME
}
bool Artist::createTable( DBConnection dbConnection )
{
static const std::string req = "CREATE TABLE IF NOT EXISTS " +
policy::ArtistTable::Name +
"("
"id_artist INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT UNIQUE ON CONFLICT FAIL,"
"shortbio TEXT"
")";
return sqlite::Tools::executeRequest( dbConnection, req );
}
ArtistPtr Artist::create( DBConnection dbConnection, const std::string &name )
{
auto artist = std::make_shared<Artist>( name );
static const std::string req = "INSERT INTO " + policy::ArtistTable::Name +
"(id_artist, name) VALUES(NULL, ?)";
if ( _Cache::insert( dbConnection, artist, req, name ) == false )
return nullptr;
artist->m_dbConnection = dbConnection;
return artist;
}
#pragma once
#include "database/Cache.h"
#include "IArtist.h"
#include "IMediaLibrary.h"
class Artist;
namespace policy
{
struct ArtistTable
{
static const std::string Name;
static const std::string CacheColumn;
static unsigned int Artist::*const PrimaryKey;
};
}
class Artist : public IArtist, public Cache<Artist, IArtist, policy::ArtistTable>
{
private:
using _Cache = Cache<Artist, IArtist, policy::ArtistTable>;
public:
Artist( DBConnection dbConnection, sqlite3_stmt* stmt );
Artist( const std::string& name );
virtual unsigned int id() const override;
virtual const std::string &name() const override;
virtual const std::string& shortBio() const override;
virtual bool setShortBio( const std::string& shortBio ) override;
virtual std::vector<AlbumPtr> albums() const override;
static bool createTable( DBConnection dbConnection );
static ArtistPtr create( DBConnection dbConnection, const std::string& name );
private:
DBConnection m_dbConnection;
unsigned int m_id;
std::string m_name;
std::string m_shortBio;
friend _Cache;
friend struct policy::ArtistTable;
};
......@@ -34,6 +34,7 @@ list(APPEND HEADERS_LIST
${CMAKE_SOURCE_DIR}/include/filesystem/IFile.h
${CMAKE_SOURCE_DIR}/include/factory/IFileSystem.h
${CMAKE_SOURCE_DIR}/include/ILogger.h
${CMAKE_SOURCE_DIR}/include/IArtist.h
database/Cache.h
database/SqliteTools.h
......@@ -64,6 +65,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
VideoTrack.cpp
AudioTrack.cpp
Parser.cpp
Artist.cpp
factory/FileSystem.cpp
filesystem/${ARCH_FOLDER}/Directory.cpp
......
......@@ -2,6 +2,7 @@
#include <functional>
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "AudioTrack.h"
#include "File.h"
#include "Folder.h"
......@@ -69,6 +70,7 @@ MediaLibrary::~MediaLibrary()
Movie::clear();
VideoTrack::clear();
AudioTrack::clear();
Artist::clear();
}
void MediaLibrary::setFsFactory(std::shared_ptr<factory::IFileSystem> fsFactory)
......@@ -125,7 +127,8 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
ShowEpisode::createTable( m_dbConnection ) &&
Movie::createTable( m_dbConnection ) &&
VideoTrack::createTable( m_dbConnection ) &&
AudioTrack::createTable( m_dbConnection ) ) )
AudioTrack::createTable( m_dbConnection ) &&
Artist::createTable( m_dbConnection ) ) )
{
LOG_ERROR( "Failed to create database structure" );
return false;
......@@ -244,6 +247,18 @@ MoviePtr MediaLibrary::createMovie( const std::string& title )
return Movie::create( m_dbConnection, title );
}
ArtistPtr MediaLibrary::artist(const std::string &name)
{
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name
+ " WHERE name = ?";
return sqlite::Tools::fetchOne<Artist>( m_dbConnection, req, name );
}
ArtistPtr MediaLibrary::createArtist( const std::string& name )
{
return Artist::create( m_dbConnection, name );
}
void MediaLibrary::addMetadataService(std::unique_ptr<IMetadataService> service)
{
if ( service->initialize( m_parser.get(), this ) == false )
......
......@@ -43,6 +43,9 @@ class MediaLibrary : public IMediaLibrary, public IDiscovererCb
virtual MoviePtr movie( const std::string& title ) override;
virtual MoviePtr createMovie( const std::string& title ) override;
virtual ArtistPtr artist( const std::string& name ) override;
virtual ArtistPtr createArtist( const std::string& name ) override;
virtual void discover( const std::string& entryPoint ) override;
// IDiscovererCb implementation
virtual FolderPtr onNewFolder( const fs::IDirectory* directory, FolderPtr parent ) override;
......
#include "Tests.h"
#include "IArtist.h"
class Artists : public Tests
{
};
TEST_F( Artists, Create )
{
auto a = ml->createArtist( "Flying Otters" );
ASSERT_NE( a, nullptr );
ASSERT_EQ( a->name(), "Flying Otters" );
Reload();
a = ml->artist( "Flying Otters" );
ASSERT_NE( a, nullptr );
ASSERT_EQ( a->name(), "Flying Otters" );
}
TEST_F( Artists, ShortBio )
{
auto a = ml->createArtist( "Raging Otters" );
ASSERT_NE( a, nullptr );
ASSERT_EQ( a->shortBio(), "" );
std::string bio("An otter based post-rock band");
a->setShortBio( bio );
ASSERT_EQ( a->shortBio(), bio );
Reload();
a = ml->artist( "Raging Otters" );
ASSERT_NE( a, nullptr );
ASSERT_EQ( a->shortBio(), bio );
}
......@@ -44,6 +44,7 @@ list(APPEND TEST_SRCS
AudioTracks.cpp
VLCMetadataServices.cpp
FsUtilsTests.cpp
ArtistTests.cpp
mocks/FileSystem.h
)
......
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