Skip to content

Commit

Permalink
Make track IDs Hash(name + artist) (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git authored Jul 9, 2024
2 parents 65bc11f + 291b183 commit a5fb88c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/Library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Library {
return library;
}

readonly #music: Map<number, Library.Track> = new Map();
readonly #music: Map<string, Library.Track> = new Map();

public constructor() {
}
Expand All @@ -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<void> {
for (const music of tracks) {
let album: Library.Album | null = null;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -237,6 +235,10 @@ namespace Library {
}
}
}

public static id(name: string, artist: string | null) {
return Library.id(artist, name);
}
}

export class Album {
Expand Down
2 changes: 1 addition & 1 deletion src/api/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down

0 comments on commit a5fb88c

Please sign in to comment.