|
|
|
@ -16,44 +16,51 @@ 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') |
|
|
|
|
.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 = reqObject.artistIds ? |
|
|
|
|
const artistIdsPromise = newSong.artistIds ? |
|
|
|
|
trx.select('artistId') |
|
|
|
|
.from('songs_artists') |
|
|
|
|
.whereIn('id', reqObject.artistIds) |
|
|
|
|
.whereIn('id', newSong.artistIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['artistId'])) : |
|
|
|
|
(async () => { return [] })(); |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tags.
|
|
|
|
|
const tagIdsPromise = reqObject.tagIds ? |
|
|
|
|
const tagIdsPromise = newSong.tagIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('songs_tags') |
|
|
|
|
.whereIn('id', reqObject.tagIds) |
|
|
|
|
.whereIn('id', newSong.tagIds) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['tagId'])) : |
|
|
|
|
(async () => { return [] })(); |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving albums.
|
|
|
|
|
const albumIdsPromise = reqObject.albumIds ? |
|
|
|
|
const albumIdsPromise = newSong.albumIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('songs_albums') |
|
|
|
|
.whereIn('id', reqObject.albumIds) |
|
|
|
|
.whereIn('id', newSong.albumIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['albumId'])) : |
|
|
|
|
(async () => { return [] })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving the song itself.
|
|
|
|
|
const songPromise = trx.select('id') |
|
|
|
|
.from('songs') |
|
|
|
|
.where({ id: req.params.id }) |
|
|
|
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined) |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
var [song, artists, tags, albums] = |
|
|
|
|
await Promise.all([songPromise, artistIdsPromise, tagIdsPromise, albumIdsPromise]);; |
|
|
|
|
var [artists, tags, albums] = |
|
|
|
|
await Promise.all([artistIdsPromise, tagIdsPromise, albumIdsPromise]);; |
|
|
|
|
|
|
|
|
|
// Check that we found all objects we need.
|
|
|
|
|
if ((reqObject.artistIds && artists.length !== reqObject.artistIds.length) || |
|
|
|
|
(reqObject.tagIds && tags.length !== reqObject.tagIds.length) || |
|
|
|
|
(reqObject.albumIds && albums.length !== reqObject.albumIds.length) || |
|
|
|
|
if ((newSong.artistIds && artists.length !== newSong.artistIds.length) || |
|
|
|
|
(newSong.tagIds && tags.length !== newSong.tagIds.length) || |
|
|
|
|
(newSong.albumIds && albums.length !== newSong.albumIds.length) || |
|
|
|
|
!song) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Not all albums and/or artists and/or tags exist for ModifySong request: ' + JSON.stringify(req.body), |
|
|
|
@ -66,34 +73,34 @@ export const ModifySongEndpointHandler: EndpointHandler = async (req: any, res: |
|
|
|
|
const modifySongPromise = trx('songs') |
|
|
|
|
.where({ 'id': req.params.id }) |
|
|
|
|
.update({ |
|
|
|
|
title: reqObject.title, |
|
|
|
|
storeLinks: JSON.stringify(reqObject.storeLinks || []), |
|
|
|
|
title: newSong.title, |
|
|
|
|
storeLinks: JSON.stringify(newSong.storeLinks || []), |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Remove unlinked artists.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedArtists = trx('artists_songs') |
|
|
|
|
const removeUnlinkedArtists = artists ? trx('songs_artists') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.whereNotIn('artistId', reqObject.artistIds || []) |
|
|
|
|
.delete(); |
|
|
|
|
.whereNotIn('artistId', newSong.artistIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Remove unlinked tags.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedTags = trx('songs_tags') |
|
|
|
|
const removeUnlinkedTags = tags ? trx('songs_tags') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.whereNotIn('tagId', reqObject.tagIds || []) |
|
|
|
|
.delete(); |
|
|
|
|
.whereNotIn('tagId', newSong.tagIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Remove unlinked albums.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedAlbums = trx('songs_albums') |
|
|
|
|
const removeUnlinkedAlbums = albums ? trx('songs_albums') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.whereNotIn('albumId', reqObject.albumIds || []) |
|
|
|
|
.delete(); |
|
|
|
|
.whereNotIn('albumId', newSong.albumIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Link new artists.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const addArtists = trx('artists_songs') |
|
|
|
|
const addArtists = artists ? trx('songs_artists') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['artistId'])) |
|
|
|
|
.then((doneArtistIds: number[]) => { |
|
|
|
@ -111,14 +118,14 @@ export const ModifySongEndpointHandler: EndpointHandler = async (req: any, res: |
|
|
|
|
// Link them
|
|
|
|
|
return Promise.all( |
|
|
|
|
insertObjects.map((obj: any) => |
|
|
|
|
trx('artists_songs').insert(obj) |
|
|
|
|
trx('songs_artists').insert(obj) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
}) |
|
|
|
|
}) : undefined; |
|
|
|
|
|
|
|
|
|
// Link new tags.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const addTags = trx('songs_tags') |
|
|
|
|
const addTags = tags ? trx('songs_tags') |
|
|
|
|
.where({ 'songId': req.params.id }) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['tagId'])) |
|
|
|
|
.then((doneTagIds: number[]) => { |
|
|
|
@ -139,11 +146,11 @@ export const ModifySongEndpointHandler: EndpointHandler = async (req: any, res: |
|
|
|
|
trx('songs_tags').insert(obj) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
}) |
|
|
|
|
}) : undefined; |
|
|
|
|
|
|
|
|
|
// Link new albums.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const addAlbums = trx('songs_albums') |
|
|
|
|
const addAlbums = albums ? trx('songs_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['albumId'])) |
|
|
|
|
.then((doneAlbumIds: number[]) => { |
|
|
|
@ -164,7 +171,7 @@ export const ModifySongEndpointHandler: EndpointHandler = async (req: any, res: |
|
|
|
|
trx('songs_albums').insert(obj) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
}) |
|
|
|
|
}) : undefined; |
|
|
|
|
|
|
|
|
|
// Wait for all operations to finish.
|
|
|
|
|
await Promise.all([ |
|
|
|
|