From 4ba20d741e8a56292d4a24d1a1f2b8ce7d116fef Mon Sep 17 00:00:00 2001 From: XShine555 Date: Thu, 16 Jul 2026 17:36:36 +0200 Subject: [PATCH] feat(frontend): add server action to attach local tracks to a playlist from explore --- .../src/routes/(app)/explore/+page.server.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/frontend/src/routes/(app)/explore/+page.server.ts b/frontend/src/routes/(app)/explore/+page.server.ts index 09caba8..a24e74b 100644 --- a/frontend/src/routes/(app)/explore/+page.server.ts +++ b/frontend/src/routes/(app)/explore/+page.server.ts @@ -46,6 +46,32 @@ export const load: PageServerLoad = async ({ url, locals, fetch }) => { }; export const actions: Actions = { + addTrack: async ({ request, locals, fetch }) => { + if (!locals.accessToken) return fail(401, { message: 'Inicia sesión.' }); + + const form = await request.formData(); + const playlistId = String(form.get('playlistId') ?? ''); + const trackId = String(form.get('trackId') ?? ''); + + if (!playlistId || !trackId) { + return fail(400, { message: 'Faltan datos de la canción.' }); + } + + const api = createApiClient({ fetch, accessToken: locals.accessToken }); + const { error: err, response } = await api.POST('/playlists/{playlistId}/tracks/{trackId}', { + params: { path: { playlistId, trackId } } + }); + + if (err) { + if (response?.status === 409) { + return fail(409, { message: 'La canción ya está en esa playlist.', trackId }); + } + return fail(502, { message: 'No se pudo añadir la canción.', trackId }); + } + + return { added: true, trackId, playlistId }; + }, + addYouTubeToPlaylist: async ({ request, locals, fetch }) => { if (!locals.accessToken) return fail(401, { message: 'Inicia sesión.' });