feat: implement favorites functionality with SoundCard integration and FavoritesService

This commit is contained in:
JSC
2025-08-16 21:16:13 +02:00
parent ecb17e9f94
commit 2e41d5b695
5 changed files with 228 additions and 16 deletions

View 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()

View File

@@ -3,3 +3,4 @@ export * from './sounds'
export * from './player'
export * from './files'
export * from './extractions'
export * from './favorites'

View File

@@ -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
}