const models = require('../models'); import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; export const ModifyArtistEndpointHandler: EndpointHandler = async (req: any, res: any) => { if (!api.checkModifyArtistRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid ModifyArtist request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject:api.ModifyArtistRequest = req.body; await models.Artist.findAll({ where: { id: req.params.id } }) .then(async (artists: any[]) => { if (artists.length != 1) { const e: EndpointError = { internalMessage: 'There is no artist with id ' + req.params.id + '.', httpStatus: 400 }; throw e; } let artist = artists[0]; artist.name = reqObject.name; if(reqObject.storeLinks) { artist.setStoreLinks(reqObject.storeLinks) }; await artist.save(); }) .then(() => { res.status(200).send({}); }) .catch(catchUnhandledErrors); }