Files
Showy/react_frontend/src/api/collections.ts
T

48 lines
1.4 KiB
TypeScript

import { api } from "@/lib/api";
import { normalizeCollection } from "@/lib/normalizers";
import type { Collection } from "@/types";
export const collectionsApi = {
getAll: () =>
api
.get<Collection[]>("/collections")
.then((r) => r.data.map(normalizeCollection)),
getById: (id: number) =>
api
.get<Collection>(`/collections/${id}`)
.then((r) => normalizeCollection(r.data)),
create: (data: Partial<Collection>) =>
api.post<Collection>("/collections", data).then((r) => r.data),
update: (id: number, data: Partial<Collection>) =>
api
.put<Collection>(`/collections/${id}`, data)
.then((r) => normalizeCollection(r.data)),
updateWithMedia: (id: number, formData: FormData) =>
api
.put<Collection>(`/collections/${id}`, formData, {
headers: { "Content-Type": "multipart/form-data" },
})
.then((r) => normalizeCollection(r.data)),
delete: (id: number) => api.delete(`/collections/${id}`).then((r) => r.data),
getCollectionParts: (id: number) =>
api
.put<Collection>(`/collections/${id}/parts`)
.then((r) => normalizeCollection(r.data)),
addEntry: (collectionId: number, entryId: number) =>
api
.post(`/collections/${collectionId}/entries/${entryId}`, {})
.then((r) => r.data),
removeEntry: (collectionId: number, entryId: number) =>
api
.delete(`/collections/${collectionId}/entries/${entryId}`)
.then((r) => r.data),
};