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.
 
 
 
 

63 lines
2.0 KiB

const models = require('../models');
import * as api from '../../client/src/api';
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types';
const { Op } = require("sequelize");
export const CreateAlbumEndpointHandler: EndpointHandler = async (req: any, res: any) => {
if (!api.checkCreateAlbumRequest(req)) {
const e: EndpointError = {
internalMessage: 'Invalid CreateAlbum request: ' + JSON.stringify(req.body),
httpStatus: 400
};
throw e;
}
const reqObject: api.CreateAlbumRequest = req.body;
// Start retrieving the artist instances to link the album to.
var artistInstancesPromise = reqObject.artistIds && models.Artist.findAll({
where: {
id: {
[Op.in]: reqObject.artistIds
}
}
});
// Start retrieving the tag instances to link the album to.
var tagInstancesPromise = reqObject.tagIds && models.Tag.findAll({
where: {
id: {
[Op.in]: reqObject.tagIds
}
}
});
// Upon finish retrieving artists and tags, create the album and associate it.
await Promise.all([artistInstancesPromise, tagInstancesPromise])
.then((values: any) => {
var [artists, tags] = values;
if ((reqObject.artistIds && artists.length !== reqObject.artistIds.length) ||
(reqObject.tagIds && tags.length !== reqObject.tagIds.length)) {
const e: EndpointError = {
internalMessage: 'Not all albums and/or artists and/or tags exist for CreateAlbum request: ' + JSON.stringify(req.body),
httpStatus: 400
};
throw e;
}
var album = models.Album.build({
name: reqObject.name,
storeLinks: reqObject.storeLinks || [],
});
artists && album.addArtists(artists);
tags && album.addTags(tags);
return album.save();
})
.then((album: any) => {
const responseObject: api.CreateSongResponse = {
id: album.id
};
res.status(200).send(responseObject);
})
.catch(catchUnhandledErrors);
}