|
|
|
@ -160,21 +160,21 @@ export async function createArtist(userId: number, artist: ArtistWithRefs, knex: |
|
|
|
|
export async function modifyArtist(userId: number, artistId: number, artist: ArtistBaseWithRefs, knex: Knex): Promise<void> { |
|
|
|
|
await knex.transaction(async (trx) => { |
|
|
|
|
// Start retrieving the artist itself.
|
|
|
|
|
const artistIdPromise: Promise<number | undefined> = |
|
|
|
|
const artistIdPromise: Promise<number | undefined | null> = |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('artists') |
|
|
|
|
.where({ 'user': userId }) |
|
|
|
|
.where({ id: artistId }) |
|
|
|
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined); |
|
|
|
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : null); |
|
|
|
|
|
|
|
|
|
// Start retrieving albums if we are modifying those.
|
|
|
|
|
const albumIdsPromise: Promise<number[] | undefined> = |
|
|
|
|
const albumIdsPromise: Promise<number[] | undefined | null> = |
|
|
|
|
artist.albumIds ? |
|
|
|
|
trx.select('id') |
|
|
|
|
.from('albums') |
|
|
|
|
.whereIn('id', artist.albumIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['id'])) |
|
|
|
|
: (async () => undefined)(); |
|
|
|
|
: (async () => null)(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tracks if we are modifying those.
|
|
|
|
|
const trackIdsPromise: Promise<number[] | undefined> = |
|
|
|
@ -183,7 +183,7 @@ export async function modifyArtist(userId: number, artistId: number, artist: Art |
|
|
|
|
.from('tracks') |
|
|
|
|
.whereIn('id', artist.trackIds) |
|
|
|
|
.then((as: any) => as.map((a: any) => a['id'])) |
|
|
|
|
: (async () => undefined)(); |
|
|
|
|
: (async () => null)(); |
|
|
|
|
|
|
|
|
|
// Start retrieving tags if we are modifying those.
|
|
|
|
|
const tagIdsPromise = |
|
|
|
@ -192,15 +192,15 @@ export async function modifyArtist(userId: number, artistId: number, artist: Art |
|
|
|
|
.from('tags') |
|
|
|
|
.whereIn('id', artist.tagIds) |
|
|
|
|
.then((ts: any) => ts.map((t: any) => t['id'])) : |
|
|
|
|
(async () => undefined)(); |
|
|
|
|
(async () => null)(); |
|
|
|
|
|
|
|
|
|
// Wait for the requests to finish.
|
|
|
|
|
var [oldArtist, albums, tags, tracks] = await Promise.all([artistIdPromise, albumIdsPromise, tagIdsPromise, trackIdsPromise]);; |
|
|
|
|
|
|
|
|
|
// Check that we found all objects we need.
|
|
|
|
|
if ((!albums || !_.isEqual(albums.sort(), (artist.albumIds || []).sort())) || |
|
|
|
|
(!tags || !_.isEqual(tags.sort(), (artist.tagIds || []).sort())) || |
|
|
|
|
(!tracks || !_.isEqual(tracks.sort(), (artist.trackIds || []).sort())) || |
|
|
|
|
if ((albums === undefined || !_.isEqual((albums || []).sort(), (artist.albumIds || []).sort())) || |
|
|
|
|
(tags === undefined || !_.isEqual((tags || []).sort(), (artist.tagIds || []).sort())) || |
|
|
|
|
(tracks === undefined || !_.isEqual((tracks || []).sort(), (artist.trackIds || []).sort())) || |
|
|
|
|
!oldArtist) { |
|
|
|
|
throw makeNotFoundError(); |
|
|
|
|
} |
|
|
|
|