import {listArtists, listAlbums, listAlbumsByArtist,
        listSongs, listSongsByAlbum, } from '../queries/list-tracks';

export const getLibrary = {
    url: '/library',
    generateHandler: function(tracks) {
        return function(req, res) {
            res.send(JSON.stringify(tracks));
        };
    }
}

export const getArtists = {
    url: '/artists',
    generateHandler: function(db) {
        return function(req, res) {
            listArtists(db).then(results => {
                res.send(JSON.stringify(results));
            });
        };
    }
}

export const getAlbums = {
    url: '/albums',
    generateHandler: function(db) {
        return function(req, res) {
            if (req.param.artist) {
                listAlbumsByArtist(db, req.param.artist).then(results => {
                    res.send(JSON.stringify(results));
                });
            } else {
                listAlbums(db).then(results => {
                    res.send(JSON.stringify(results));
                });
            }
        };
    }
}

export const getSongs = {
    url: '/songs',
    generateHandler: function(db) {
        return function(req, res) {
            if (req.param.album) {
                listSongsByAlbum(db, req.param.album).then(results => {
                    res.send(JSON.stringify(results));
                });
            } else if (req.param.artist) {
                listSongsByArtist(db, req.param.artist).then(results => {
                    res.send(JSON.stringify(results));
                });
            } else {
                listSongs(db).then(results => {
                    res.send(JSON.stringify(results));
                });
            }
        }
    }
}

import {listArtists, listAlbums, listAlbumsByArtist,
        listSongs, listSongsByAlbum, } from '../queries/list-tracks';

export const getLibrary = {
    url: '/library',
    generateHandler: function(tracks) {
        return function(req, res) {
            res.send(JSON.stringify(tracks));
        };
    }
}

export const getArtists = {
    url: '/artists',
    generateHandler: function(db) {
        return function(req, res) {
            listArtists(db).then(results => {
                res.send(JSON.stringify(results));
            });
        };
    }
}

export const getAlbums = {
    url: '/albums',
    generateHandler: function(db) {
        return function(req, res) {
            if (req.query.artist) {
                listAlbumsByArtist(db, req.query.artist).then(results => {
                    res.send(JSON.stringify(results));
                });
            } else {
                listAlbums(db).then(results => {
                    res.send(JSON.stringify(results));
                });
            }
        };
    }
}

export const getSongs = {
    url: '/songs',
    generateHandler: function(db) {
        return function(req, res) {
            if (req.query.album) {
                listSongsByAlbum(db, req.query.album).then(results => {
                    res.send(JSON.stringify(results));
                });
            } else if (req.query.artist) {
                listSongsByArtist(db, req.query.artist).then(results => {
                    res.send(JSON.stringify(results));
                });
            } else {
                listSongs(db).then(results => {
                    res.send(JSON.stringify(results));
                });
            }
        }
    }
}

Fix library routes