fixed creation of database entries / links

This commit is contained in:
Daniel Sommer 2022-06-15 17:04:18 +02:00
parent 73c225ac6d
commit 60f431d6cf

View file

@ -4,6 +4,8 @@ const { extname } = require('path');
const Artist = require('../models/Artist.js');
const Album = require('../models/Album.js');
const Track = require('../models/Track.js');
const TrackToArtist = require('../models/TrackToArtist.js');
const TrackToAlbum = require('../models/TrackToAlbum.js');
const AlbumToArtist = require('../models/AlbumToArtist.js');
class Queue {
@ -82,8 +84,8 @@ class Queue {
const artists = await this.addArtist(tags);
const album = await this.addAlbum(tags);
const track = await this.addTrack(tags, file);
// this.linkTrackToArtist(track, artist);
// this.linkTrackToAlbum(track, album);
this.linkTrackToArtists(track, artists);
this.linkTrackToAlbum(track, album);
this.linkAlbumToArtists(album, artists);
}
@ -152,23 +154,12 @@ class Queue {
return track;
}
async linkTrackToArtist(track, artist) {
if (track === undefined || artist === undefined) {
async linkTrackToArtists(track, artists) {
if (track === undefined || artists === undefined || artists.length === 0) {
return;
}
try {
const [element, created] = await database.models.TrackToArtist.findOrCreate({
where: {
track: track.id,
artist: artist.id
}
});
if (created) {
logger.debug('linked track \'' + track.id + '\' to artist \'' + artist.id + '\': ' + JSON.stringify(element));
}
return element;
} catch (err) {
logger.error('error finding or creating tracktoartist entry for track \'' + track.id + '\' and album \'' + artist.id + '\' > ' + err);
for (let index = 0; index < artists.length; index++) {
await new TrackToArtist(track, artists[index]).save();
}
}
@ -176,20 +167,7 @@ class Queue {
if (track === undefined || album === undefined) {
return;
}
try {
const [element, created] = await database.models.TrackToAlbum.findOrCreate({
where: {
track: track.id,
album: album.id
}
});
if (created) {
logger.debug('linked track \'' + track.id + '\' to album \'' + album.id + '\': ' + JSON.stringify(element));
}
return element;
} catch (err) {
logger.error('error finding or creating tracktoalbum entry for track \'' + track.id + '\' and album \'' + album.id + '\' > ' + err);
}
await new TrackToAlbum(track, album).save();
}
async linkAlbumToArtists(album, artists) {