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

Move releaseYear from IAlbum to IAlbumTrack

parent 3a60d667
......@@ -31,7 +31,6 @@ class IAlbum
virtual ~IAlbum() {}
virtual unsigned int id() const = 0;
virtual const std::string& title() const = 0;
virtual time_t releaseYear() const = 0;
virtual const std::string& shortSummary() const = 0;
virtual const std::string& artworkUrl() const = 0;
virtual time_t lastSyncDate() const = 0;
......
......@@ -45,6 +45,12 @@ class IAlbumTrack
virtual const std::string& genre() = 0;
virtual unsigned int trackNumber() = 0;
virtual std::shared_ptr<IAlbum> album() = 0;
/**
* @brief releaseYear Represent the track release year. It doesn't
* imply anything regarding the album's release year.
* @return This track release year, or 0 if unknown.
*/
virtual unsigned int releaseYear() const = 0;
};
#endif // IALBUMTRACK_H
......@@ -37,7 +37,6 @@ Album::Album(DBConnection dbConnection, sqlite::Row& row)
row >> m_id
>> m_title
>> m_artistId
>> m_releaseYear
>> m_shortSummary
>> m_artworkUrl
>> m_lastSyncDate
......@@ -48,7 +47,6 @@ Album::Album(const std::string& title )
: m_id( 0 )
, m_title( title )
, m_artistId( 0 )
, m_releaseYear( 0 )
, m_lastSyncDate( 0 )
, m_nbTracks( 0 )
{
......@@ -64,21 +62,6 @@ const std::string& Album::title() const
return m_title;
}
time_t Album::releaseYear() const
{
return m_releaseYear;
}
bool Album::setReleaseYear( time_t date )
{
static const std::string req = "UPDATE " + policy::AlbumTable::Name
+ " SET release_year = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
return false;
m_releaseYear = date;
return true;
}
const std::string& Album::shortSummary() const
{
return m_shortSummary;
......@@ -205,7 +188,6 @@ 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,"
"last_sync_date UNSIGNED INTEGER,"
......
......@@ -56,8 +56,6 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
virtual unsigned int id() const override;
virtual const std::string& title() const override;
virtual time_t releaseYear() const override;
bool setReleaseYear( time_t date );
virtual const std::string& shortSummary() const override;
bool setShortSummary( const std::string& summary );
virtual const std::string& artworkUrl() const override;
......@@ -82,7 +80,6 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
unsigned int m_id;
std::string m_title;
unsigned int m_artistId;
unsigned int m_releaseYear;
std::string m_shortSummary;
std::string m_artworkUrl;
time_t m_lastSyncDate;
......
......@@ -39,7 +39,8 @@ AlbumTrack::AlbumTrack(DBConnection dbConnection, sqlite::Row& row )
>> m_artist
>> m_genre
>> m_trackNumber
>> m_albumId;
>> m_albumId
>> m_releaseYear;
}
//FIXME: constify media
......@@ -48,6 +49,7 @@ AlbumTrack::AlbumTrack( Media* media, unsigned int trackNumber, unsigned int alb
, m_mediaId( media->id() )
, m_trackNumber( trackNumber )
, m_albumId( albumId )
, m_releaseYear( 0 )
, m_album( nullptr )
{
}
......@@ -83,6 +85,7 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
"genre TEXT,"
"track_number UNSIGNED INTEGER,"
"album_id UNSIGNED INTEGER NOT NULL,"
"release_year UNSIGNED INTEGER,"
"FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name + "(id_media)"
" ON DELETE CASCADE, "
"FOREIGN KEY (album_id) REFERENCES Album(id_album) "
......@@ -110,7 +113,7 @@ const std::string& AlbumTrack::genre()
bool AlbumTrack::setGenre(const std::string& genre)
{
static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name
+ " SET genre = ? WHERE id_track = ? ";
+ " SET genre = ? WHERE id_track = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, genre, m_id ) == false )
return false;
m_genre = genre;
......@@ -122,6 +125,23 @@ unsigned int AlbumTrack::trackNumber()
return m_trackNumber;
}
unsigned int AlbumTrack::releaseYear() const
{
return m_releaseYear;
}
bool AlbumTrack::setReleaseYear(unsigned int year)
{
if ( m_releaseYear == year )
return true;
static const std::string req = "UPDATE " + policy::AlbumTrackTable::Name +
" SET release_year = ? WHERE id_track = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, year, m_id ) == false )
return false;
m_releaseYear = year;
return true;
}
std::shared_ptr<IAlbum> AlbumTrack::album()
{
if ( m_album == nullptr && m_albumId != 0 )
......
......@@ -58,6 +58,8 @@ class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, pol
virtual const std::string& genre() override;
bool setGenre( const std::string& genre );
virtual unsigned int trackNumber() override;
virtual unsigned int releaseYear() const override;
bool setReleaseYear( unsigned int year );
virtual std::shared_ptr<IAlbum> album() override;
static bool createTable( DBConnection dbConnection );
......@@ -72,6 +74,7 @@ class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, pol
std::string m_genre;
unsigned int m_trackNumber;
unsigned int m_albumId;
unsigned int m_releaseYear;
std::shared_ptr<Album> m_album;
......
......@@ -204,7 +204,6 @@ std::shared_ptr<Album> VLCMetadataService::findAlbum( const std::string& title,
if ( artistName.empty() == true )
artistName = vlcMedia.meta( libvlc_meta_Artist );
auto date = vlcMedia.meta( libvlc_meta_Date );
auto i_date = (time_t)atoi( date.c_str() );
for ( auto it = begin( albums ); it != end( albums ); )
{
......@@ -220,14 +219,6 @@ std::shared_ptr<Album> VLCMetadataService::findAlbum( const std::string& title,
continue;
}
}
if ( date.empty() == false )
{
if ( i_date != a->releaseYear() )
{
it = albums.erase( it );
continue;
}
}
++it;
}
if ( albums.size() == 0 )
......@@ -253,9 +244,6 @@ std::pair<std::shared_ptr<Album>, bool> VLCMetadataService::handleAlbum( std::sh
if ( album != nullptr )
{
newAlbum = true;
auto date = vlcMedia.meta( libvlc_meta_Date );
if ( date.length() > 0 )
album->setReleaseYear( std::stoul( date ) );
auto artwork = vlcMedia.meta( libvlc_meta_ArtworkURL );
if ( artwork.length() != 0 )
album->setArtworkUrl( artwork );
......
......@@ -119,15 +119,11 @@ void Tests::checkAlbums(const rapidjson::Value& expectedAlbums )
// Start by checking if the album was found
const auto title = expectedAlbum["title"].GetString();
const char* artist = nullptr;
auto releaseYear = -1;
if ( expectedAlbum.HasMember( "releaseYear" ) )
releaseYear = expectedAlbum["releaseYear"].GetInt();
if ( expectedAlbum.HasMember( "artist" ) )
artist = expectedAlbum["artist"].GetString();
auto it = std::find_if( begin( albums ), end( albums ), [title, artist, releaseYear](const AlbumPtr& a) {
auto it = std::find_if( begin( albums ), end( albums ), [title, artist](const AlbumPtr& a) {
return strcasecmp( a->title().c_str(), title ) == 0 &&
( artist == nullptr || strcasecmp( a->albumArtist()->name().c_str(), artist ) == 0 ) &&
( releaseYear == -1 || releaseYear == a->releaseYear() );
( artist == nullptr || strcasecmp( a->albumArtist()->name().c_str(), artist ) == 0 );
});
ASSERT_NE( end( albums ), it );
auto album = *it;
......@@ -165,10 +161,6 @@ void Tests::checkAlbums(const rapidjson::Value& expectedAlbums )
checkAlbumTracks( album.get(), tracks, expectedAlbum["tracks"] );
}
}
if ( releaseYear != -1 )
{
ASSERT_EQ( releaseYear, album->releaseYear() );
}
}
}
......
......@@ -111,19 +111,6 @@ TEST_F( Albums, SetGenre )
ASSERT_EQ( t->genre(), t2->albumTrack()->genre() );
}
TEST_F( Albums, SetReleaseDate )
{
auto a = ml->createAlbum( "album" );
a->setReleaseYear( 1234 );
ASSERT_EQ( a->releaseYear(), 1234 );
Reload();
auto a2 = ml->album( a->id() );
ASSERT_EQ( a->releaseYear(), a2->releaseYear() );
}
TEST_F( Albums, SetShortSummary )
{
auto a = ml->createAlbum( "album" );
......
......@@ -49,3 +49,21 @@ TEST_F( AlbumTracks, Artist )
auto albumTrack = file->albumTrack();
ASSERT_EQ( albumTrack->artist(), "artist" );
}
TEST_F( AlbumTracks, SetReleaseYear )
{
auto a = ml->createAlbum( "album" );
auto m = ml->addFile( "test.mp3", nullptr );
auto t = a->addTrack( m, 1 );
ASSERT_EQ( 0, t->releaseYear() );
t->setReleaseYear( 1234 );
ASSERT_EQ( t->releaseYear(), 1234 );
Reload();
auto m2 = ml->file( "test.mp3" );
auto t2 = m2->albumTrack();
ASSERT_EQ( t->releaseYear(), t2->releaseYear() );
}
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