Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make track IDs Hash(name + artist) #16

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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