Commit 559f2190 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Genre: Provide listing of albums

parent 5266b5f9
......@@ -34,4 +34,5 @@ public:
virtual const std::string& name() const = 0;
virtual std::vector<ArtistPtr> artists() const = 0;
virtual std::vector<AlbumTrackPtr> tracks() const = 0;
virtual std::vector<AlbumPtr> albums() const = 0;
};
......@@ -22,6 +22,7 @@
#include "Genre.h"
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
......@@ -67,6 +68,14 @@ std::vector<AlbumTrackPtr> Genre::tracks() const
return AlbumTrack::fromGenre( m_dbConnection, m_id );
}
std::vector<AlbumPtr> Genre::albums() 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_dbConnection, req, m_id );
}
bool Genre::createTable( DBConnection dbConn )
{
static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::GenreTable::Name +
......
......@@ -47,6 +47,7 @@ public:
virtual const std::string& name() const override;
virtual std::vector<ArtistPtr> artists() const override;
virtual std::vector<AlbumTrackPtr> tracks() const override;
virtual std::vector<AlbumPtr> albums() const override;
static bool createTable( DBConnection dbConn );
static std::shared_ptr<Genre> create( DBConnection dbConn, const std::string& name );
......
......@@ -98,3 +98,43 @@ TEST_F( Genres, ListArtists )
artists = g->artists();
ASSERT_EQ( 2u, artists.size() );
}
TEST_F( Genres, ListAlbums )
{
auto album = ml->createAlbum( "album" );
auto m = ml->addFile( "some track.mp3" );
auto t = album->addTrack( m, 10, 1 );
t->setGenre( g );
auto album2 = ml->createAlbum( "album2" );
m = ml->addFile( "some other track.mp3" );
t = album2->addTrack( m, 10, 1 );
t->setGenre( g );
// We have 2 albums with at least a song with genre "g" (as defined in SetUp)
// Now we create more albums with "random" genre, all of them should have 1 album
for ( auto i = 1u; i <= 5u; ++i )
{
auto m = ml->addFile( std::to_string( i ) + ".mp3" );
auto track = album->addTrack( m, i, 1 );
auto g = ml->createGenre( std::to_string( i ) );
track->setGenre( g );
}
auto genres = ml->genres();
for ( auto& genre : genres )
{
auto albums = genre->albums();
if ( genre->id() == g->id() )
{
// Initial genre with 2 albums:
ASSERT_EQ( 2u, albums.size() );
}
else
{
ASSERT_EQ( 1u, albums.size() );
ASSERT_EQ( album->id(), albums[0]->id() );
}
}
}
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