diff --git a/frontend/services/clients.ts b/frontend/services/clients.ts new file mode 100644 index 0000000..6a100be --- /dev/null +++ b/frontend/services/clients.ts @@ -0,0 +1,32 @@ +import type { Client, ClientWrite } from './dto/client' +import type { HydraCollection } from '~/utils/api' +import { extractHydraMembers } from '~/utils/api' + +export function useClientService() { + const api = useApi() + + async function getAll(): Promise { + const data = await api.get>('/clients') + return extractHydraMembers(data) + } + + async function create(payload: ClientWrite): Promise { + return api.post('/clients', payload as Record, { + toastSuccessKey: 'clients.created', + }) + } + + async function update(id: number, payload: Partial): Promise { + return api.patch(`/clients/${id}`, payload as Record, { + toastSuccessKey: 'clients.updated', + }) + } + + async function remove(id: number): Promise { + await api.delete(`/clients/${id}`, {}, { + toastSuccessKey: 'clients.deleted', + }) + } + + return { getAll, create, update, remove } +} diff --git a/frontend/services/dto/client.ts b/frontend/services/dto/client.ts new file mode 100644 index 0000000..191931a --- /dev/null +++ b/frontend/services/dto/client.ts @@ -0,0 +1,19 @@ +export type Client = { + id: number + '@id'?: string + name: string + email: string | null + phone: string | null + street: string | null + city: string | null + postalCode: string | null +} + +export type ClientWrite = { + name: string + email: string | null + phone: string | null + street: string | null + city: string | null + postalCode: string | null +} diff --git a/frontend/utils/api.ts b/frontend/utils/api.ts new file mode 100644 index 0000000..6a220e1 --- /dev/null +++ b/frontend/utils/api.ts @@ -0,0 +1,8 @@ +export type HydraCollection = { + 'hydra:member': T[] + 'hydra:totalItems': number +} + +export function extractHydraMembers(response: HydraCollection): T[] { + return response['hydra:member'] ?? [] +}