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

IAlbumTrack: Provide a method to fetch the associated media

parent 074d5a06
......@@ -43,6 +43,7 @@ class IAlbumTrack
virtual GenrePtr genre() = 0;
virtual unsigned int trackNumber() = 0;
virtual AlbumPtr album() = 0;
virtual MediaPtr media() = 0;
/**
* @return Which disc this tracks appears on (or 0 if unspecified)
*/
......
......@@ -203,3 +203,13 @@ std::shared_ptr<IAlbum> AlbumTrack::album()
}
return album;
}
std::shared_ptr<IMedia> AlbumTrack::media()
{
auto lock = m_media.lock();
if ( m_media.isCached() == false )
{
m_media = Media::fetch( m_ml, m_mediaId );
}
return m_media.get().lock();
}
......@@ -64,6 +64,7 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
bool setReleaseYear( unsigned int year );
virtual unsigned int discNumber() const override;
virtual std::shared_ptr<IAlbum> album() override;
virtual std::shared_ptr<IMedia> media() override;
static bool createTable( DBConnection dbConnection );
static std::shared_ptr<AlbumTrack> create( MediaLibraryPtr ml, unsigned int albumId,
......@@ -87,6 +88,7 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
std::weak_ptr<Album> m_album;
mutable Cache<std::shared_ptr<Artist>> m_artist;
mutable Cache<std::shared_ptr<Genre>> m_genre;
mutable Cache<std::weak_ptr<Media>> m_media;
friend struct policy::AlbumTrackTable;
};
......
......@@ -112,3 +112,22 @@ TEST_F( AlbumTracks, SetGenre )
auto t2 = tracks[0];
ASSERT_EQ( t->genre()->id(), t2->albumTrack()->genre()->id() );
}
TEST_F( AlbumTracks, Media )
{
auto album = ml->createAlbum( "album" );
auto f = ml->addFile( "track1.mp3" );
auto track = album->addTrack( f, 1, 10 );
f->save();
auto media = track->media();
ASSERT_NE( nullptr, media );
ASSERT_EQ( media->id(), f->id() );
Reload();
track = ml->albumTrack( track->id() );
media = track->media();
ASSERT_NE( nullptr, media );
ASSERT_EQ( media->id(), f->id() );
}
......@@ -172,3 +172,8 @@ void MediaLibraryTester::deleteTrack(unsigned int trackId)
{
AlbumTrack::destroy( this, trackId );
}
std::shared_ptr<AlbumTrack> MediaLibraryTester::albumTrack( unsigned int id )
{
return AlbumTrack::fetch( this, id );
}
......@@ -23,6 +23,7 @@
#include "gtest/gtest.h"
class Playlist;
class AlbumTrack;
#include "factory/IFileSystem.h"
#include "MediaLibrary.h"
......@@ -45,6 +46,7 @@ public:
std::shared_ptr<Device> addDevice( const std::string& uuid, bool isRemovable );
void setFsFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
void deleteTrack( unsigned int trackId );
std::shared_ptr<AlbumTrack> albumTrack( unsigned int id );
private:
std::unique_ptr<fs::IDirectory> dummyDirectory;
......
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