feat: add playlist editing functionality; implement PlaylistEditPage and integrate with playlists service
feat: enhance PlaylistsPage with search, sorting, and playlist creation features; improve UI components and state management
This commit is contained in:
181
src/lib/api/services/playlists.ts
Normal file
181
src/lib/api/services/playlists.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
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
|
||||
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
|
||||
limit?: number
|
||||
offset?: number
|
||||
}
|
||||
|
||||
export class PlaylistsService {
|
||||
/**
|
||||
* Get all playlists with optional filtering, searching, and sorting
|
||||
*/
|
||||
async getPlaylists(params?: GetPlaylistsParams): Promise<Playlist[]> {
|
||||
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?.limit) {
|
||||
searchParams.append('limit', params.limit.toString())
|
||||
}
|
||||
if (params?.offset) {
|
||||
searchParams.append('offset', params.offset.toString())
|
||||
}
|
||||
|
||||
const url = searchParams.toString() ? `/api/v1/playlists/?${searchParams.toString()}` : '/api/v1/playlists/'
|
||||
return apiClient.get<Playlist[]>(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()
|
||||
Reference in New Issue
Block a user