Commit 5261a3c7 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Artist: Allow sorting by most recently played tracks

Fix #240
parent 2c0a83bc
......@@ -91,6 +91,7 @@ enum class SortingCriteria
NbMedia,
// Only valid for artists for now
NbAlbum,
LastPlaybackDate,
};
......
......@@ -215,6 +215,11 @@ std::string Artist::addRequestJoin(const QueryParameters* params)
{
default:
return std::string{};
case SortingCriteria::LastPlaybackDate:
return std::string{ " INNER JOIN " } + MediaRelationTable::Name +
" mrt ON art.id_artist = mrt.artist_id"
" INNER JOIN " + Media::Table::Name +
" m ON mrt.media_id = m.id_media";
}
}
......@@ -741,6 +746,10 @@ std::string Artist::sortRequest( const QueryParameters* params )
case SortingCriteria::TrackNumber:
req += "art.nb_tracks";
break;
case SortingCriteria::LastPlaybackDate:
req = " GROUP BY art.id_artist"
" ORDER BY MAX(IFNULL(m.last_played_date, 0))";
break;
}
if ( desc == true )
req += " DESC";
......
......@@ -775,6 +775,75 @@ static void SortByNbTracks( Tests* T )
ASSERT_EQ( artist2->id(), artists[1]->id() );
}
static void SortByLastPlayedDate( Tests* T )
{
auto artist1 = T->ml->createArtist( "A artist" );
auto artist2 = T->ml->createArtist( "Z artist" );
auto a1m1 = std::static_pointer_cast<Media>(
T->ml->addMedia( "a1m1.mp3", IMedia::Type::Audio ) );
auto a1m2 = std::static_pointer_cast<Media>(
T->ml->addMedia( "a1m2.mp3", IMedia::Type::Audio ) );
auto a2m1 = std::static_pointer_cast<Media>(
T->ml->addMedia( "a2m1.mp3", IMedia::Type::Audio ) );
auto a2m2 = std::static_pointer_cast<Media>(
T->ml->addMedia( "a2m2.mp3", IMedia::Type::Audio ) );
ASSERT_NON_NULL( a1m1 );
ASSERT_NON_NULL( a1m2 );
ASSERT_NON_NULL( a2m1 );
ASSERT_NON_NULL( a2m2 );
auto res = artist1->addMedia( *a1m1 );
ASSERT_TRUE( res );
res = artist1->addMedia( *a1m2 );
ASSERT_TRUE( res );
res = artist2->addMedia( *a2m1 );
ASSERT_TRUE( res );
res = artist2->addMedia( *a2m2 );
ASSERT_TRUE( res );
res = T->ml->setMediaLastPlayedDate( a1m1->id(), 0 );
ASSERT_TRUE( res );
res = T->ml->setMediaLastPlayedDate( a1m2->id(), 1 );
ASSERT_TRUE( res );
res = T->ml->setMediaLastPlayedDate( a2m1->id(), 0 );
ASSERT_TRUE( res );
res = T->ml->setMediaLastPlayedDate( a2m2->id(), 0 );
ASSERT_TRUE( res );
QueryParameters params{};
params.sort = SortingCriteria::LastPlaybackDate;
params.desc = false;
auto artists = T->ml->artists( ArtistIncluded::All, &params )->all();
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( artist2->id(), artists[0]->id() );
ASSERT_EQ( artist1->id(), artists[1]->id() );
params.desc = true;
artists = T->ml->artists( ArtistIncluded::All, &params )->all();
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( artist1->id(), artists[0]->id() );
ASSERT_EQ( artist2->id(), artists[1]->id() );
res = T->ml->setMediaLastPlayedDate( a2m1->id(), 10 );
ASSERT_TRUE( res );
params.desc = false;
artists = T->ml->artists( ArtistIncluded::All, &params )->all();
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( artist1->id(), artists[0]->id() );
ASSERT_EQ( artist2->id(), artists[1]->id() );
res = T->ml->setMediaLastPlayedDate( a1m1->id(), 100 );
ASSERT_TRUE( res );
artists = T->ml->artists( ArtistIncluded::All, &params )->all();
ASSERT_EQ( 2u, artists.size() );
ASSERT_EQ( artist2->id(), artists[0]->id() );
ASSERT_EQ( artist1->id(), artists[1]->id() );
}
int main( int ac, char** av )
{
INIT_TESTS( Artist )
......@@ -806,6 +875,7 @@ int main( int ac, char** av )
ADD_TEST( CheckDbModel );
ADD_TEST( SortByNbAlbums );
ADD_TEST( SortByNbTracks );
ADD_TEST( SortByLastPlayedDate );
END_TESTS
}
......@@ -248,6 +248,7 @@ artist_tests = [
'CheckDbModel',
'SortByNbAlbums',
'SortByNbTracks',
'SortByLastPlayedDate',
]
foreach t : artist_tests
......
Supports Markdown
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