118 lines
3.6 KiB
TypeScript

import type { Song, PlaylistNode } from '../types/interfaces';
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000/api';
export interface PaginationInfo {
page: number;
limit: number;
totalSongs: number;
totalPages: number;
hasNextPage: boolean;
hasPrevPage: boolean;
}
export interface SongsResponse {
songs: Song[];
pagination: PaginationInfo;
totalDuration?: number; // Total duration of the entire playlist in seconds
}
class Api {
// Legacy method for backward compatibility
async getSongs(): Promise<Song[]> {
const response = await fetch(`${API_BASE_URL}/songs`);
if (!response.ok) throw new Error('Failed to fetch songs');
return response.json();
}
// New paginated method for all songs
async getSongsPaginated(page: number = 1, limit: number = 50, search: string = ''): Promise<SongsResponse> {
const params = new URLSearchParams({
page: page.toString(),
limit: limit.toString(),
...(search && { search })
});
const response = await fetch(`${API_BASE_URL}/songs?${params}`);
if (!response.ok) throw new Error('Failed to fetch songs');
return response.json();
}
// New paginated method for playlist songs
async getPlaylistSongsPaginated(playlistName: string, page: number = 1, limit: number = 50, search: string = ''): Promise<SongsResponse> {
const params = new URLSearchParams({
page: page.toString(),
limit: limit.toString(),
...(search && { search })
});
const encodedPlaylistName = encodeURIComponent(playlistName);
const response = await fetch(`${API_BASE_URL}/songs/playlist/${encodedPlaylistName}?${params}`);
if (!response.ok) throw new Error('Failed to fetch playlist songs');
return response.json();
}
// Get total song count
async getSongCount(): Promise<number> {
const response = await fetch(`${API_BASE_URL}/songs/count`);
if (!response.ok) throw new Error('Failed to fetch song count');
const data = await response.json();
return data.count;
}
async saveSongs(songs: Song[]): Promise<Song[]> {
const response = await fetch(`${API_BASE_URL}/songs/batch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(songs),
});
if (!response.ok) throw new Error('Failed to save songs');
return response.json();
}
async getPlaylists(): Promise<PlaylistNode[]> {
const response = await fetch(`${API_BASE_URL}/playlists`);
if (!response.ok) throw new Error('Failed to fetch playlists');
return response.json();
}
// Get playlist structure only (without track data) for faster loading
async getPlaylistStructure(): Promise<PlaylistNode[]> {
const response = await fetch(`${API_BASE_URL}/playlists/structure`);
if (!response.ok) throw new Error('Failed to fetch playlist structure');
return response.json();
}
async savePlaylists(playlists: PlaylistNode[]): Promise<PlaylistNode[]> {
const response = await fetch(`${API_BASE_URL}/playlists/batch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(playlists),
});
if (!response.ok) throw new Error('Failed to save playlists');
return response.json();
}
async resetDatabase(): Promise<boolean> {
try {
const response = await fetch(`${API_BASE_URL}/reset`, {
method: 'POST',
});
if (!response.ok) {
throw new Error('Failed to reset database');
}
return true;
} catch (error) {
console.error('Error resetting database:', error);
throw error;
}
}
}
export const api = new Api();