const models = require('../models'); import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; export const SongDetailsEndpointHandler: EndpointHandler = async (req: any, res: any) => { if (!api.checkSongDetailsRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid SongDetails request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } await models.Song.findAll({ include: [models.Artist, models.Album], where: { id: req.params.id } }) .then((songs: any[]) => { if (songs.length != 1) { const e: EndpointError = { internalMessage: 'There is no song with id ' + req.params.id + '.', httpStatus: 400 }; throw e; } let song = songs[0]; const response: api.SongDetailsResponse = { title: song.title, artistIds: song.Artists.map((artist:any) => artist.id), albumIds: song.Albums.map((album:any) => album.id), storeLinks: song.storeLinks, } res.send(response); }) .catch(catchUnhandledErrors); }