Fans the frozen ETag/If-Match/412 recipe (Block reference, #253) onto the Playlist aggregate: - ReplacePlaylistItems command carries ExpectedVersion; the handler runs CheckVersion as a standalone Either after validation (so a stale write survives as 412, not flattened to 422 by Apply/Join), bumps Version unconditionally before saving, and persists via SaveChangesWithConcurrencyGuard (EF concurrency-token backstop). - PlaylistViewModel carries Version; the items GET sets a strong ETag and the PUT parses If-Match, threads it into the command, and returns the refreshed ETag on success (400 on a malformed If-Match). - Sibling item-adding handlers (AddItemsToPlaylist, AddMovie/Episode/ Season/ShowToPlaylist) bump Version too, since they mutate the same editor-visible item list. - SPA: playlists.ts exposes getPlaylistItemsWithMeta and an If-Match-aware updatePlaylist; PlaylistEditor holds the ETag in a ref, round-trips it on save, and opens a "changed elsewhere" ConfirmDialog on 412 (mirrors BlockEditor). Tests: new ReplacePlaylistItemsHandlerConcurrencyTests (stale/match/ force-write/no-op-bump/racing-save), new PlaylistController tests (ETag on GET items, 400/412/thread-version/force-write on PUT), and a vitest 412-conflict-dialog test for PlaylistsScreen. dotnet test: 1304/1304 green. web: npm run typecheck clean, npm run build clean, vitest 664/664 green. Ref #253 PR2.
96 lines
4.2 KiB
TypeScript
96 lines
4.2 KiB
TypeScript
import { ApiError, request, requestWithMeta, type ResponseWithMeta } from './client';
|
|
import type { components } from './generated/v1';
|
|
|
|
export type AddItemsToPlaylistRequest = components['schemas']['AddItemsToPlaylistRequest'];
|
|
|
|
export type PlaylistGroup = components['schemas']['PlaylistGroupResponseModel'];
|
|
export type Playlist = components['schemas']['PlaylistResponseModel'];
|
|
export type PlaylistItem = components['schemas']['PlaylistItemResponseModel'];
|
|
export type PlaylistItemRequest = components['schemas']['PlaylistItemRequest'];
|
|
export type PlaylistPreviewItem = components['schemas']['PlaylistPreviewItemResponseModel'];
|
|
export type CreatePlaylistGroupRequest = components['schemas']['CreatePlaylistGroupRequest'];
|
|
export type UpdatePlaylistGroupRequest = components['schemas']['UpdatePlaylistGroupRequest'];
|
|
export type CreatePlaylistRequest = components['schemas']['CreatePlaylistRequest'];
|
|
export type ReplacePlaylistRequest = components['schemas']['ReplacePlaylistRequest'];
|
|
|
|
export function getPlaylistGroups(): Promise<PlaylistGroup[]> {
|
|
return request<PlaylistGroup[]>('/api/playlists/groups');
|
|
}
|
|
|
|
export function createPlaylistGroup(body: CreatePlaylistGroupRequest): Promise<PlaylistGroup> {
|
|
return request<PlaylistGroup>('/api/playlists/groups', { body, method: 'POST' });
|
|
}
|
|
|
|
export function updatePlaylistGroup(id: number, body: UpdatePlaylistGroupRequest): Promise<PlaylistGroup> {
|
|
return request<PlaylistGroup>(`/api/playlists/groups/${id}`, { body, method: 'PUT' });
|
|
}
|
|
|
|
export function deletePlaylistGroup(id: number): Promise<void> {
|
|
return request<void>(`/api/playlists/groups/${id}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export function getPlaylists(playlistGroupId: number): Promise<Playlist[]> {
|
|
return request<Playlist[]>(`/api/playlists?playlistGroupId=${playlistGroupId}`);
|
|
}
|
|
|
|
export function getPlaylistById(id: number): Promise<Playlist> {
|
|
return request<Playlist>(`/api/playlists/${id}`);
|
|
}
|
|
|
|
export function getPlaylistItems(id: number): Promise<PlaylistItem[]> {
|
|
return request<PlaylistItem[]>(`/api/playlists/${id}/items`);
|
|
}
|
|
|
|
/** Load playlist items together with the playlist's concurrency ETag (issue #253). */
|
|
export function getPlaylistItemsWithMeta(id: number): Promise<ResponseWithMeta<PlaylistItem[]>> {
|
|
return requestWithMeta<PlaylistItem[]>(`/api/playlists/${id}/items`);
|
|
}
|
|
|
|
export function createPlaylist(body: CreatePlaylistRequest): Promise<Playlist> {
|
|
return request<Playlist>('/api/playlists', { body, method: 'POST' });
|
|
}
|
|
|
|
// PUT = rename + replace the full item list; returns the persisted (re-indexed) items. Pass the
|
|
// last-seen ETag as `If-Match` to reject a stale overwrite with 412; the resolved value carries
|
|
// the new ETag for a subsequent save (issue #253).
|
|
export function updatePlaylist(
|
|
id: number,
|
|
body: ReplacePlaylistRequest,
|
|
ifMatch?: string | null
|
|
): Promise<ResponseWithMeta<PlaylistItem[]>> {
|
|
return requestWithMeta<PlaylistItem[]>(`/api/playlists/${id}`, {
|
|
body,
|
|
method: 'PUT',
|
|
headers: ifMatch ? { 'If-Match': ifMatch } : undefined
|
|
});
|
|
}
|
|
|
|
export function deletePlaylist(id: number): Promise<void> {
|
|
return request<void>(`/api/playlists/${id}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export function previewPlaylist(body: ReplacePlaylistRequest): Promise<PlaylistPreviewItem[]> {
|
|
return request<PlaylistPreviewItem[]>('/api/playlists/preview', { body, method: 'POST' });
|
|
}
|
|
|
|
// Adds media items (bucketed by kind) to an existing playlist. The request body is the same
|
|
// ten-array shape as AddItemsToCollectionRequest; the server returns 204 on success, 404 for a
|
|
// missing playlist, 422 for validation failures. AddItemsToPlaylistRequest is the generated type
|
|
// for this endpoint's body, but the collection-shaped request is structurally identical, so
|
|
// callers can pipe toAddItemsRequest / toAddItemsRequestFromSearch output straight in.
|
|
export function addItemsToPlaylist(playlistId: number, body: AddItemsToPlaylistRequest): Promise<void> {
|
|
return request<void>(`/api/playlists/${playlistId}/items`, { body, method: 'POST' });
|
|
}
|
|
|
|
export function messageFromPlaylistError(error: unknown, fallback = 'Unable to load playlists'): string {
|
|
if (error instanceof ApiError) {
|
|
return error.detail ?? error.message;
|
|
}
|
|
|
|
if (error instanceof Error) {
|
|
return error.message;
|
|
}
|
|
|
|
return fallback;
|
|
}
|