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

Album: Provide a genre filter when listing tracks

parent 559f2190
......@@ -45,6 +45,12 @@ public:
* @brief tracks fetches album tracks from the database
*/
virtual std::vector<std::shared_ptr<IMedia>> tracks() const = 0;
/**
* @brief tracks fetches album tracks, filtered by genre
* @param genre A musical genre. Only tracks of this genre will be returned
* @return
*/
virtual std::vector<std::shared_ptr<IMedia>> tracks( GenrePtr genre ) const = 0;
/**
* @brief albumArtist Returns the album main artist (generally tagged as album-artist)
*/
......
......@@ -25,6 +25,7 @@
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "IGenre.h"
#include "Media.h"
#include "database/SqliteTools.h"
......@@ -144,6 +145,18 @@ std::vector<MediaPtr> Album::tracks() const
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
}
std::vector<std::shared_ptr<IMedia> > Album::tracks( GenrePtr genre ) const
{
if ( genre == nullptr )
return {};
static const std::string req = "SELECT med.* FROM " + policy::MediaTable::Name + " med "
" INNER JOIN " + policy::AlbumTrackTable::Name + " att ON att.media_id = med.id_media "
" WHERE att.album_id = ? AND med.is_present = 1"
" AND genre_id = ?"
" ORDER BY att.disc_number, att.track_number";
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id, genre->id() );
}
std::vector<MediaPtr> Album::cachedTracks() const
{
auto lock = m_tracks.lock();
......
......@@ -73,6 +73,7 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
virtual const std::string& artworkMrl() const override;
bool setArtworkMrl( const std::string& artworkMrl );
virtual std::vector<MediaPtr> tracks() const override;
virtual std::vector<std::shared_ptr<IMedia>> tracks( GenrePtr genre ) const override;
///
/// \brief cachedTracks Returns a cached list of tracks
/// This has no warranty of ordering, validity, or anything else.
......
......@@ -25,6 +25,7 @@
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "Genre.h"
#include "Media.h"
#include "IMediaLibrary.h"
......@@ -95,6 +96,31 @@ TEST_F( Albums, NbTracks )
ASSERT_EQ( tracks.size(), a->nbTracks() );
}
TEST_F( Albums, TracksByGenre )
{
auto a = ml->createAlbum( "albumtag" );
auto g = ml->createGenre( "genre" );
for ( auto i = 1u; i <= 10; ++i )
{
auto f = ml->addFile( "track" + std::to_string(i) + ".mp3" );
auto track = a->addTrack( f, i, i );
f->save();
ASSERT_NE( track, nullptr );
if ( i <= 5 )
track->setGenre( g );
}
auto tracks = a->tracks( g );
ASSERT_EQ( 5u, tracks.size() );
Reload();
a = std::static_pointer_cast<Album>( ml->album( a->id() ) );
tracks = a->tracks( g );
ASSERT_NE( tracks.size(), a->nbTracks() );
ASSERT_EQ( 5u, tracks.size() );
}
TEST_F( Albums, SetReleaseDate )
{
auto a = ml->createAlbum( "album" );
......
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