web-player: add albums to playlists through the bulk endpoint
This commit is contained in:
@@ -47,5 +47,4 @@ Each page loads its data in `+page.server.ts` through the typed API client, and
|
||||
## Pending backend work
|
||||
|
||||
- Sorting playlists by last listen (`lastPlayedAt`) for a "recently played playlists" shelf.
|
||||
- A bulk endpoint to add several tracks to a playlist at once (adding an album currently posts one track at a time).
|
||||
- An endpoint returning the most listened tracks, to bring back a "popular" section on Home.
|
||||
|
||||
Vendored
+58
@@ -331,6 +331,23 @@ export interface paths {
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/playlists/{playlistId}/albums/{albumId}": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put?: never;
|
||||
/** Add All The Tracks Of An Album To One Of Your PlayLists, Skipping Those Already In It. */
|
||||
post: operations["AddAlbumToPlayList"];
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/playlists/{playlistId}/tracks": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
@@ -731,6 +748,10 @@ export interface paths {
|
||||
export type webhooks = Record<string, never>;
|
||||
export interface components {
|
||||
schemas: {
|
||||
AddAlbumToPlayListResponse: {
|
||||
/** Format: int32 */
|
||||
addedCount: number | string;
|
||||
};
|
||||
AddPlayListTrackRequest: {
|
||||
/** Format: uuid */
|
||||
trackId: string;
|
||||
@@ -1892,6 +1913,43 @@ export interface operations {
|
||||
};
|
||||
};
|
||||
};
|
||||
AddAlbumToPlayList: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
playlistId: string;
|
||||
albumId: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description OK */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["AddAlbumToPlayListResponse"];
|
||||
};
|
||||
};
|
||||
/** @description Unauthorized */
|
||||
401: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content?: never;
|
||||
};
|
||||
/** @description Not Found */
|
||||
404: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content?: never;
|
||||
};
|
||||
};
|
||||
};
|
||||
AddPlayListTrack: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { fail } from '@sveltejs/kit';
|
||||
import { authedAction, formString } from '$lib/server/api';
|
||||
import { ALBUM_TRACKS_PAGE_SIZE } from '$lib/config';
|
||||
|
||||
export const addTrackAction = authedAction(async ({ api, form, params }) => {
|
||||
const playlistId = formString(form, 'playlistId') || (params.id ?? '');
|
||||
@@ -33,22 +32,14 @@ export const addAlbumToPlaylistAction = authedAction(async ({ api, form, params
|
||||
return fail(400, { message: 'Faltan datos del álbum.' });
|
||||
}
|
||||
|
||||
const { data: tracks, error: tracksError } = await api.GET('/albums/{albumId}/tracks', {
|
||||
params: { path: { albumId }, query: { pageNumber: 1, pageSize: ALBUM_TRACKS_PAGE_SIZE } }
|
||||
const { data, error } = await api.POST('/playlists/{playlistId}/albums/{albumId}', {
|
||||
params: { path: { playlistId, albumId } }
|
||||
});
|
||||
|
||||
if (tracksError || !tracks) {
|
||||
return fail(502, { message: 'No se pudo cargar el álbum.', albumId });
|
||||
}
|
||||
|
||||
let added = 0;
|
||||
for (const track of tracks.items) {
|
||||
const { error } = await api.POST('/playlists/{playlistId}/tracks', {
|
||||
params: { path: { playlistId } },
|
||||
body: { trackId: track.id }
|
||||
});
|
||||
if (!error) added++;
|
||||
if (error || !data) {
|
||||
return fail(502, { message: 'No se pudo añadir el álbum.', albumId });
|
||||
}
|
||||
|
||||
const added = Number(data.addedCount);
|
||||
return { addedAlbum: true, albumId, playlistId, added };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user