[Remote] Added genre filter on artist searches
This commit is contained in:
@@ -17,8 +17,12 @@
|
|||||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <boost/foreach.hpp>
|
||||||
|
|
||||||
#include "AudioTypes.hpp"
|
#include "AudioTypes.hpp"
|
||||||
|
|
||||||
|
#include "SqlQuery.hpp"
|
||||||
|
|
||||||
namespace Database
|
namespace Database
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -63,5 +67,40 @@ Artist::getAllOrphans(Wt::Dbo::Session& session)
|
|||||||
return session.query< Wt::Dbo::ptr<Artist> >("select a from artist a LEFT OUTER JOIN Track t ON a.id = t.artist_id WHERE t.id IS NULL");
|
return session.query< Wt::Dbo::ptr<Artist> >("select a from artist a LEFT OUTER JOIN Track t ON a.id = t.artist_id WHERE t.id IS NULL");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Wt::Dbo::collection<Artist::pointer>
|
||||||
|
Artist::getAll(Wt::Dbo::Session& session,
|
||||||
|
const std::vector<Genre::id_type>& genreIds,
|
||||||
|
int offset, int size)
|
||||||
|
{
|
||||||
|
std::string sqlQuery = "SELECT a FROM artist a";
|
||||||
|
|
||||||
|
if (!genreIds.empty())
|
||||||
|
{
|
||||||
|
sqlQuery += " INNER JOIN genre g ON g.id = t_g.genre_id";
|
||||||
|
sqlQuery += " INNER JOIN track t ON t.id = t_g.track_id AND t.artist_id = a.id";
|
||||||
|
sqlQuery += " INNER JOIN track_genre t_g ON t_g.track_id = t.id AND t_g.genre_id = g.id";
|
||||||
|
}
|
||||||
|
|
||||||
|
WhereClause where;
|
||||||
|
{
|
||||||
|
WhereClause genreWhere;
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < genreIds.size(); ++i)
|
||||||
|
genreWhere.Or( WhereClause("g.id = ?") );
|
||||||
|
|
||||||
|
where.And(genreWhere);
|
||||||
|
}
|
||||||
|
|
||||||
|
Wt::Dbo::Query<Artist::pointer> query = session.query<Artist::pointer>( sqlQuery + " " + where.get() ).offset(offset).limit(size);
|
||||||
|
|
||||||
|
BOOST_FOREACH(const Genre::id_type genreId, genreIds)
|
||||||
|
query.bind(genreId);
|
||||||
|
|
||||||
|
query.groupBy("a");
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|
||||||
|
|||||||
+41
-37
@@ -36,6 +36,44 @@ class Track;
|
|||||||
class Release;
|
class Release;
|
||||||
class Artist;
|
class Artist;
|
||||||
|
|
||||||
|
class Genre
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef Wt::Dbo::ptr<Genre> pointer;
|
||||||
|
typedef Wt::Dbo::dbo_traits<Genre>::IdType id_type;
|
||||||
|
|
||||||
|
Genre();
|
||||||
|
Genre(const std::string& name);
|
||||||
|
|
||||||
|
// Find utility
|
||||||
|
static pointer getByName(Wt::Dbo::Session& session, const std::string& name);
|
||||||
|
static pointer getNone(Wt::Dbo::Session& session);
|
||||||
|
static Wt::Dbo::collection<pointer> getAll(Wt::Dbo::Session& session, std::size_t offset = -1, std::size_t size = -1);
|
||||||
|
|
||||||
|
// Create utility
|
||||||
|
static pointer create(Wt::Dbo::Session& session, const std::string& name);
|
||||||
|
|
||||||
|
// Accessors
|
||||||
|
const std::string& getName(void) const { return _name; }
|
||||||
|
bool isNone(void) const;
|
||||||
|
const Wt::Dbo::collection< Wt::Dbo::ptr<Track> >& getTracks() const { return _tracks;}
|
||||||
|
|
||||||
|
template<class Action>
|
||||||
|
void persist(Action& a)
|
||||||
|
{
|
||||||
|
Wt::Dbo::field(a, _name, "name");
|
||||||
|
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_genre", "", Wt::Dbo::OnDeleteCascade);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const std::size_t _maxNameLength = 128;
|
||||||
|
std::string _name;
|
||||||
|
|
||||||
|
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class Artist
|
class Artist
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -50,6 +88,9 @@ class Artist
|
|||||||
static pointer getByName(Wt::Dbo::Session& session, const std::string& name);
|
static pointer getByName(Wt::Dbo::Session& session, const std::string& name);
|
||||||
static pointer getNone(Wt::Dbo::Session& session);
|
static pointer getNone(Wt::Dbo::Session& session);
|
||||||
static Wt::Dbo::collection<pointer> getAll(Wt::Dbo::Session& session, int offset = -1, int size = -1);
|
static Wt::Dbo::collection<pointer> getAll(Wt::Dbo::Session& session, int offset = -1, int size = -1);
|
||||||
|
static Wt::Dbo::collection<pointer> getAll(Wt::Dbo::Session& session,
|
||||||
|
const std::vector<Genre::id_type>& genreIds,
|
||||||
|
int offset = -1, int size = -1);
|
||||||
static Wt::Dbo::collection<pointer> getAllOrphans(Wt::Dbo::Session& session);
|
static Wt::Dbo::collection<pointer> getAllOrphans(Wt::Dbo::Session& session);
|
||||||
|
|
||||||
const std::string& getName(void) const { return _name; }
|
const std::string& getName(void) const { return _name; }
|
||||||
@@ -122,43 +163,6 @@ class Release
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class Genre
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef Wt::Dbo::ptr<Genre> pointer;
|
|
||||||
typedef Wt::Dbo::dbo_traits<Genre>::IdType id_type;
|
|
||||||
|
|
||||||
Genre();
|
|
||||||
Genre(const std::string& name);
|
|
||||||
|
|
||||||
// Find utility
|
|
||||||
static pointer getByName(Wt::Dbo::Session& session, const std::string& name);
|
|
||||||
static pointer getNone(Wt::Dbo::Session& session);
|
|
||||||
static Wt::Dbo::collection<pointer> getAll(Wt::Dbo::Session& session, std::size_t offset = -1, std::size_t size = -1);
|
|
||||||
|
|
||||||
// Create utility
|
|
||||||
static pointer create(Wt::Dbo::Session& session, const std::string& name);
|
|
||||||
|
|
||||||
// Accessors
|
|
||||||
const std::string& getName(void) const { return _name; }
|
|
||||||
bool isNone(void) const;
|
|
||||||
const Wt::Dbo::collection< Wt::Dbo::ptr<Track> >& getTracks() const { return _tracks;}
|
|
||||||
|
|
||||||
template<class Action>
|
|
||||||
void persist(Action& a)
|
|
||||||
{
|
|
||||||
Wt::Dbo::field(a, _name, "name");
|
|
||||||
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_genre", "", Wt::Dbo::OnDeleteCascade);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static const std::size_t _maxNameLength = 128;
|
|
||||||
std::string _name;
|
|
||||||
|
|
||||||
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Track
|
class Track
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -164,11 +164,16 @@ AudioCollectionRequestHandler::processGetArtists(const AudioCollectionRequest::G
|
|||||||
size = _maxListArtists;
|
size = _maxListArtists;
|
||||||
size = std::min(size, _maxListArtists);
|
size = std::min(size, _maxListArtists);
|
||||||
|
|
||||||
|
// Get filters
|
||||||
|
std::vector<Database::Artist::id_type> genreIds;
|
||||||
|
for (int id = 0; id < request.genre_id_size(); ++id)
|
||||||
|
genreIds.push_back( request.genre_id(id) );
|
||||||
|
|
||||||
// Now fetch requested data...
|
// Now fetch requested data...
|
||||||
|
|
||||||
Wt::Dbo::Transaction transaction( _db.getSession() );
|
Wt::Dbo::Transaction transaction( _db.getSession() );
|
||||||
|
|
||||||
Wt::Dbo::collection<Database::Artist::pointer> artists = Database::Artist::getAll( _db.getSession(), request.batch_parameter().offset(), static_cast<int>(size) );
|
Wt::Dbo::collection<Database::Artist::pointer> artists = Database::Artist::getAll( _db.getSession(), genreIds, request.batch_parameter().offset(), static_cast<int>(size) );
|
||||||
|
|
||||||
typedef Wt::Dbo::collection< Database::Artist::pointer > Artists;
|
typedef Wt::Dbo::collection< Database::Artist::pointer > Artists;
|
||||||
|
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ class TestClient
|
|||||||
_socket.handshake(boost::asio::ssl::stream_base::client);
|
_socket.handshake(boost::asio::ssl::stream_base::client);
|
||||||
}
|
}
|
||||||
|
|
||||||
void getArtists(std::vector<ArtistInfo>& artists)
|
void getArtists(std::vector<ArtistInfo>& artists, const std::vector<uint64_t>& genreIds)
|
||||||
{
|
{
|
||||||
|
|
||||||
const std::size_t requestedBatchSize = 128;
|
const std::size_t requestedBatchSize = 128;
|
||||||
@@ -125,12 +125,12 @@ class TestClient
|
|||||||
std::size_t res = 0;
|
std::size_t res = 0;
|
||||||
|
|
||||||
|
|
||||||
while ((res = getArtists(artists, offset, requestedBatchSize) ) > 0)
|
while ((res = getArtists(artists, genreIds, offset, requestedBatchSize) ) > 0)
|
||||||
offset += res;
|
offset += res;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t getArtists(std::vector<ArtistInfo>& artists, std::size_t offset, std::size_t size)
|
std::size_t getArtists(std::vector<ArtistInfo>& artists, const std::vector<uint64_t>& genreIds, std::size_t offset, std::size_t size)
|
||||||
{
|
{
|
||||||
std::size_t nbArtists = 0;
|
std::size_t nbArtists = 0;
|
||||||
|
|
||||||
@@ -142,6 +142,8 @@ class TestClient
|
|||||||
request.mutable_audio_collection_request()->set_type( Remote::AudioCollectionRequest_Type_TypeGetArtistList);
|
request.mutable_audio_collection_request()->set_type( Remote::AudioCollectionRequest_Type_TypeGetArtistList);
|
||||||
request.mutable_audio_collection_request()->mutable_get_artists()->mutable_batch_parameter()->set_size(size);
|
request.mutable_audio_collection_request()->mutable_get_artists()->mutable_batch_parameter()->set_size(size);
|
||||||
request.mutable_audio_collection_request()->mutable_get_artists()->mutable_batch_parameter()->set_offset(offset);
|
request.mutable_audio_collection_request()->mutable_get_artists()->mutable_batch_parameter()->set_offset(offset);
|
||||||
|
BOOST_FOREACH(uint64_t genreId, genreIds)
|
||||||
|
request.mutable_audio_collection_request()->mutable_get_artists()->add_genre_id(genreId);
|
||||||
|
|
||||||
sendMsg(request);
|
sendMsg(request);
|
||||||
|
|
||||||
@@ -719,7 +721,7 @@ class TestClient
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
bool extendedTests = false;
|
bool extendedTests = true;
|
||||||
bool writeCovers = false;
|
bool writeCovers = false;
|
||||||
|
|
||||||
std::cout << "Running test... extendedTests = " << std::boolalpha << extendedTests << std::endl;
|
std::cout << "Running test... extendedTests = " << std::boolalpha << extendedTests << std::endl;
|
||||||
@@ -733,18 +735,21 @@ int main()
|
|||||||
throw std::runtime_error("login failed!");
|
throw std::runtime_error("login failed!");
|
||||||
|
|
||||||
// **** REVISION ***
|
// **** REVISION ***
|
||||||
|
std::cout << "Getting revision..." << std::endl;
|
||||||
std::string rev = client.getRevision();
|
std::string rev = client.getRevision();
|
||||||
std::cout << "Revision '" << rev << "'" << std::endl;
|
std::cout << "Revision '" << rev << "'" << std::endl;
|
||||||
|
|
||||||
// ****** Artists *********
|
// ****** Artists *********
|
||||||
|
std::cout << "Getting artists..." << std::endl;
|
||||||
std::vector<ArtistInfo> artists;
|
std::vector<ArtistInfo> artists;
|
||||||
client.getArtists(artists);
|
client.getArtists(artists, std::vector<uint64_t>());
|
||||||
|
|
||||||
std::cout << "Got " << artists.size() << " artists!" << std::endl;
|
std::cout << "Got " << artists.size() << " artists!" << std::endl;
|
||||||
BOOST_FOREACH(const ArtistInfo& artist, artists)
|
BOOST_FOREACH(const ArtistInfo& artist, artists)
|
||||||
std::cout << "Artist: '" << artist << "'" << std::endl;
|
std::cout << "Artist: '" << artist << "'" << std::endl;
|
||||||
|
|
||||||
// ***** Genres *********
|
// ***** Genres *********
|
||||||
|
std::cout << "Getting genres..." << std::endl;
|
||||||
std::vector<GenreInfo> genres;
|
std::vector<GenreInfo> genres;
|
||||||
client.getGenres(genres);
|
client.getGenres(genres);
|
||||||
|
|
||||||
@@ -753,31 +758,47 @@ int main()
|
|||||||
std::cout << "Genre: '" << genre << "'" << std::endl;
|
std::cout << "Genre: '" << genre << "'" << std::endl;
|
||||||
|
|
||||||
// **** Releases ******
|
// **** Releases ******
|
||||||
|
std::cout << "Getting releases..." << std::endl;
|
||||||
std::vector<ReleaseInfo> releases;
|
std::vector<ReleaseInfo> releases;
|
||||||
client.getReleases(releases, std::vector<uint64_t>());
|
client.getReleases(releases, std::vector<uint64_t>());
|
||||||
BOOST_FOREACH(const ReleaseInfo& release, releases)
|
BOOST_FOREACH(const ReleaseInfo& release, releases)
|
||||||
std::cout << "Release: '" << release << "'" << std::endl;
|
std::cout << "Release: '" << release << "'" << std::endl;
|
||||||
|
|
||||||
// **** Tracks ******
|
// **** Tracks ******
|
||||||
|
std::cout << "Getting tracks..." << std::endl;
|
||||||
std::vector<TrackInfo> tracks;
|
std::vector<TrackInfo> tracks;
|
||||||
client.getTracks(tracks, std::vector<uint64_t>(), std::vector<uint64_t>(), std::vector<uint64_t>());
|
client.getTracks(tracks, std::vector<uint64_t>(), std::vector<uint64_t>(), std::vector<uint64_t>());
|
||||||
BOOST_FOREACH(const TrackInfo& track, tracks)
|
BOOST_FOREACH(const TrackInfo& track, tracks)
|
||||||
std::cout << "Track: '" << track << "'" << std::endl;
|
std::cout << "Track: '" << track << "'" << std::endl;
|
||||||
|
|
||||||
// Caution: long test!
|
// Caution: long test!
|
||||||
/* if (extendedTests)
|
if (extendedTests)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
std::cout << "Getting artist for each genre..." << std::endl;
|
||||||
|
// Get the artists for each genre
|
||||||
|
BOOST_FOREACH(const GenreInfo& genre, genres)
|
||||||
|
{
|
||||||
|
std::cout << "Getting artists genre '" << genre.name << "'" << std::endl;
|
||||||
|
std::vector<ArtistInfo> artists;
|
||||||
|
client.getArtists(artists, std::vector<uint64_t>(1, genre.id));
|
||||||
|
|
||||||
|
BOOST_FOREACH(const ArtistInfo& artist, artists)
|
||||||
|
std::cout << "-> Artist: " << artist << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Getting tracks for each artist..." << std::endl;
|
||||||
|
// Get the tracks for each artist
|
||||||
BOOST_FOREACH(const ArtistInfo& artist, artists)
|
BOOST_FOREACH(const ArtistInfo& artist, artists)
|
||||||
{
|
{
|
||||||
// Get the tracks for each artist
|
|
||||||
std::vector<TrackInfo> tracks;
|
std::vector<TrackInfo> tracks;
|
||||||
client.getTracks(tracks, std::vector<uint64_t>(1, artist.id), std::vector<uint64_t>(), std::vector<uint64_t>());
|
client.getTracks(tracks, std::vector<uint64_t>(1, artist.id), std::vector<uint64_t>(), std::vector<uint64_t>());
|
||||||
|
|
||||||
std::cout << "Artist '" << artist.name << "', nb tracks = " << tracks.size() << std::endl;
|
std::cout << "Artist '" << artist.name << "', nb tracks = " << tracks.size() << std::endl;
|
||||||
BOOST_FOREACH(const TrackInfo& track, tracks)
|
BOOST_FOREACH(const TrackInfo& track, tracks)
|
||||||
std::cout << "Track: '" << track << "'" << std::endl;
|
std::cout << "Artist '" << artist.name << "', track: '" << track << "'" << std::endl;
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
|
|
||||||
// ***** Covers *******
|
// ***** Covers *******
|
||||||
if (extendedTests)
|
if (extendedTests)
|
||||||
|
|||||||
Reference in New Issue
Block a user