const models = require('../models'); import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; export const AlbumDetailsEndpointHandler: EndpointHandler = async (req: any, res: any) => { if (!api.checkAlbumDetailsRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid AlbumDetails request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } try { const albums = await models.Album.findAll({ include: [models.Artist, models.Tag, models.Song], where: { id: req.params.id } }); if (albums.length != 1) { const e: EndpointError = { internalMessage: 'There is no album with id ' + req.params.id + '.', httpStatus: 400 }; throw e; } let album = albums[0]; const response: api.AlbumDetailsResponse = { name: album.name, artistIds: album.Artists.map((artist: any) => artist.id), tagIds: album.Tags.map((tag: any) => tag.id), songIds: album.Songs.map((song: any) => song.id), storeLinks: album.storeLinks, } await res.send(response); } catch (e) { catchUnhandledErrors(e); } }