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; } try { const songs = await models.Song.findAll({ include: [models.Artist, models.Album, models.Tag], where: { id: req.params.id } }); 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), tagIds: song.Tags.map((tag: any) => tag.id), storeLinks: song.storeLinks, } await res.send(response); } catch (e) { catchUnhandledErrors(e); } }