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.
62 lines
2.1 KiB
62 lines
2.1 KiB
const models = require('../models'); |
|
import * as api from '../../client/src/api'; |
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
|
const { Op } = require("sequelize"); |
|
|
|
export const ModifyAlbumEndpointHandler: EndpointHandler = async (req: any, res: any) => { |
|
if (!api.checkModifyAlbumRequest(req)) { |
|
const e: EndpointError = { |
|
internalMessage: 'Invalid ModifyAlbum request: ' + JSON.stringify(req.body), |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
const reqObject: api.ModifyAlbumRequest = 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 |
|
} |
|
} |
|
}); |
|
|
|
// Start retrieving the album to modify. |
|
var albumInstancePromise = models.Album.findOne({ |
|
where: { |
|
id: req.params.id |
|
} |
|
}); |
|
|
|
// Upon finish retrieving artists and albums, modify the album. |
|
await Promise.all([artistInstancesPromise, tagInstancesPromise, albumInstancePromise]) |
|
.then(async (values: any) => { |
|
var [artists, tags, album] = values; |
|
if (!album) { |
|
const e: EndpointError = { |
|
internalMessage: 'There is no album with id ' + req.params.id + '.', |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
if (reqObject.artistIds) { album.setArtists(artists) }; |
|
if (reqObject.tagIds) { album.setTags(tags) }; |
|
if (reqObject.name) { album.name = reqObject.name }; |
|
if (reqObject.storeLinks) { album.setStoreIds(reqObject.storeLinks) }; |
|
await album.save(); |
|
}) |
|
.then(() => { |
|
res.status(200).send({}); |
|
}) |
|
.catch(catchUnhandledErrors); |
|
} |