feat: implement favorites functionality with SoundCard integration and FavoritesService
This commit is contained in:
112
src/lib/api/services/favorites.ts
Normal file
112
src/lib/api/services/favorites.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { apiClient } from '../client'
|
||||
|
||||
export interface Favorite {
|
||||
id: number
|
||||
user_id: number
|
||||
sound_id?: number
|
||||
playlist_id?: number
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface FavoriteCountsResponse {
|
||||
total: number
|
||||
sounds: number
|
||||
playlists: number
|
||||
}
|
||||
|
||||
export interface FavoritesListResponse {
|
||||
favorites: Favorite[]
|
||||
}
|
||||
|
||||
export class FavoritesService {
|
||||
/**
|
||||
* Add a sound to favorites
|
||||
*/
|
||||
async addSoundFavorite(soundId: number): Promise<Favorite> {
|
||||
const response = await apiClient.post<Favorite>(`/api/v1/favorites/sounds/${soundId}`)
|
||||
return response
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a sound from favorites
|
||||
*/
|
||||
async removeSoundFavorite(soundId: number): Promise<void> {
|
||||
await apiClient.delete(`/api/v1/favorites/sounds/${soundId}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a playlist to favorites
|
||||
*/
|
||||
async addPlaylistFavorite(playlistId: number): Promise<Favorite> {
|
||||
const response = await apiClient.post<Favorite>(`/api/v1/favorites/playlists/${playlistId}`)
|
||||
return response
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a playlist from favorites
|
||||
*/
|
||||
async removePlaylistFavorite(playlistId: number): Promise<void> {
|
||||
await apiClient.delete(`/api/v1/favorites/playlists/${playlistId}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all favorites for the current user
|
||||
*/
|
||||
async getFavorites(limit = 50, offset = 0): Promise<Favorite[]> {
|
||||
const response = await apiClient.get<FavoritesListResponse>(
|
||||
`/api/v1/favorites/?limit=${limit}&offset=${offset}`
|
||||
)
|
||||
return response.favorites
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sound favorites for the current user
|
||||
*/
|
||||
async getSoundFavorites(limit = 50, offset = 0): Promise<Favorite[]> {
|
||||
const response = await apiClient.get<FavoritesListResponse>(
|
||||
`/api/v1/favorites/sounds?limit=${limit}&offset=${offset}`
|
||||
)
|
||||
return response.favorites
|
||||
}
|
||||
|
||||
/**
|
||||
* Get playlist favorites for the current user
|
||||
*/
|
||||
async getPlaylistFavorites(limit = 50, offset = 0): Promise<Favorite[]> {
|
||||
const response = await apiClient.get<FavoritesListResponse>(
|
||||
`/api/v1/favorites/playlists?limit=${limit}&offset=${offset}`
|
||||
)
|
||||
return response.favorites
|
||||
}
|
||||
|
||||
/**
|
||||
* Get favorite counts for the current user
|
||||
*/
|
||||
async getFavoriteCounts(): Promise<FavoriteCountsResponse> {
|
||||
const response = await apiClient.get<FavoriteCountsResponse>('/api/v1/favorites/counts')
|
||||
return response
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a sound is favorited
|
||||
*/
|
||||
async isSoundFavorited(soundId: number): Promise<boolean> {
|
||||
const response = await apiClient.get<{ is_favorited: boolean }>(
|
||||
`/api/v1/favorites/sounds/${soundId}/check`
|
||||
)
|
||||
return response.is_favorited
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a playlist is favorited
|
||||
*/
|
||||
async isPlaylistFavorited(playlistId: number): Promise<boolean> {
|
||||
const response = await apiClient.get<{ is_favorited: boolean }>(
|
||||
`/api/v1/favorites/playlists/${playlistId}/check`
|
||||
)
|
||||
return response.is_favorited
|
||||
}
|
||||
}
|
||||
|
||||
export const favoritesService = new FavoritesService()
|
||||
@@ -3,3 +3,4 @@ export * from './sounds'
|
||||
export * from './player'
|
||||
export * from './files'
|
||||
export * from './extractions'
|
||||
export * from './favorites'
|
||||
|
||||
@@ -17,6 +17,8 @@ export interface Sound {
|
||||
thumbnail?: string
|
||||
is_music: boolean
|
||||
is_deletable: boolean
|
||||
is_favorited: boolean
|
||||
favorite_count: number
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user