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.
 
 
 
 

346 lines
13 KiB

import Knex from "knex";
import { Track, TrackRefs, TrackDetails, Id, Name, StoreLinks, Tag, Album, Artist, TagParentId } from "../../client/src/api/api";
import * as api from '../../client/src/api/api';
import asJson from "../lib/asJson";
import { DBError, DBErrorKind } from "../endpoints/types";
import { makeNotFoundError } from "./common";
var _ = require('lodash')
// Returns an track with details, or null if not found.
export async function getTrack(id: number, userId: number, knex: Knex):
Promise<Track & Name & StoreLinks & TrackDetails> {
// Start transfers for tracks, tags and artists.
// Also request the track itself.
const tagsPromise: Promise<(Tag & Id & Name & TagParentId)[]> =
knex.select('tagId')
.from('tracks_tags')
.where({ 'trackId': id })
.then((tags: any) => tags.map((tag: any) => tag['tagId']))
.then((ids: number[]) =>
knex.select(['id', 'name', 'parentId'])
.from('tags')
.whereIn('id', ids)
.then((tags: (Id & Name & TagParentId)[]) =>
tags.map((tag : (Id & Name & TagParentId)) =>
{ return {...tag, mbApi_typename: "tag"}}
))
);
const artistsPromise: Promise<(Artist & Id & Name & StoreLinks)[]> =
knex.select('artistId')
.from('tracks_artists')
.where({ 'trackId': id })
.then((artists: any) => artists.map((artist: any) => artist['artistId']))
.then((ids: number[]) =>
knex.select(['id', 'name', 'storeLinks'])
.from('artists')
.whereIn('id', ids)
.then((artists: (Id & Name & StoreLinks)[]) =>
artists.map((artist : (Id & Name & StoreLinks)) =>
{ return {...artist, mbApi_typename: "artist"}}
))
);
const trackPromise: Promise<(Track & StoreLinks & Name) | undefined> =
knex.select('name', 'storeLinks', 'album')
.from('tracks')
.where({ 'user': userId })
.where({ id: id })
.then((tracks: any) => { return {
name: tracks[0].name,
storeLinks: tracks[0].storeLinks,
albumId: tracks[0].album,
mbApi_typename: 'track'
}});
const albumPromise: Promise<(Album & Name & Id & StoreLinks) | null> =
trackPromise
.then((t: api.Track | undefined) =>
t ? knex.select('id', 'name', 'storeLinks')
.from('albums')
.where({ 'user': userId })
.where({ id: t.albumId })
.then((albums: any) => albums.length > 0 ?
{...albums[0], mpApi_typename: 'album' }
: null)
: (() => null)()
)
// Wait for the requests to finish.
const [track, tags, album, artists] =
await Promise.all([trackPromise, tagsPromise, albumPromise, artistsPromise]);
if (track) {
return {
mbApi_typename: 'track',
name: track['name'],
artists: artists || [],
tags: tags || [],
album: album || null,
storeLinks: asJson(track['storeLinks'] || []),
};
} else {
throw makeNotFoundError();
}
}
// Returns the id of the created track.
export async function createTrack(userId: number, track: (Track & Name & TrackRefs), knex: Knex): Promise<number> {
return await knex.transaction(async (trx) => {
// Start retrieving artists.
const artistIdsPromise: Promise<number[]> =
trx.select('id')
.from('artists')
.where({ 'user': userId })
.whereIn('id', track.artistIds)
.then((as: any) => as.map((a: any) => a['id']));
// Start retrieving tags.
const tagIdsPromise: Promise<number[]> =
trx.select('id')
.from('tags')
.where({ 'user': userId })
.whereIn('id', track.tagIds)
.then((as: any) => as.map((a: any) => a['id']));
// Start retrieving album.
const albumIdPromise: Promise<number | null> =
track.albumId ?
trx.select('id')
.from('albums')
.where({ 'user': userId, 'id': track.albumId })
.then((albums: any) => albums.map((album: any) => album['id']))
.then((ids: number[]) =>
ids.length > 0 ? ids[0] : (() => null)()
) :
(async () => null)();
// Wait for the requests to finish.
var [artists, tags, album] = await Promise.all([artistIdsPromise, tagIdsPromise, albumIdPromise]);
// Check that we found all artists and tags we need.
if (!_.isEqual((artists as number[]).sort(), track.artistIds.sort()) ||
(!_.isEqual((tags as number[]).sort(), track.tagIds.sort())) ||
(track.albumId && (album === null))) {
throw makeNotFoundError();
}
// Create the track.
const trackId = (await trx('tracks')
.insert({
name: track.name,
storeLinks: JSON.stringify(track.storeLinks || []),
user: userId,
album: album || null,
})
.returning('id') // Needed for Postgres
)[0];
// Link the artists via the linking table.
if (artists && artists.length) {
await trx('tracks_artists').insert(
artists.map((artistId: number) => {
return {
artistId: artistId,
trackId: trackId,
}
})
)
}
// Link the tags via the linking table.
if (tags && tags.length) {
await trx('tracks_tags').insert(
tags.map((tagId: number) => {
return {
trackId: trackId,
tagId: tagId,
}
})
)
}
console.log('created track', track, ', ID ', trackId);
return trackId;
})
}
export async function modifyTrack(userId: number, trackId: number, track: Track, knex: Knex): Promise<void> {
await knex.transaction(async (trx) => {
// Start retrieving the track itself.
const trackIdPromise: Promise<number | undefined> =
trx.select('id')
.from('tracks')
.where({ 'user': userId })
.where({ id: trackId })
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined);
// Start retrieving artists if we are modifying those.
const artistIdsPromise: Promise<number[] | undefined> =
track.artistIds ?
trx.select('id')
.from('artists')
.whereIn('id', track.artistIds)
.then((as: any) => as.map((a: any) => a['id']))
: (async () => undefined)();
// Start retrieving tags if we are modifying those.
const tagIdsPromise =
track.tagIds ?
trx.select('id')
.from('tags')
.whereIn('id', track.tagIds)
.then((ts: any) => ts.map((t: any) => t['id'])) :
(async () => undefined)();
// Start retrieving album if we are modifying that.
const albumIdPromise =
track.albumId ?
trx.select('id')
.from('albums')
.where({ 'user': userId })
.where({ id: track.albumId })
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined) :
(async () => undefined)();
// Wait for the requests to finish.
var [oldTrack, artists, tags, album] = await Promise.all([trackIdPromise, artistIdsPromise, tagIdsPromise, albumIdPromise]);;
console.log("Patch track: ", oldTrack, artists, tags, album);
// Check that we found all objects we need.
if ((track.artistIds && (!artists || !_.isEqual(artists.sort(), (track.artistIds || []).sort()))) ||
(track.tagIds && (!tags || !_.isEqual(tags.sort(), (track.tagIds || []).sort()))) ||
(track.albumId && !album) ||
!oldTrack) {
throw makeNotFoundError();
}
// Modify the track.
var update: any = {};
if ("name" in track) { update["name"] = track.name; }
if ("storeLinks" in track) { update["storeLinks"] = JSON.stringify(track.storeLinks || []); }
if ("albumId" in track) { update["album"] = track.albumId; }
const modifyTrackPromise = trx('tracks')
.where({ 'user': userId })
.where({ 'id': trackId })
.update(update)
// Remove unlinked artists.
const removeUnlinkedArtists = artists ? trx('tracks_artists')
.where({ 'trackId': trackId })
.whereNotIn('artistId', track.artistIds || [])
.delete() : undefined;
// Remove unlinked tags.
const removeUnlinkedTags = tags ? trx('tracks_tags')
.where({ 'trackId': trackId })
.whereNotIn('tagId', track.tagIds || [])
.delete() : undefined;
// Link new artists.
const addArtists = artists ? trx('tracks_artists')
.where({ 'trackId': trackId })
.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,
trackId: trackId,
}
})
// Link them
return Promise.all(
insertObjects.map((obj: any) =>
trx('tracks_artists').insert(obj)
)
);
}) : undefined;
// Link new tags.
const addTags = tags ? trx('tracks_tags')
.where({ 'trackId': trackId })
.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,
trackId: trackId,
}
})
// Link them
return Promise.all(
insertObjects.map((obj: any) =>
trx('tracks_tags').insert(obj)
)
);
}) : undefined;
// Wait for all operations to finish.
await Promise.all([
modifyTrackPromise,
removeUnlinkedArtists,
removeUnlinkedTags,
addArtists,
addTags,
]);
return;
})
}
export async function deleteTrack(userId: number, trackId: number, knex: Knex): Promise<void> {
await knex.transaction(async (trx) => {
// FIXME remove
let tracks = await trx.select('id', 'name')
.from('tracks');
console.log("All tracks:", tracks);
// Start by retrieving the track itself for sanity.
const confirmTrackId: number | undefined =
await trx.select('id')
.from('tracks')
.where({ 'user': userId })
.where({ id: trackId })
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined);
if (!confirmTrackId) {
throw makeNotFoundError();
}
// Start deleting artist associations with the track.
const deleteArtistsPromise: Promise<any> =
trx.delete()
.from('tracks_artists')
.where({ 'trackId': trackId });
// Start deleting tag associations with the track.
const deleteTagsPromise: Promise<any> =
trx.delete()
.from('tracks_tags')
.where({ 'trackId': trackId });
// Start deleting the track.
const deleteTrackPromise: Promise<any> =
trx.delete()
.from('tracks')
.where({ id: trackId });
// Wait for the requests to finish.
await Promise.all([deleteArtistsPromise, deleteTagsPromise, deleteTrackPromise]);
})
}