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

Artist: Replace "isAlbumArtist" by "nbAlbums"

This feels less like a hack, and provide valuable informations
parent 0db0e950
......@@ -185,8 +185,13 @@ bool Album::setAlbumArtist(Artist* artist)
"artist_id = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, artist->id(), m_id ) == false )
return false;
if ( m_artistId != 0 )
{
auto previousArtist = Artist::fetch( m_dbConnection, m_artistId );
previousArtist->updateNbAlbum( -1 );
}
m_artistId = artist->id();
artist->markAsAlbumArtist();
artist->updateNbAlbum( 1 );
return true;
}
......
......@@ -39,20 +39,13 @@ Artist::Artist( DBConnection dbConnection, sqlite::Row& row )
>> m_name
>> m_shortBio
>> m_artworkUrl
>> m_isAlbumArtist;
>> m_nbAlbums;
}
Artist::Artist( const std::string& name )
: m_id( 0 )
, m_name( name )
, m_isAlbumArtist( false )
{
}
Artist::Artist(DBConnection dbConnection)
: m_dbConnection( dbConnection )
, m_id( 0 )
, m_isAlbumArtist( false )
, m_nbAlbums( 0 )
{
}
......@@ -135,18 +128,16 @@ bool Artist::setArtworkUrl( const std::string& artworkUrl )
return true;
}
bool Artist::markAsAlbumArtist()
bool Artist::updateNbAlbum( int increment )
{
if ( m_isAlbumArtist == true )
return true;
// UnknownArtist can't be an album artist.
if ( m_id == 0 )
return false;
assert( increment != 0 );
assert( increment > 0 || ( increment < 0 && m_nbAlbums >= 1 ) );
static const std::string req = "UPDATE " + policy::ArtistTable::Name +
" SET is_album_artist=1 WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_id ) == false )
" SET nb_albums = nb_albums + ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, increment, m_id ) == false )
return false;
m_isAlbumArtist = true;
m_nbAlbums += increment;
return true;
}
......@@ -159,7 +150,7 @@ bool Artist::createTable( DBConnection dbConnection )
"name TEXT UNIQUE ON CONFLICT FAIL,"
"shortbio TEXT,"
"artwork_url TEXT,"
"is_album_artist BOOLEAN DEFAULT 0"
"nb_albums UNSIGNED INT DEFAULT 0"
")";
static const std::string reqRel = "CREATE TABLE IF NOT EXISTS MediaArtistRelation("
"id_media INTEGER NOT NULL,"
......
......@@ -47,11 +47,6 @@ private:
public:
Artist( DBConnection dbConnection, sqlite::Row& row );
Artist( const std::string& name );
/**
* @brief Artist Construct an empty artist, with a DB connection.
* This is only meant to construct the Unknown Artist virtual representation
*/
Artist( DBConnection dbConnection );
virtual unsigned int id() const override;
virtual const std::string &name() const override;
......@@ -62,7 +57,7 @@ public:
bool addMedia( Media* media );
virtual const std::string& artworkUrl() const override;
bool setArtworkUrl( const std::string& artworkUrl );
bool markAsAlbumArtist();
bool updateNbAlbum( int increment );
static bool createTable( DBConnection dbConnection );
static bool createDefaultArtists( DBConnection dbConnection );
......@@ -74,7 +69,7 @@ private:
std::string m_name;
std::string m_shortBio;
std::string m_artworkUrl;
bool m_isAlbumArtist;
unsigned int m_nbAlbums;
friend _Cache;
friend struct policy::ArtistTable;
......
......@@ -331,7 +331,7 @@ std::shared_ptr<Artist> MediaLibrary::createArtist( const std::string& name )
std::vector<ArtistPtr> MediaLibrary::artists() const
{
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name +
" WHERE is_album_artist = 1";
" WHERE nb_albums > 0";
return Artist::fetchAll( m_dbConnection.get(), req );
}
......
......@@ -142,7 +142,9 @@ TEST_F( Artists, GetAll )
for ( int i = 0; i < 5; i++ )
{
auto a = ml->createArtist( std::to_string( i ) );
a->markAsAlbumArtist();
auto alb = ml->createAlbum( std::to_string( i ) );
ASSERT_NE( nullptr, alb );
alb->setAlbumArtist( a.get() );
ASSERT_NE( a, nullptr );
}
artists = ml->artists();
......@@ -153,23 +155,3 @@ TEST_F( Artists, GetAll )
auto artists2 = ml->artists();
ASSERT_EQ( artists2.size(), 5u );
}
TEST_F( Artists, MarkAlbumArtist )
{
auto artist = ml->createArtist( "Explotters In The Sky" );
ASSERT_NE( artist, nullptr );
// Since it's not an album artist, we shouldn't have it in the artist listing
auto artists = ml->artists();
ASSERT_EQ( artists.size(), 0u );
artist->markAsAlbumArtist();
artists = ml->artists();
ASSERT_EQ( artists.size(), 1u );
Reload();
artists = ml->artists();
ASSERT_EQ( artists.size(), 1u );
}
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