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

Genre: Sort albums

parent 6f885672
......@@ -34,5 +34,5 @@ public:
virtual const std::string& name() const = 0;
virtual std::vector<ArtistPtr> artists( medialibrary::SortingCriteria sort, bool desc ) const = 0;
virtual std::vector<AlbumTrackPtr> tracks( medialibrary::SortingCriteria sort, bool desc ) const = 0;
virtual std::vector<AlbumPtr> albums() const = 0;
virtual std::vector<AlbumPtr> albums( medialibrary::SortingCriteria sort, bool desc ) const = 0;
};
......@@ -164,6 +164,26 @@ std::string Album::orderTracksBy( medialibrary::SortingCriteria sort, bool desc
return req;
}
std::string Album::orderBy( medialibrary::SortingCriteria sort, bool desc )
{
std::string req = " ORDER BY ";
switch ( sort )
{
case medialibrary::SortingCriteria::ReleaseDate:
if ( desc == true )
req += "release_year DESC, title";
else
req += "release_year, title";
break;
default:
req += "title";
if ( desc == true )
req += " DESC";
break;
}
return req;
}
std::vector<MediaPtr> Album::tracks( medialibrary::SortingCriteria sort, bool desc ) const
{
// This doesn't return the cached version, because it would be fairly complicated, if not impossible or
......@@ -416,23 +436,19 @@ std::vector<AlbumPtr> Album::fromArtist( MediaLibraryPtr ml, unsigned int artist
return fetchAll<IAlbum>( ml, req, artistId );
}
std::vector<AlbumPtr> Album::fromGenre(MediaLibraryPtr ml, unsigned int genreId, medialibrary::SortingCriteria sort, bool desc)
{
std::string req = "SELECT a.* FROM " + policy::AlbumTable::Name + " a "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.album_id = a.id_album "
"WHERE att.genre_id = ? GROUP BY att.album_id";
req += orderBy( sort, desc );
return fetchAll<IAlbum>( ml, req, genreId );
}
std::vector<AlbumPtr> Album::listAll( MediaLibraryPtr ml, medialibrary::SortingCriteria sort, bool desc )
{
std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" WHERE is_present=1 ORDER BY ";
switch ( sort )
{
case medialibrary::SortingCriteria::ReleaseDate:
if ( desc == true )
req += "release_year DESC, title";
else
req += "release_year, title";
break;
default:
req += "title";
if ( desc == true )
req += " DESC";
break;
}
" WHERE is_present=1";
req += orderBy( sort, desc );
return fetchAll<IAlbum>( ml, req );
}
......@@ -104,12 +104,13 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
/// \return
///
static std::vector<AlbumPtr> search( MediaLibraryPtr ml, const std::string& pattern );
static std::vector<AlbumPtr> fromArtist(MediaLibraryPtr ml, unsigned int artistId , medialibrary::SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> fromArtist( MediaLibraryPtr ml, unsigned int artistId, medialibrary::SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> fromGenre( MediaLibraryPtr ml, unsigned int genreId, medialibrary::SortingCriteria sort, bool desc );
static std::vector<AlbumPtr> listAll( MediaLibraryPtr ml, medialibrary::SortingCriteria sort, bool desc );
private:
static std::string orderTracksBy( medialibrary::SortingCriteria sort, bool desc );
static std::string orderBy( medialibrary::SortingCriteria sort, bool desc );
protected:
MediaLibraryPtr m_ml;
unsigned int m_id;
......
......@@ -72,12 +72,9 @@ std::vector<AlbumTrackPtr> Genre::tracks( medialibrary::SortingCriteria sort, bo
return AlbumTrack::fromGenre( m_ml, m_id, sort, desc );
}
std::vector<AlbumPtr> Genre::albums() const
std::vector<AlbumPtr> Genre::albums( medialibrary::SortingCriteria sort, bool desc ) const
{
static const std::string req = "SELECT a.* FROM " + policy::AlbumTable::Name + " a "
"INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.album_id = a.id_album "
"WHERE att.genre_id = ? GROUP BY att.album_id";
return Album::fetchAll<IAlbum>( m_ml, req, m_id );
return Album::fromGenre( m_ml, m_id, sort, desc );
}
bool Genre::createTable( DBConnection dbConn )
......
......@@ -47,7 +47,7 @@ public:
virtual const std::string& name() const override;
virtual std::vector<ArtistPtr> artists( medialibrary::SortingCriteria sort, bool desc ) const override;
virtual std::vector<AlbumTrackPtr> tracks(medialibrary::SortingCriteria sort, bool desc) const override;
virtual std::vector<AlbumPtr> albums() const override;
virtual std::vector<AlbumPtr> albums( medialibrary::SortingCriteria sort, bool desc ) const override;
static bool createTable( DBConnection dbConn );
static std::shared_ptr<Genre> create( MediaLibraryPtr ml, const std::string& name );
......
......@@ -125,7 +125,7 @@ TEST_F( Genres, ListAlbums )
auto genres = ml->genres( medialibrary::SortingCriteria::Default, false );
for ( auto& genre : genres )
{
auto albums = genre->albums();
auto albums = genre->albums( medialibrary::SortingCriteria::Default, false );
if ( genre->id() == g->id() )
{
......
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