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
|
## Pending backend work
|
||||||
|
|
||||||
- Sorting playlists by last listen (`lastPlayedAt`) for a "recently played playlists" shelf.
|
- 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.
|
- 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;
|
patch?: never;
|
||||||
trace?: 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": {
|
"/playlists/{playlistId}/tracks": {
|
||||||
parameters: {
|
parameters: {
|
||||||
query?: never;
|
query?: never;
|
||||||
@@ -731,6 +748,10 @@ export interface paths {
|
|||||||
export type webhooks = Record<string, never>;
|
export type webhooks = Record<string, never>;
|
||||||
export interface components {
|
export interface components {
|
||||||
schemas: {
|
schemas: {
|
||||||
|
AddAlbumToPlayListResponse: {
|
||||||
|
/** Format: int32 */
|
||||||
|
addedCount: number | string;
|
||||||
|
};
|
||||||
AddPlayListTrackRequest: {
|
AddPlayListTrackRequest: {
|
||||||
/** Format: uuid */
|
/** Format: uuid */
|
||||||
trackId: string;
|
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: {
|
AddPlayListTrack: {
|
||||||
parameters: {
|
parameters: {
|
||||||
query?: never;
|
query?: never;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { fail } from '@sveltejs/kit';
|
import { fail } from '@sveltejs/kit';
|
||||||
import { authedAction, formString } from '$lib/server/api';
|
import { authedAction, formString } from '$lib/server/api';
|
||||||
import { ALBUM_TRACKS_PAGE_SIZE } from '$lib/config';
|
|
||||||
|
|
||||||
export const addTrackAction = authedAction(async ({ api, form, params }) => {
|
export const addTrackAction = authedAction(async ({ api, form, params }) => {
|
||||||
const playlistId = formString(form, 'playlistId') || (params.id ?? '');
|
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.' });
|
return fail(400, { message: 'Faltan datos del álbum.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: tracks, error: tracksError } = await api.GET('/albums/{albumId}/tracks', {
|
const { data, error } = await api.POST('/playlists/{playlistId}/albums/{albumId}', {
|
||||||
params: { path: { albumId }, query: { pageNumber: 1, pageSize: ALBUM_TRACKS_PAGE_SIZE } }
|
params: { path: { playlistId, albumId } }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (tracksError || !tracks) {
|
if (error || !data) {
|
||||||
return fail(502, { message: 'No se pudo cargar el álbum.', albumId });
|
return fail(502, { message: 'No se pudo añadir 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++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const added = Number(data.addedCount);
|
||||||
return { addedAlbum: true, albumId, playlistId, added };
|
return { addedAlbum: true, albumId, playlistId, added };
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user