Commit 49c998cc authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Artist: Implement sorting for media listing

parent b94b37c6
......@@ -35,7 +35,7 @@ public:
virtual const std::string& name() const = 0;
virtual const std::string& shortBio() const = 0;
virtual std::vector<AlbumPtr> albums() const = 0;
virtual std::vector<MediaPtr> media() const = 0;
virtual std::vector<MediaPtr> media( medialibrary::SortingCriteria sort, bool desc ) const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual const std::string& musicBrainzId() const = 0;
};
......@@ -87,11 +87,29 @@ std::vector<AlbumPtr> Artist::albums() const
return Album::fetchAll<IAlbum>( m_ml, req, m_id );
}
std::vector<MediaPtr> Artist::media() const
std::vector<MediaPtr> Artist::media( medialibrary::SortingCriteria sort, bool desc ) const
{
static const std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med "
std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med "
"INNER JOIN MediaArtistRelation mar ON mar.media_id = med.id_media "
"WHERE mar.artist_id = ? AND med.is_present = 1";
"WHERE mar.artist_id = ? AND med.is_present = 1 ORDER BY ";
switch ( sort )
{
case medialibrary::SortingCriteria::Duration:
req += "med.duration";
break;
case medialibrary::SortingCriteria::InsertionDate:
req += "med.insertion_date";
break;
case medialibrary::SortingCriteria::ReleaseDate:
req += "med.release_date";
break;
default:
req += "med.title";
break;
}
if ( desc == true )
req += " DESC";
return Media::fetchAll<IMedia>( m_ml, req, m_id );
}
......
......@@ -51,7 +51,7 @@ public:
virtual const std::string& shortBio() const override;
bool setShortBio( const std::string& shortBio );
virtual std::vector<AlbumPtr> albums() const override;
virtual std::vector<MediaPtr> media() const override;
virtual std::vector<MediaPtr> media(medialibrary::SortingCriteria sort, bool desc) const override;
bool addMedia( Media& media );
virtual const std::string& artworkMrl() const override;
bool setArtworkMrl( const std::string& artworkMrl );
......
......@@ -228,7 +228,7 @@ void Tests::checkArtists(const rapidjson::Value& expectedArtists, std::vector<Ar
}
if ( expectedArtist.HasMember( "nbTracks" ) )
{
auto tracks = artist->media();
auto tracks = artist->media( medialibrary::SortingCriteria::Default, false );
if ( expectedArtist["nbTracks"].GetUint() != tracks.size() )
return false;
}
......
......@@ -123,13 +123,13 @@ TEST_F( Artists, AllSongs )
ASSERT_TRUE( res );
}
auto songs = artist->media();
auto songs = artist->media( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( songs.size(), 3u );
Reload();
auto artist2 = ml->artist( "Cannibal Otters" );
songs = artist2->media();
songs = artist2->media( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( songs.size(), 3u );
}
......@@ -215,3 +215,28 @@ TEST_F( Artists, SearchAfterDelete )
artists = ml->searchArtists( "artist" );
ASSERT_EQ( 1u, artists.size() );
}
TEST_F( Artists, SortMedia )
{
auto artist = ml->createArtist( "Russian Otters" );
for (auto i = 1; i <= 3; ++i)
{
auto f = ml->addFile( "song" + std::to_string(i) + ".mp3" );
f->setDuration( 10 - i );
f->save();
artist->addMedia( *f );
}
auto tracks = artist->media( medialibrary::SortingCriteria::Duration, false );
ASSERT_EQ( 3u, tracks.size() );
ASSERT_EQ( "song3.mp3", tracks[0]->title() ); // Duration: 8
ASSERT_EQ( "song2.mp3", tracks[1]->title() ); // Duration: 9
ASSERT_EQ( "song1.mp3", tracks[2]->title() ); // Duration: 10
tracks = artist->media( medialibrary::SortingCriteria::Duration, true );
ASSERT_EQ( 3u, tracks.size() );
ASSERT_EQ( "song1.mp3", tracks[0]->title() );
ASSERT_EQ( "song2.mp3", tracks[1]->title() );
ASSERT_EQ( "song3.mp3", tracks[2]->title() );
}
......@@ -312,7 +312,7 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
auto artists = ml->artists();
ASSERT_EQ( 1u, artists.size() );
auto artist = artists[0];
ASSERT_EQ( 2u, artist->media().size() );
ASSERT_EQ( 2u, artist->media( medialibrary::SortingCriteria::Default, false ).size() );
auto device = fsMock->removeDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
......@@ -325,7 +325,7 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
artists = ml->artists();
ASSERT_EQ( 1u, artists.size() );
ASSERT_EQ( 1u, albums[0]->tracks( medialibrary::SortingCriteria::Default, false ).size() );
ASSERT_EQ( 1u, artists[0]->media().size() );
ASSERT_EQ( 1u, artists[0]->media( medialibrary::SortingCriteria::Default, false ).size() );
}
TEST_F( DeviceFs, ChangeDevice )
......
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