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

Album: Expose an albumArtist method

parent df6dff16
......@@ -39,6 +39,13 @@ class IAlbum
* @brief tracks fetches album tracks from the database
*/
virtual std::vector<std::shared_ptr<IMedia>> tracks() const = 0;
/**
* @brief albumArtist Returns the album main artist (generally tagged as album-artist)
*/
virtual ArtistPtr albumArtist() const = 0;
/**
* @brief artists Returns a vector of all additional artists appearing on the album.
*/
virtual std::vector<ArtistPtr> artists() const = 0;
/**
* @brief nbTracks Returns the amount of track in this album.
......
......@@ -36,6 +36,7 @@ Album::Album(DBConnection dbConnection, sqlite::Row& row)
{
row >> m_id
>> m_title
>> m_artistId
>> m_releaseYear
>> m_shortSummary
>> m_artworkUrl
......@@ -46,6 +47,7 @@ Album::Album(DBConnection dbConnection, sqlite::Row& row)
Album::Album(const std::string& title )
: m_id( 0 )
, m_title( title )
, m_artistId( 0 )
, m_releaseYear( 0 )
, m_lastSyncDate( 0 )
, m_nbTracks( 0 )
......@@ -142,6 +144,27 @@ unsigned int Album::nbTracks() const
return m_nbTracks;
}
ArtistPtr Album::albumArtist() const
{
if ( m_artistId == 0 )
return nullptr;
return Artist::fetch( m_dbConnection, m_artistId );
}
bool Album::setAlbumArtist(Artist* artist)
{
if ( m_artistId == artist->id() )
return true;
if ( artist->id() == 0 )
return false;
static const std::string req = "UPDATE " + policy::AlbumTable::Name + " SET "
"artist_id = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artist->id(), m_id ) == false )
return false;
m_artistId = artist->id();
return true;
}
std::vector<ArtistPtr> Album::artists() const
{
static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
......@@ -168,6 +191,7 @@ bool Album::createTable(DBConnection dbConnection )
"("
"id_album INTEGER PRIMARY KEY AUTOINCREMENT,"
"title TEXT,"
"artist_id UNSIGNED INTEGER,"
"release_year UNSIGNED INTEGER,"
"short_summary TEXT,"
"artwork_url TEXT,"
......
......@@ -67,6 +67,8 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
std::shared_ptr<AlbumTrack> addTrack( std::shared_ptr<Media> media, unsigned int trackNb );
unsigned int nbTracks() const override;
virtual ArtistPtr albumArtist() const override;
bool setAlbumArtist( Artist* artist );
virtual std::vector<ArtistPtr> artists() const override;
bool addArtist( std::shared_ptr<Artist> artist );
......@@ -77,6 +79,7 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
DBConnection m_dbConnection;
unsigned int m_id;
std::string m_title;
unsigned int m_artistId;
unsigned int m_releaseYear;
std::string m_shortSummary;
std::string m_artworkUrl;
......
......@@ -24,6 +24,7 @@
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "Media.h"
#include "IMediaLibrary.h"
......@@ -189,5 +190,22 @@ TEST_F( Albums, Artists )
album = std::static_pointer_cast<Album>( ml->album( album->id() ) );
artists = album->artists();
ASSERT_EQ( album->albumArtist(), nullptr );
ASSERT_EQ( artists.size(), 2u );
}
TEST_F( Albums, AlbumArtist )
{
auto album = ml->createAlbum( "test" );
ASSERT_EQ( album->albumArtist(), nullptr );
auto artist = ml->createArtist( "artist" );
album->setAlbumArtist( artist.get() );
ASSERT_NE( album->albumArtist(), nullptr );
Reload();
album = std::static_pointer_cast<Album>( ml->album( album->id() ) );
auto albumArtist = album->albumArtist();
ASSERT_NE( albumArtist, nullptr );
ASSERT_EQ( albumArtist->name(), artist->name() );
}
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