|
|
|
@ -4,299 +4,311 @@ import Knex from 'knex'; |
|
|
|
|
import asJson from '../lib/asJson'; |
|
|
|
|
|
|
|
|
|
export const GetAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
|
|
|
if (!api.checkAlbumDetailsRequest(req)) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Invalid GetAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
}; |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
if (!api.checkAlbumDetailsRequest(req)) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Invalid GetAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
}; |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const { id: userId } = req.user; |
|
|
|
|
const { id: userId } = req.user; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
// Start transfers for songs, tags and artists.
|
|
|
|
|
// Also request the album itself.
|
|
|
|
|
const tagIdsPromise = knex.select('tagId') |
|
|
|
|
.from('albums_tags') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((tags: any) => { |
|
|
|
|
return tags.map((tag: any) => tag['tagId']) |
|
|
|
|
}); |
|
|
|
|
const songIdsPromise = knex.select('songId') |
|
|
|
|
.from('songs_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((songs: any) => { |
|
|
|
|
return songs.map((song: any) => song['songId']) |
|
|
|
|
}); |
|
|
|
|
const artistIdsPromise = knex.select('artistId') |
|
|
|
|
.from('artists_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((artists: any) => { |
|
|
|
|
return artists.map((artist: any) => artist['artistId']) |
|
|
|
|
}); |
|
|
|
|
const albumPromise = knex.select('name', 'storeLinks') |
|
|
|
|
.from('albums') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.where({ id: req.params.id }) |
|
|
|
|
.then((albums: any) => albums[0]); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
const [album, tags, songs, artists] = |
|
|
|
|
await Promise.all([albumPromise, tagIdsPromise, songIdsPromise, artistIdsPromise]); |
|
|
|
|
|
|
|
|
|
// Respond to the request.
|
|
|
|
|
if (album) { |
|
|
|
|
const response: api.AlbumDetailsResponse = { |
|
|
|
|
name: album['name'], |
|
|
|
|
artistIds: artists, |
|
|
|
|
tagIds: tags, |
|
|
|
|
songIds: songs, |
|
|
|
|
storeLinks: asJson(album['storeLinks']), |
|
|
|
|
}; |
|
|
|
|
await res.send(response); |
|
|
|
|
} else { |
|
|
|
|
await res.status(404).send({}); |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
try { |
|
|
|
|
// Start transfers for songs, tags and artists.
|
|
|
|
|
// Also request the album itself.
|
|
|
|
|
const tagsPromise: Promise<api.TagDetailsResponseWithId[]> = knex.select('tagId') |
|
|
|
|
.from('albums_tags') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((tags: any) => { |
|
|
|
|
return tags.map((tag: any) => tag['tagId']) |
|
|
|
|
}) |
|
|
|
|
.then((ids: number[]) => knex.select(['id', 'name', 'parentId']) |
|
|
|
|
.from('tags') |
|
|
|
|
.whereIn('id', ids)); |
|
|
|
|
|
|
|
|
|
const songsPromise: Promise<api.SongDetailsResponseWithId[]> = knex.select('songId') |
|
|
|
|
.from('songs_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((songs: any) => { |
|
|
|
|
return songs.map((song: any) => song['songId']) |
|
|
|
|
}) |
|
|
|
|
.then((ids: number[]) => knex.select(['id', 'title', 'storeLinks']) |
|
|
|
|
.from('songs') |
|
|
|
|
.whereIn('id', ids)); |
|
|
|
|
|
|
|
|
|
const artistsPromise = knex.select('artistId') |
|
|
|
|
.from('artists_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((artists: any) => { |
|
|
|
|
return artists.map((artist: any) => artist['artistId']) |
|
|
|
|
}) |
|
|
|
|
.then((ids: number[]) => knex.select(['id', 'name', 'storeLinks']) |
|
|
|
|
.from('artists') |
|
|
|
|
.whereIn('id', ids)); |
|
|
|
|
|
|
|
|
|
const albumPromise = knex.select('name', 'storeLinks') |
|
|
|
|
.from('albums') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.where({ id: req.params.id }) |
|
|
|
|
.then((albums: any) => albums[0]); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
const [album, tags, songs, artists] = |
|
|
|
|
await Promise.all([albumPromise, tagsPromise, songsPromise, artistsPromise]); |
|
|
|
|
|
|
|
|
|
// Respond to the request.
|
|
|
|
|
if (album) { |
|
|
|
|
const response: api.AlbumDetailsResponse = { |
|
|
|
|
name: album['name'], |
|
|
|
|
artists: artists, |
|
|
|
|
tags: tags, |
|
|
|
|
songs: songs, |
|
|
|
|
storeLinks: asJson(album['storeLinks']), |
|
|
|
|
}; |
|
|
|
|
await res.send(response); |
|
|
|
|
} else { |
|
|
|
|
await res.status(404).send({}); |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
catchUnhandledErrors(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const PostAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
|
|
|
if (!api.checkCreateAlbumRequest(req)) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Invalid PostAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
}; |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
const reqObject: api.CreateAlbumRequest = req.body; |
|
|
|
|
const { id: userId } = req.user; |
|
|
|
|
|
|
|
|
|
console.log("User ", userId, ": Post Album ", reqObject); |
|
|
|
|
|
|
|
|
|
await knex.transaction(async (trx) => { |
|
|
|
|
try { |
|
|
|
|
// Start retrieving artists.
|
|
|
|
|
const artistIdsPromise = reqObject.artistIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('artists') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.whereIn('id', reqObject.artistIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['id'])) : |
|
|
|
|
(async () => { return [] })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tags.
|
|
|
|
|
const tagIdsPromise = reqObject.tagIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('tags') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.whereIn('id', reqObject.tagIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['id'])) : |
|
|
|
|
(async () => { return [] })(); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
var [artists, tags] = await Promise.all([artistIdsPromise, tagIdsPromise]);; |
|
|
|
|
|
|
|
|
|
// Check that we found all artists and tags we need.
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create the album.
|
|
|
|
|
const albumId = (await trx('albums') |
|
|
|
|
.insert({ |
|
|
|
|
name: reqObject.name, |
|
|
|
|
storeLinks: JSON.stringify(reqObject.storeLinks || []), |
|
|
|
|
user: userId, |
|
|
|
|
}) |
|
|
|
|
.returning('id') // Needed for Postgres
|
|
|
|
|
)[0]; |
|
|
|
|
|
|
|
|
|
// Link the artists via the linking table.
|
|
|
|
|
if (artists && artists.length) { |
|
|
|
|
await trx('artists_albums').insert( |
|
|
|
|
artists.map((artistId: number) => { |
|
|
|
|
return { |
|
|
|
|
artistId: artistId, |
|
|
|
|
albumId: albumId, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Link the tags via the linking table.
|
|
|
|
|
if (tags && tags.length) { |
|
|
|
|
await trx('albums_tags').insert( |
|
|
|
|
tags.map((tagId: number) => { |
|
|
|
|
return { |
|
|
|
|
albumId: albumId, |
|
|
|
|
tagId: tagId, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Respond to the request.
|
|
|
|
|
const responseObject: api.CreateSongResponse = { |
|
|
|
|
id: albumId |
|
|
|
|
if (!api.checkCreateAlbumRequest(req)) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Invalid PostAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
}; |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
const reqObject: api.CreateAlbumRequest = req.body; |
|
|
|
|
const { id: userId } = req.user; |
|
|
|
|
|
|
|
|
|
console.log("User ", userId, ": Post Album ", reqObject); |
|
|
|
|
|
|
|
|
|
await knex.transaction(async (trx) => { |
|
|
|
|
try { |
|
|
|
|
// Start retrieving artists.
|
|
|
|
|
const artistIdsPromise = reqObject.artistIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('artists') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.whereIn('id', reqObject.artistIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['id'])) : |
|
|
|
|
(async () => { return [] })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tags.
|
|
|
|
|
const tagIdsPromise = reqObject.tagIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('tags') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.whereIn('id', reqObject.tagIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['id'])) : |
|
|
|
|
(async () => { return [] })(); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
var [artists, tags] = await Promise.all([artistIdsPromise, tagIdsPromise]);; |
|
|
|
|
|
|
|
|
|
// Check that we found all artists and tags we need.
|
|
|
|
|
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 |
|
|
|
|
}; |
|
|
|
|
res.status(200).send(responseObject); |
|
|
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
catchUnhandledErrors(e); |
|
|
|
|
trx.rollback(); |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create the album.
|
|
|
|
|
const albumId = (await trx('albums') |
|
|
|
|
.insert({ |
|
|
|
|
name: reqObject.name, |
|
|
|
|
storeLinks: JSON.stringify(reqObject.storeLinks || []), |
|
|
|
|
user: userId, |
|
|
|
|
}) |
|
|
|
|
.returning('id') // Needed for Postgres
|
|
|
|
|
)[0]; |
|
|
|
|
|
|
|
|
|
// Link the artists via the linking table.
|
|
|
|
|
if (artists && artists.length) { |
|
|
|
|
await trx('artists_albums').insert( |
|
|
|
|
artists.map((artistId: number) => { |
|
|
|
|
return { |
|
|
|
|
artistId: artistId, |
|
|
|
|
albumId: albumId, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Link the tags via the linking table.
|
|
|
|
|
if (tags && tags.length) { |
|
|
|
|
await trx('albums_tags').insert( |
|
|
|
|
tags.map((tagId: number) => { |
|
|
|
|
return { |
|
|
|
|
albumId: albumId, |
|
|
|
|
tagId: tagId, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Respond to the request.
|
|
|
|
|
const responseObject: api.CreateSongResponse = { |
|
|
|
|
id: albumId |
|
|
|
|
}; |
|
|
|
|
res.status(200).send(responseObject); |
|
|
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
catchUnhandledErrors(e); |
|
|
|
|
trx.rollback(); |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const PutAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
|
|
|
if (!api.checkModifyAlbumRequest(req)) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Invalid PutAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
}; |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
const reqObject: api.ModifyAlbumRequest = req.body; |
|
|
|
|
const { id: userId } = req.user; |
|
|
|
|
|
|
|
|
|
console.log("User ", userId, ": Put Album ", reqObject); |
|
|
|
|
|
|
|
|
|
await knex.transaction(async (trx) => { |
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
// Start retrieving the album itself.
|
|
|
|
|
const albumPromise = trx.select('id') |
|
|
|
|
.from('albums') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.where({ id: req.params.id }) |
|
|
|
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined); |
|
|
|
|
|
|
|
|
|
// Start retrieving artists.
|
|
|
|
|
const artistIdsPromise = reqObject.artistIds ? |
|
|
|
|
trx.select('artistId') |
|
|
|
|
.from('artists_albums') |
|
|
|
|
.whereIn('id', reqObject.artistIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['artistId'])) : |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tags.
|
|
|
|
|
const tagIdsPromise = reqObject.tagIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('albums_tags') |
|
|
|
|
.whereIn('id', reqObject.tagIds) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['tagId'])) : |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
var [album, artists, tags] = await Promise.all([albumPromise, artistIdsPromise, tagIdsPromise]);; |
|
|
|
|
|
|
|
|
|
export const PutAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
|
|
|
if (!api.checkModifyAlbumRequest(req)) { |
|
|
|
|
// Check that we found all objects we need.
|
|
|
|
|
if ((reqObject.artistIds && artists.length !== reqObject.artistIds.length) || |
|
|
|
|
(reqObject.tagIds && tags.length !== reqObject.tagIds.length) || |
|
|
|
|
!album) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Invalid PutAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
internalMessage: 'Not all albums and/or artists and/or tags exist for ModifyAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
}; |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
const reqObject: api.ModifyAlbumRequest = req.body; |
|
|
|
|
const { id: userId } = req.user; |
|
|
|
|
|
|
|
|
|
console.log("User ", userId, ": Put Album ", reqObject); |
|
|
|
|
|
|
|
|
|
await knex.transaction(async (trx) => { |
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
// Start retrieving the album itself.
|
|
|
|
|
const albumPromise = trx.select('id') |
|
|
|
|
.from('albums') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.where({ id: req.params.id }) |
|
|
|
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined); |
|
|
|
|
|
|
|
|
|
// Start retrieving artists.
|
|
|
|
|
const artistIdsPromise = reqObject.artistIds ? |
|
|
|
|
trx.select('artistId') |
|
|
|
|
.from('artists_albums') |
|
|
|
|
.whereIn('id', reqObject.artistIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['artistId'])) : |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tags.
|
|
|
|
|
const tagIdsPromise = reqObject.tagIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('albums_tags') |
|
|
|
|
.whereIn('id', reqObject.tagIds) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['tagId'])) : |
|
|
|
|
(async () => { return undefined })(); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
var [album, artists, tags] = await Promise.all([albumPromise, artistIdsPromise, tagIdsPromise]);; |
|
|
|
|
|
|
|
|
|
// Check that we found all objects we need.
|
|
|
|
|
if ((reqObject.artistIds && artists.length !== reqObject.artistIds.length) || |
|
|
|
|
(reqObject.tagIds && tags.length !== reqObject.tagIds.length) || |
|
|
|
|
!album) { |
|
|
|
|
const e: EndpointError = { |
|
|
|
|
internalMessage: 'Not all albums and/or artists and/or tags exist for ModifyAlbum request: ' + JSON.stringify(req.body), |
|
|
|
|
httpStatus: 400 |
|
|
|
|
}; |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Modify the album.
|
|
|
|
|
var update: any = {}; |
|
|
|
|
if ("name" in reqObject) { update["name"] = reqObject.name; } |
|
|
|
|
if ("storeLinks" in reqObject) { update["storeLinks"] = JSON.stringify(reqObject.storeLinks || []); } |
|
|
|
|
const modifyAlbumPromise = trx('albums') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.where({ 'id': req.params.id }) |
|
|
|
|
.update(update) |
|
|
|
|
|
|
|
|
|
// Remove unlinked artists.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedArtists = artists ? trx('artists_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.whereNotIn('artistId', reqObject.artistIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Remove unlinked tags.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedTags = tags ? trx('albums_tags') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.whereNotIn('tagId', reqObject.tagIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Link new artists.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const addArtists = artists ? trx('artists_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['artistId'])) |
|
|
|
|
.then((doneArtistIds: number[]) => { |
|
|
|
|
// Get the set of artists that are not yet linked
|
|
|
|
|
const toLink = artists.filter((id: number) => { |
|
|
|
|
return !doneArtistIds.includes(id); |
|
|
|
|
}); |
|
|
|
|
const insertObjects = toLink.map((artistId: number) => { |
|
|
|
|
return { |
|
|
|
|
artistId: artistId, |
|
|
|
|
albumId: req.params.id, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Link them
|
|
|
|
|
return Promise.all( |
|
|
|
|
insertObjects.map((obj: any) => |
|
|
|
|
trx('artists_albums').insert(obj) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
}) : undefined; |
|
|
|
|
|
|
|
|
|
// Link new tags.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const addTags = tags ? trx('albums_tags') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['tagId'])) |
|
|
|
|
.then((doneTagIds: number[]) => { |
|
|
|
|
// Get the set of tags that are not yet linked
|
|
|
|
|
const toLink = tags.filter((id: number) => { |
|
|
|
|
return !doneTagIds.includes(id); |
|
|
|
|
}); |
|
|
|
|
const insertObjects = toLink.map((tagId: number) => { |
|
|
|
|
return { |
|
|
|
|
tagId: tagId, |
|
|
|
|
albumId: req.params.id, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Link them
|
|
|
|
|
return Promise.all( |
|
|
|
|
insertObjects.map((obj: any) => |
|
|
|
|
trx('albums_tags').insert(obj) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
}) : undefined; |
|
|
|
|
|
|
|
|
|
// Modify the album.
|
|
|
|
|
var update: any = {}; |
|
|
|
|
if ("name" in reqObject) { update["name"] = reqObject.name; } |
|
|
|
|
if ("storeLinks" in reqObject) { update["storeLinks"] = JSON.stringify(reqObject.storeLinks || []); } |
|
|
|
|
const modifyAlbumPromise = trx('albums') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.where({ 'id': req.params.id }) |
|
|
|
|
.update(update) |
|
|
|
|
|
|
|
|
|
// Remove unlinked artists.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedArtists = artists ? trx('artists_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.whereNotIn('artistId', reqObject.artistIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Remove unlinked tags.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const removeUnlinkedTags = tags ? trx('albums_tags') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.whereNotIn('tagId', reqObject.tagIds || []) |
|
|
|
|
.delete() : undefined; |
|
|
|
|
|
|
|
|
|
// Link new artists.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const addArtists = artists ? trx('artists_albums') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['artistId'])) |
|
|
|
|
.then((doneArtistIds: number[]) => { |
|
|
|
|
// Get the set of artists that are not yet linked
|
|
|
|
|
const toLink = artists.filter((id: number) => { |
|
|
|
|
return !doneArtistIds.includes(id); |
|
|
|
|
}); |
|
|
|
|
const insertObjects = toLink.map((artistId: number) => { |
|
|
|
|
return { |
|
|
|
|
artistId: artistId, |
|
|
|
|
albumId: req.params.id, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Link them
|
|
|
|
|
return Promise.all( |
|
|
|
|
insertObjects.map((obj: any) => |
|
|
|
|
trx('artists_albums').insert(obj) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
}) : undefined; |
|
|
|
|
|
|
|
|
|
// Link new tags.
|
|
|
|
|
// TODO: test this!
|
|
|
|
|
const addTags = tags ? trx('albums_tags') |
|
|
|
|
.where({ 'albumId': req.params.id }) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['tagId'])) |
|
|
|
|
.then((doneTagIds: number[]) => { |
|
|
|
|
// Get the set of tags that are not yet linked
|
|
|
|
|
const toLink = tags.filter((id: number) => { |
|
|
|
|
return !doneTagIds.includes(id); |
|
|
|
|
}); |
|
|
|
|
const insertObjects = toLink.map((tagId: number) => { |
|
|
|
|
return { |
|
|
|
|
tagId: tagId, |
|
|
|
|
albumId: req.params.id, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Link them
|
|
|
|
|
return Promise.all( |
|
|
|
|
insertObjects.map((obj: any) => |
|
|
|
|
trx('albums_tags').insert(obj) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
}) : undefined; |
|
|
|
|
|
|
|
|
|
// Wait for all operations to finish.
|
|
|
|
|
await Promise.all([ |
|
|
|
|
modifyAlbumPromise, |
|
|
|
|
removeUnlinkedArtists, |
|
|
|
|
removeUnlinkedTags, |
|
|
|
|
addArtists, |
|
|
|
|
addTags |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
// Respond to the request.
|
|
|
|
|
res.status(200).send(); |
|
|
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
catchUnhandledErrors(e); |
|
|
|
|
trx.rollback(); |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
// Wait for all operations to finish.
|
|
|
|
|
await Promise.all([ |
|
|
|
|
modifyAlbumPromise, |
|
|
|
|
removeUnlinkedArtists, |
|
|
|
|
removeUnlinkedTags, |
|
|
|
|
addArtists, |
|
|
|
|
addTags |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
// Respond to the request.
|
|
|
|
|
res.status(200).send(); |
|
|
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
catchUnhandledErrors(e); |
|
|
|
|
trx.rollback(); |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |