Move library album and track operations into shared server modules
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import type { PageServerLoad, Actions } from './$types';
|
||||
import { fail } from '@sveltejs/kit';
|
||||
import { apiFor, failOnError } from '$lib/server/api';
|
||||
import { apiFor } from '$lib/server/api';
|
||||
import { requireUser } from '$lib/server/guards';
|
||||
import { formIds, loadLibraryTracks, tagged } from '$lib/server/albums';
|
||||
import { formIds } from '$lib/server/forms/fields';
|
||||
import { tagged } from '$lib/server/actions/tagged';
|
||||
import { loadLibraryTracks } from '$lib/server/albums';
|
||||
import { deleteTracks } from '$lib/server/tracks';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals, url, fetch }) => {
|
||||
const user = requireUser(locals, url);
|
||||
@@ -15,18 +18,13 @@ export const actions: Actions = {
|
||||
const trackIds = formIds(form, 'trackIds');
|
||||
if (trackIds.length === 0) return fail(400, { message: 'Falta la canción.' });
|
||||
|
||||
const results = await Promise.all(
|
||||
trackIds.map((id) => api.DELETE('/tracks/{id}', { params: { path: { id } } }))
|
||||
);
|
||||
const failed = results.find((result) => result.error);
|
||||
return (
|
||||
(failed &&
|
||||
failOnError(
|
||||
failed,
|
||||
trackIds.length === 1
|
||||
? 'No se pudo borrar la canción.'
|
||||
: 'No se pudieron borrar todas las canciones.'
|
||||
)) ?? { deleted: true }
|
||||
);
|
||||
const { failed } = await deleteTracks(api, trackIds);
|
||||
if (failed === 0) return { deleted: true };
|
||||
return fail(502, {
|
||||
message:
|
||||
trackIds.length === 1
|
||||
? 'No se pudo borrar la canción.'
|
||||
: `No se pudieron borrar ${failed} de ${trackIds.length} canciones.`
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user