|
|
|
@ -17,50 +17,43 @@ export const ModifySongEndpointHandler: EndpointHandler = async (req: any, res: |
|
|
|
|
await knex.transaction(async (trx) => { |
|
|
|
|
try { |
|
|
|
|
// Retrieve the song to be modified itself.
|
|
|
|
|
const song = await trx.select('id') |
|
|
|
|
const songPromise = trx.select('id') |
|
|
|
|
.from('songs') |
|
|
|
|
.where({ id: req.params.id }) |
|
|
|
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined) |
|
|
|
|
|
|
|
|
|
// Construct the new object from the original plus the
|
|
|
|
|
// changes included in the PUT.
|
|
|
|
|
const newSong = { |
|
|
|
|
...song, |
|
|
|
|
...reqObject, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Start retrieving artists.
|
|
|
|
|
const artistIdsPromise = newSong.artistIds ? |
|
|
|
|
const artistIdsPromise = reqObject.artistIds ? |
|
|
|
|
trx.select('artistId') |
|
|
|
|
.from('songs_artists') |
|
|
|
|
.whereIn('id', newSong.artistIds) |
|
|
|
|
.whereIn('id', reqObject.artistIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['artistId'])) : |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tags.
|
|
|
|
|
const tagIdsPromise = newSong.tagIds ? |
|
|
|
|
const tagIdsPromise = reqObject.tagIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('songs_tags') |
|
|
|
|
.whereIn('id', newSong.tagIds) |
|
|
|
|
.whereIn('id', reqObject.tagIds) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['tagId'])) : |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving albums.
|
|
|
|
|
const albumIdsPromise = newSong.albumIds ? |
|
|
|
|
const albumIdsPromise = reqObject.albumIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('songs_albums') |
|
|
|
|
.whereIn('id', newSong.albumIds) |
|
|
|
|
.whereIn('id', reqObject.albumIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['albumId'])) : |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
var [artists, tags, albums] = |
|
|
|
|
await Promise.all([artistIdsPromise, tagIdsPromise, albumIdsPromise]);; |
|
|
|
|
var [song, artists, tags, albums] = |
|
|
|
|
await Promise.all([songPromise, artistIdsPromise, tagIdsPromise, albumIdsPromise]);; |
|
|
|
|
|
|
|
|
|
// Check that we found all objects we need.
|
|
|
|
|
if ((newSong.artistIds && artists.length !== newSong.artistIds.length) || |
|
|
|
|
(newSong.tagIds && tags.length !== newSong.tagIds.length) || |
|
|
|
|
(newSong.albumIds && albums.length !== newSong.albumIds.length) || |
|
|
|
|
if ((reqObject.artistIds && artists.length !== reqObject.artistIds.length) || |
|
|
|
|
(reqObject.tagIds && tags.length !== reqObject.tagIds.length) || |
|
|
|
|
(reqObject.albumIds && albums.length !== reqObject.albumIds.length) || |
|
|
|
|
!song) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Not all albums and/or artists and/or tags exist for ModifySong request: ' + JSON.stringify(req.body), |
|
|
|
@ -70,32 +63,32 @@ export const ModifySongEndpointHandler: EndpointHandler = async (req: any, res: |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Modify the song.
|
|
|
|
|
var update: any = {}; |
|
|
|
|
if ("title" in reqObject) { update["title"] = reqObject.title; } |
|
|
|
|
if ("storeLinks" in reqObject) { update["storeLinks"] = JSON.stringify(reqObject.storeLinks || []); } |
|
|
|
|
const modifySongPromise = trx('songs') |
|
|
|
|
.where({ 'id': req.params.id }) |
|
|
|
|
.update({ |
|
|
|
|
title: newSong.title, |
|
|
|
|
storeLinks: JSON.stringify(newSong.storeLinks || []), |
|
|
|
|
}) |
|
|
|
|
.update(update) |
|
|
|
|
|
|
|
|
|
// Remove unlinked artists.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedArtists = artists ? trx('songs_artists') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.whereNotIn('artistId', newSong.artistIds || []) |
|
|
|
|
.whereNotIn('artistId', reqObject.artistIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Remove unlinked tags.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedTags = tags ? trx('songs_tags') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.whereNotIn('tagId', newSong.tagIds || []) |
|
|
|
|
.whereNotIn('tagId', reqObject.tagIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Remove unlinked albums.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedAlbums = albums ? trx('songs_albums') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.whereNotIn('albumId', newSong.albumIds || []) |
|
|
|
|
.whereNotIn('albumId', reqObject.albumIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Link new artists.
|
|
|
|
|