You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

65 lines
2.2 KiB

const models = require('../models');
import * as api from '../../client/src/api';
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types';
const { Op } = require("sequelize");
export const ModifySongEndpointHandler: EndpointHandler = async (req: any, res: any) => {
if (!api.checkModifySongRequest(req)) {
const e: EndpointError = {
internalMessage: 'Invalid ModifySong request: ' + JSON.stringify(req.body),
httpStatus: 400
};
throw e;
}
const reqObject: api.ModifySongRequest = req.body;
// Start retrieving the artist instances to link the song to.
var artistInstancesPromise = reqObject.artistIds && models.Artist.findAll({
where: {
id: {
[Op.in]: reqObject.artistIds
}
}
});
// Start retrieving the album instances to link the song to.
var albumInstancesPromise = reqObject.albumIds && models.Album.findAll({
where: {
id: {
[Op.in]: reqObject.albumIds
}
}
});
// Start retrieving the tag instances to link the song to.
var tagInstancesPromise = reqObject.tagIds && models.Tag.findAll({
where: {
id: {
[Op.in]: reqObject.tagIds
}
}
});
// Start retrieving the song to modify.
var songInstancePromise = models.Song.findAll({
where: {
id: req.params.id
}
});
// Upon finish retrieving artists and albums, modify the song.
await Promise.all([artistInstancesPromise, albumInstancesPromise, tagInstancesPromise, songInstancePromise])
.then(async (values: any) => {
var [artists, albums, tags, song] = values;
if (reqObject.artistIds) { song.setArtists(artists) };
if (reqObject.albumIds) { song.setAlbums(albums) };
if (reqObject.tagIds) { song.setTags(tags) };
if (reqObject.title) { song.setTitle(reqObject.title) };
if (reqObject.storeLinks) { song.setStoreIds(reqObject.storeLinks) };
await song.save();
})
.then(() => {
res.status(200).send({});
})
.catch(catchUnhandledErrors);
}