diff --git a/frontend/services/dto/project.ts b/frontend/services/dto/project.ts new file mode 100644 index 0000000..06d0255 --- /dev/null +++ b/frontend/services/dto/project.ts @@ -0,0 +1,17 @@ +import type { Client } from './client' + +export type Project = { + id: number + '@id'?: string + name: string + description: string | null + color: string + client: Client | null +} + +export type ProjectWrite = { + name: string + description: string | null + color: string + client: string | null // IRI : "/api/clients/1" ou null +} diff --git a/frontend/services/projects.ts b/frontend/services/projects.ts new file mode 100644 index 0000000..852824c --- /dev/null +++ b/frontend/services/projects.ts @@ -0,0 +1,32 @@ +import type { Project, ProjectWrite } from './dto/project' +import type { HydraCollection } from '~/utils/api' +import { extractHydraMembers } from '~/utils/api' + +export function useProjectService() { + const api = useApi() + + async function getAll(): Promise { + const data = await api.get>('/projects') + return extractHydraMembers(data) + } + + async function create(payload: ProjectWrite): Promise { + return api.post('/projects', payload as Record, { + toastSuccessKey: 'projects.created', + }) + } + + async function update(id: number, payload: Partial): Promise { + return api.patch(`/projects/${id}`, payload as Record, { + toastSuccessKey: 'projects.updated', + }) + } + + async function remove(id: number): Promise { + await api.delete(`/projects/${id}`, {}, { + toastSuccessKey: 'projects.deleted', + }) + } + + return { getAll, create, update, remove } +}