From 291b183ef6900b2fb955212b20c0c0eb082ff0ff Mon Sep 17 00:00:00 2001 From: Zefir Kirilov Date: Mon, 8 Jul 2024 03:50:30 +0300 Subject: [PATCH] Make track IDs Hash(name + artist) --- src/Library.ts | 18 ++++++++++-------- src/api/Api.ts | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Library.ts b/src/Library.ts index ceb96bb..39088c2 100644 --- a/src/Library.ts +++ b/src/Library.ts @@ -82,7 +82,7 @@ class Library { return library; } - readonly #music: Map = new Map(); + readonly #music: Map = new Map(); public constructor() { } @@ -91,14 +91,10 @@ class Library { return Array.from(this.#music.values()); } - public getTrack(id: number): Library.Track | null { + public getTrack(id: string): Library.Track | null { return this.#music.get(id) ?? null; } - public nextTrackId(): number { - return this.#music.size === 0 ? 0 : Math.max(...this.#music.keys()) + 1; - } - public async addTrack(...tracks: Music[]): Promise { for (const music of tracks) { let album: Library.Album | null = null; @@ -109,7 +105,7 @@ class Library { this.#albums.set(album.id, album); } } - const track = music instanceof Library.Track ? music : new Library.Track(this.nextTrackId(), music, album); + const track = music instanceof Library.Track ? music : new Library.Track(music, album); for (const artistName of music.artists) { const artist = this.#artists.get(Library.Artist.id(artistName)) ?? await (async () => { try { @@ -209,8 +205,10 @@ class Library { namespace Library { export class Track extends Music { - public constructor(public readonly id: number, music: Music, public readonly album: Album | null) { + public readonly id: string; + public constructor(music: Music, public readonly album: Album | null) { super(music.file, music.meta, music); + this.id = Library.Track.id(music.title, music.artist); } public get(): JsonResponse.Object { @@ -237,6 +235,10 @@ namespace Library { } } } + + public static id(name: string, artist: string | null) { + return Library.id(artist, name); + } } export class Album { diff --git a/src/api/Api.ts b/src/api/Api.ts index 3dcf193..e31a99b 100644 --- a/src/api/Api.ts +++ b/src/api/Api.ts @@ -48,7 +48,7 @@ class Api { .value; } else { - const track = this.library.getTrack(Number.parseInt(parts[1]!, 10)); + const track = this.library.getTrack(parts[1]!); if (track === null) return new ErrorResponse(404, "The requested track is not part of this library.");