const models = require('../models'); import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler } from './types'; export const ListSongsEndpointHandler:EndpointHandler = async (req: any, res: any) => { if (!api.checkListSongsRequest(req)) { const e:EndpointError = { internalMessage: 'Invalid ListSongs request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } await models.Song.findAll({ include: [models.Artist, models.Album] }) .then((songs: any[]) => { console.log(songs); const response: api.ListSongsResponse = songs.map((song: any) => { return { title: song.title, id: song.id, artists: song.Artists.map((artist: any) => { return { name: artist.name, id: artist.id, }; }), albums: song.Albums.map((album: any) => { return { name: album.name, id: album.id, }; }), }; }); res.send(response); }); }