def getArtistData(artist_id): session = db.loadSession() artist = session.query(db.Artist.id, db.Artist.artist_name)\ .select_from(db.Artist)\ .filter(db.Artist.id == artist_id)\ .first() album_list = session.query(db.Album.id, db.Album.album_name, db.Album.album_art, db.Album.year)\ .select_from(db.Artist)\ .join(db.Album)\ .filter(db.Album.artist_id == artist_id)\ .all() genre_list = session.query(distinct(db.Genre.id), db.Genre.genre_name)\ .select_from(db.Artist)\ .join(db.Album)\ .join(db.AlbumGenres)\ .join(db.Genre)\ .filter(db.Album.artist_id == artist_id)\ .all() session.close() keys = ('id', 'artist_name', ('albums', ('album_id', 'album_name', 'album_art', 'year')), ('genres', ('genre_id', 'genre_name'))) result_tuple = artist + (album_list, genre_list) results_dict = map_keys(keys, result_tuple) return results_dict
def get(self): """ return JSON of all artists """ session = db.loadSession() rowCount = session.query(db.Artist).count() session.close() return jsonify(artists = [getArtistData(artist_id) for artist_id in range(rowCount)])
def get(self): """ return JSON of all tracks """ session = db.loadSession() rowCount = session.query(db.Track).count() session.close() return jsonify(tracks = [getTrackData(track_id) for track_id in range(rowCount)])
def get(self): """ return JSON of all genres """ session = db.loadSession() rowCount = session.query(db.Genre).count() session.close() return jsonify(genres = [getGenreData(genre_id) for genre_id in range(rowCount)])
def getAlbumData(album_id): session = db.loadSession() album = ( session.query( db.Album.id, db.Album.artist_id, db.Album.album_name, db.Artist.artist_name, db.Album.album_art, db.Album.type, db.Album.year, db.Album.bc_link, ) .select_from(db.Album) .join(db.Artist) .filter(db.Album.id == album_id) .first() ) track_list = ( session.query(db.Track.id, db.Track.name).select_from(db.Track).filter(db.Track.album_id == album_id).all() ) genre_list = ( session.query(db.Genre.genre_name) .select_from(db.Album) .join(db.AlbumGenres) .join(db.Genre) .filter(db.Album.id == album_id) .all() ) session.close() keys = ( "id", "artist_id", "album_name", "artist_name", "album_art", "type", "year", "bc_link", ("genres", ("genre_name",)), ("tracks", ("track_id", "track_name")), ) result_tuple = album + (genre_list, track_list) results_dict = map_keys(keys, result_tuple) return results_dict
def search(): query = request.args.get('query') # Empty query, return all albums. if not query: return getAllAlbums() session = db.loadSession() album_ids = session.query(distinct(db.Album.id))\ .select_from(db.Artist)\ .join(db.Album)\ .join(db.AlbumGenres)\ .join(db.Genre)\ .filter(or_(db.Album.album_name.contains(query), db.Artist.artist_name.contains(query), db.Genre.genre_name.contains(query)))\ .all() session.close() album_ids = map(lambda x: x[0], album_ids) # Return all albums whose artist's name, genres, or album name contains the query. return jsonify(albums = [getAlbumData(album_id) for album_id in album_ids])
def getTrackData(track_id): session = db.loadSession() track = session.query(db.Track.id, db.Track.name, db.Track.duration, db.Track.album_id, db.Album.album_name, db.Artist.id, db.Artist.artist_name)\ .select_from(db.Track)\ .join(db.Album)\ .join(db.Artist)\ .filter(db.Track.id == track_id)\ .first() session.close() keys = ('id', 'track_name', 'duration', 'album_id', 'album_name', 'artist_id', 'artist_name') result_dict = map_keys(keys, track) return result_dict
def getAllAlbums(): session = db.loadSession() rowCount = session.query(db.Album).count() session.close() return jsonify(albums=[getAlbumData(album_id) for album_id in range(rowCount)])