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

Album: Pass the full Genre instance when adding a track

parent 08af48cd
......@@ -29,7 +29,7 @@
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "medialibrary/IGenre.h"
#include "Genre.h"
#include "Media.h"
#include "database/SqliteTools.h"
......@@ -232,9 +232,10 @@ std::vector<MediaPtr> Album::cachedTracks() const
}
std::shared_ptr<AlbumTrack> Album::addTrack( std::shared_ptr<Media> media, unsigned int trackNb,
unsigned int discNumber, int64_t artistId, int64_t genreId )
unsigned int discNumber, int64_t artistId, Genre* genre )
{
auto track = AlbumTrack::create( m_ml, m_id, media, trackNb, discNumber, artistId, genreId, media->duration() );
auto track = AlbumTrack::create( m_ml, m_id, media, trackNb, discNumber, artistId,
genre != nullptr ? genre->id() : 0, media->duration() );
if ( track == nullptr )
return nullptr;
media->setAlbumTrack( track );
......
......@@ -88,7 +88,7 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
/// The media will be added to the tracks cache.
///
std::shared_ptr<AlbumTrack> addTrack( std::shared_ptr<Media> media, unsigned int trackNb,
unsigned int discNumber, int64_t artistId, int64_t genreId );
unsigned int discNumber, int64_t artistId, Genre* genre );
unsigned int nbTracks() const override;
unsigned int duration() const override;
......
......@@ -419,7 +419,7 @@ std::shared_ptr<AlbumTrack> MetadataParser::handleTrack( std::shared_ptr<Album>
auto track = std::static_pointer_cast<AlbumTrack>( album->addTrack( task.media, trackNumber,
discNumber, artist->id(),
genre != nullptr ? genre->id() : 0 ) );
genre ) );
if ( track == nullptr )
{
LOG_ERROR( "Failed to create album track" );
......
......@@ -65,7 +65,7 @@ TEST_F( Albums, AddTrack )
{
auto a = ml->createAlbum( "albumtag" );
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track.mp3" ) );
auto track = a->addTrack( f, 10, 0, 0, 0 );
auto track = a->addTrack( f, 10, 0, 0, nullptr );
f->save();
ASSERT_NE( track, nullptr );
......@@ -86,7 +86,7 @@ TEST_F( Albums, NbTracks )
for ( auto i = 1u; i <= 10; ++i )
{
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string(i) + ".mp3" ) );
auto track = a->addTrack( f, i, i, 0, 0 );
auto track = a->addTrack( f, i, i, 0, nullptr );
f->save();
ASSERT_NE( track, nullptr );
}
......@@ -108,7 +108,7 @@ TEST_F( Albums, TracksByGenre )
for ( auto i = 1u; i <= 10; ++i )
{
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string(i) + ".mp3" ) );
auto track = a->addTrack( f, i, i, 0, 0 );
auto track = a->addTrack( f, i, i, 0, nullptr );
f->save();
ASSERT_NE( track, nullptr );
if ( i <= 5 )
......@@ -182,7 +182,7 @@ TEST_F( Albums, FetchAlbumFromTrack )
{
auto a = ml->createAlbum( "album" );
auto f = std::static_pointer_cast<Media>( ml->addMedia( "file.mp3" ) );
auto t = a->addTrack( f, 1, 0, 0, 0 );
auto t = a->addTrack( f, 1, 0, 0, nullptr );
f->save();
Reload();
......@@ -335,7 +335,7 @@ TEST_F( Albums, AutoDelete )
{
auto a = ml->createAlbum( "album" );
auto m = std::static_pointer_cast<Media>( ml->addMedia( "media.mp3" ) );
auto t = a->addTrack( m, 1, 1, 0, 0 );
auto t = a->addTrack( m, 1, 1, 0, nullptr );
auto album = ml->album( a->id() );
ASSERT_NE( nullptr, album );
......@@ -351,8 +351,8 @@ TEST_F( Albums, SortTracks )
auto a = ml->createAlbum( "album" );
auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "B-track1.mp3" ) );
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "A-track2.mp3" ) );
auto t1 = a->addTrack( m1, 1, 1, 0, 0 );
auto t2 = a->addTrack( m2, 2, 1, 0, 0 );
auto t1 = a->addTrack( m1, 1, 1, 0, nullptr );
auto t2 = a->addTrack( m2, 2, 1, 0, nullptr );
// Default order is by disc number & track number
auto tracks = a->tracks( SortingCriteria::Default, false );
......@@ -445,18 +445,18 @@ TEST_F( Albums, Duration )
auto m = std::static_pointer_cast<Media>( ml->addMedia( "track.mp3" ) );
m->setDuration( 100 );
m->save();
a->addTrack( m, 1, 1, 0, 0 );
a->addTrack( m, 1, 1, 0, nullptr );
ASSERT_EQ( 100u, a->duration() );
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "track2.mp3" ) );
m2->setDuration( 200 );
m2->save();
auto t2 = a->addTrack( m2, 1, 1, 0, 0 );
auto t2 = a->addTrack( m2, 1, 1, 0, nullptr );
ASSERT_EQ( 300u, a->duration() );
// Check that we don't add negative durations (default sqlite duration is -1)
auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "track3.mp3" ) );
auto t3 = a->addTrack( m3, 1, 1, 0, 0 );
auto t3 = a->addTrack( m3, 1, 1, 0, nullptr );
ASSERT_EQ( 300u, a->duration() );
Reload();
......
......@@ -40,7 +40,7 @@ TEST_F( AlbumTracks, Create )
{
auto album = ml->createAlbum( "album" );
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
auto track = album->addTrack( f, 1, 10, 0, 0 );
auto track = album->addTrack( f, 1, 10, 0, nullptr );
f->save();
ASSERT_NE( nullptr, track );
ASSERT_EQ( 10u, track->discNumber() );
......@@ -55,7 +55,7 @@ TEST_F( AlbumTracks, Artist )
{
auto album = ml->createAlbum( "album" );
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
auto track = album->addTrack( f, 1, 0, 0, 0 );
auto track = album->addTrack( f, 1, 0, 0, nullptr );
f->save();
auto artist = track->artist();
......@@ -81,7 +81,7 @@ TEST_F( AlbumTracks, SetGenre )
{
auto a = ml->createAlbum( "album" );
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track.mp3" ) );
auto t = a->addTrack( f, 1, 0, 0, 0 );
auto t = a->addTrack( f, 1, 0, 0, nullptr );
f->save();
auto genre = ml->createGenre( "happy underground post progressive death metal" );
......@@ -102,7 +102,7 @@ TEST_F( AlbumTracks, Media )
{
auto album = ml->createAlbum( "album" );
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
auto track = album->addTrack( f, 1, 10, 0, 0 );
auto track = album->addTrack( f, 1, 10, 0, nullptr );
f->save();
auto media = track->media();
......@@ -121,7 +121,7 @@ TEST_F( AlbumTracks, Album )
{
auto album = ml->createAlbum( "album" );
auto f = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
auto track = album->addTrack( f, 1, 0, 0, 0 );
auto track = album->addTrack( f, 1, 0, 0, nullptr );
f->save();
auto albumFromTrack = track->album();
......
......@@ -312,7 +312,7 @@ TEST_F( Artists, DeleteWhenNoAlbum )
auto album = ml->createAlbum( "album 1" );
album->setAlbumArtist( artist );
auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "track1.mp3" ) );
auto track1 = album->addTrack( m1, 1, 1, artist->id(), 0 );
auto track1 = album->addTrack( m1, 1, 1, artist->id(), nullptr );
auto artists = ml->artists( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( 1u, artists.size() );
......
......@@ -269,8 +269,8 @@ TEST_F( DeviceFs, RemoveAlbum )
auto media = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ml->media( RemovableDeviceMountpoint + "removablefile2.mp3" );
auto artist = ml->createArtist( "artist 2" );
album->addTrack( std::static_pointer_cast<Media>( media ), 1, 1, artist->id(), 0 );
album->addTrack( std::static_pointer_cast<Media>( media ), 2, 1, artist->id(), 0 );
album->addTrack( std::static_pointer_cast<Media>( media ), 1, 1, artist->id(), nullptr );
album->addTrack( std::static_pointer_cast<Media>( media ), 2, 1, artist->id(), nullptr );
album->setAlbumArtist( artist );
}
......@@ -304,8 +304,8 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
auto media = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto media2 = ml->media( RemovableDeviceMountpoint + "removablefile2.mp3" );
auto newArtist = ml->createArtist( "artist" );
album->addTrack( std::static_pointer_cast<Media>( media ), 1, 1, newArtist->id(), 0 );
album->addTrack( std::static_pointer_cast<Media>( media2 ), 2, 1, newArtist->id(), 0 );
album->addTrack( std::static_pointer_cast<Media>( media ), 1, 1, newArtist->id(), nullptr );
album->addTrack( std::static_pointer_cast<Media>( media2 ), 2, 1, newArtist->id(), nullptr );
album->setAlbumArtist( newArtist );
newArtist->addMedia( static_cast<Media&>( *media ) );
newArtist->addMedia( static_cast<Media&>( *media2 ) );
......
......@@ -67,7 +67,7 @@ TEST_F( Genres, ListAlbumTracks )
for ( auto i = 1u; i <= 3; i++ )
{
auto m = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string( i ) + ".mp3" ) );
auto t = a->addTrack( m, i, 1, 0, 0 );
auto t = a->addTrack( m, i, 1, 0, nullptr );
if ( i != 1 )
t->setGenre( g );
}
......@@ -90,13 +90,13 @@ TEST_F( Genres, ListArtists )
for ( auto i = 1u; i <= 5; ++i )
{
auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + ".mp3" ) );
auto track = album->addTrack( m, i, 1, a->id(), 0 );
auto track = album->addTrack( m, i, 1, a->id(), nullptr );
track->setGenre( g );
}
for ( auto i = 1u; i <= 5; ++i )
{
auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + "_2.mp3" ) );
auto track = album2->addTrack( m, i, 1, a2->id(), 0 );
auto track = album2->addTrack( m, i, 1, a2->id(), nullptr );
track->setGenre( g );
}
artists = g->artists( SortingCriteria::Default, false );
......@@ -107,12 +107,12 @@ TEST_F( Genres, ListAlbums )
{
auto album = ml->createAlbum( "album" );
auto m = std::static_pointer_cast<Media>( ml->addMedia( "some track.mp3" ) );
auto t = album->addTrack( m, 10, 1, 0, 0 );
auto t = album->addTrack( m, 10, 1, 0, nullptr );
t->setGenre( g );
auto album2 = ml->createAlbum( "album2" );
m = std::static_pointer_cast<Media>( ml->addMedia( "some other track.mp3" ) );
t = album2->addTrack( m, 10, 1, 0, 0 );
t = album2->addTrack( m, 10, 1, 0, nullptr );
t->setGenre( g );
// We have 2 albums with at least a song with genre "g" (as defined in SetUp)
......@@ -120,7 +120,7 @@ TEST_F( Genres, ListAlbums )
for ( auto i = 1u; i <= 5u; ++i )
{
auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + ".mp3" ) );
auto track = album->addTrack( m, i, 1, 0, 0 );
auto track = album->addTrack( m, i, 1, 0, nullptr );
auto g = ml->createGenre( std::to_string( i ) );
track->setGenre( g );
}
......@@ -170,7 +170,7 @@ TEST_F( Genres, SortTracks )
for ( auto i = 1u; i <= 2; i++ )
{
auto m = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string( i ) + ".mp3" ) );
auto t = a->addTrack( m, i, 1, 0, 0 );
auto t = a->addTrack( m, i, 1, 0, nullptr );
m->setDuration( i );
m->save();
t->setGenre( g );
......
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