217 lines
4.7 KiB
TypeScript
217 lines
4.7 KiB
TypeScript
import { apiClient } from '../client'
|
|
|
|
export type PlaylistSortField =
|
|
| 'name'
|
|
| 'genre'
|
|
| 'created_at'
|
|
| 'updated_at'
|
|
| 'sound_count'
|
|
| 'total_duration'
|
|
export type SortOrder = 'asc' | 'desc'
|
|
|
|
export interface Playlist {
|
|
id: number
|
|
name: string
|
|
description: string | null
|
|
genre: string | null
|
|
user_id: number | null
|
|
user_name: string | null
|
|
is_main: boolean
|
|
is_current: boolean
|
|
is_deletable: boolean
|
|
is_favorited: boolean
|
|
favorite_count: number
|
|
created_at: string
|
|
updated_at: string | null
|
|
sound_count: number
|
|
total_duration: number
|
|
}
|
|
|
|
export interface PlaylistSound {
|
|
id: number
|
|
filename: string
|
|
name: string
|
|
duration: number | null
|
|
size: number
|
|
hash: string
|
|
type: string
|
|
play_count: number
|
|
is_normalized: boolean
|
|
created_at: string
|
|
updated_at: string | null
|
|
}
|
|
|
|
export interface GetPlaylistsParams {
|
|
search?: string
|
|
sort_by?: PlaylistSortField
|
|
sort_order?: SortOrder
|
|
page?: number
|
|
limit?: number
|
|
favorites_only?: boolean
|
|
}
|
|
|
|
export interface GetPlaylistsResponse {
|
|
playlists: Playlist[]
|
|
total: number
|
|
page: number
|
|
limit: number
|
|
total_pages: number
|
|
}
|
|
|
|
export class PlaylistsService {
|
|
/**
|
|
* Get all playlists with optional filtering, searching, and sorting
|
|
*/
|
|
async getPlaylists(params?: GetPlaylistsParams): Promise<GetPlaylistsResponse> {
|
|
const searchParams = new URLSearchParams()
|
|
|
|
// Handle parameters
|
|
if (params?.search) {
|
|
searchParams.append('search', params.search)
|
|
}
|
|
if (params?.sort_by) {
|
|
searchParams.append('sort_by', params.sort_by)
|
|
}
|
|
if (params?.sort_order) {
|
|
searchParams.append('sort_order', params.sort_order)
|
|
}
|
|
if (params?.page) {
|
|
searchParams.append('page', params.page.toString())
|
|
}
|
|
if (params?.limit) {
|
|
searchParams.append('limit', params.limit.toString())
|
|
}
|
|
if (params?.favorites_only) {
|
|
searchParams.append('favorites_only', 'true')
|
|
}
|
|
|
|
const url = searchParams.toString()
|
|
? `/api/v1/playlists/?${searchParams.toString()}`
|
|
: '/api/v1/playlists/'
|
|
return apiClient.get<GetPlaylistsResponse>(url)
|
|
}
|
|
|
|
/**
|
|
* Get current user's playlists
|
|
*/
|
|
async getUserPlaylists(): Promise<Playlist[]> {
|
|
return apiClient.get<Playlist[]>('/api/v1/playlists/user')
|
|
}
|
|
|
|
/**
|
|
* Get main playlist
|
|
*/
|
|
async getMainPlaylist(): Promise<Playlist> {
|
|
return apiClient.get<Playlist>('/api/v1/playlists/main')
|
|
}
|
|
|
|
/**
|
|
* Get current playlist
|
|
*/
|
|
async getCurrentPlaylist(): Promise<Playlist> {
|
|
return apiClient.get<Playlist>('/api/v1/playlists/current')
|
|
}
|
|
|
|
/**
|
|
* Create a new playlist
|
|
*/
|
|
async createPlaylist(data: {
|
|
name: string
|
|
description?: string
|
|
genre?: string
|
|
}): Promise<Playlist> {
|
|
return apiClient.post<Playlist>('/api/v1/playlists/', data)
|
|
}
|
|
|
|
/**
|
|
* Update a playlist
|
|
*/
|
|
async updatePlaylist(
|
|
id: number,
|
|
data: {
|
|
name?: string
|
|
description?: string
|
|
genre?: string
|
|
},
|
|
): Promise<Playlist> {
|
|
return apiClient.put<Playlist>(`/api/v1/playlists/${id}`, data)
|
|
}
|
|
|
|
/**
|
|
* Delete a playlist
|
|
*/
|
|
async deletePlaylist(id: number): Promise<void> {
|
|
await apiClient.delete(`/api/v1/playlists/${id}`)
|
|
}
|
|
|
|
/**
|
|
* Set playlist as current
|
|
*/
|
|
async setCurrentPlaylist(id: number): Promise<Playlist> {
|
|
return apiClient.put<Playlist>(`/api/v1/playlists/${id}/set-current`)
|
|
}
|
|
|
|
/**
|
|
* Get playlist statistics
|
|
*/
|
|
async getPlaylistStats(id: number): Promise<{
|
|
sound_count: number
|
|
total_duration_ms: number
|
|
total_play_count: number
|
|
}> {
|
|
return apiClient.get(`/api/v1/playlists/${id}/stats`)
|
|
}
|
|
|
|
/**
|
|
* Get a specific playlist by ID
|
|
*/
|
|
async getPlaylist(id: number): Promise<Playlist> {
|
|
return apiClient.get<Playlist>(`/api/v1/playlists/${id}`)
|
|
}
|
|
|
|
/**
|
|
* Get sounds in a playlist
|
|
*/
|
|
async getPlaylistSounds(id: number): Promise<PlaylistSound[]> {
|
|
return apiClient.get<PlaylistSound[]>(`/api/v1/playlists/${id}/sounds`)
|
|
}
|
|
|
|
/**
|
|
* Add sound to playlist
|
|
*/
|
|
async addSoundToPlaylist(
|
|
playlistId: number,
|
|
soundId: number,
|
|
position?: number,
|
|
): Promise<void> {
|
|
await apiClient.post(`/api/v1/playlists/${playlistId}/sounds`, {
|
|
sound_id: soundId,
|
|
position,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Remove sound from playlist
|
|
*/
|
|
async removeSoundFromPlaylist(
|
|
playlistId: number,
|
|
soundId: number,
|
|
): Promise<void> {
|
|
await apiClient.delete(`/api/v1/playlists/${playlistId}/sounds/${soundId}`)
|
|
}
|
|
|
|
/**
|
|
* Reorder sounds in playlist
|
|
*/
|
|
async reorderPlaylistSounds(
|
|
playlistId: number,
|
|
soundPositions: Array<[number, number]>,
|
|
): Promise<void> {
|
|
await apiClient.put(`/api/v1/playlists/${playlistId}/sounds/reorder`, {
|
|
sound_positions: soundPositions,
|
|
})
|
|
}
|
|
}
|
|
|
|
export const playlistsService = new PlaylistsService()
|