From 17879b1cfbdf964b2417edf2e74eb4db90011846 Mon Sep 17 00:00:00 2001 From: Gulum Date: Sat, 6 Jun 2026 00:25:00 +0200 Subject: [PATCH] FEAT-6: typed API clients for health-records, weight-records, gerbil photos (multipart upload) --- gerbil-manager-web/src/api/healthRecords.ts | 68 +++++++++++++++++++ gerbil-manager-web/src/api/photos.ts | 72 +++++++++++++++++++++ gerbil-manager-web/src/api/weightRecords.ts | 43 ++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 gerbil-manager-web/src/api/healthRecords.ts create mode 100644 gerbil-manager-web/src/api/photos.ts create mode 100644 gerbil-manager-web/src/api/weightRecords.ts diff --git a/gerbil-manager-web/src/api/healthRecords.ts b/gerbil-manager-web/src/api/healthRecords.ts new file mode 100644 index 0000000..21fc434 --- /dev/null +++ b/gerbil-manager-web/src/api/healthRecords.ts @@ -0,0 +1,68 @@ +/** Typed API functions for the Gesundheit (HealthRecord) resource. (FEAT-6) */ +import { api } from './client' +import { condition, toQueryString } from './gridify' +import type { DateOnlyString, Paged } from './types' + +const RESOURCE = '/health-records' + +/** HealthRecord.Type — C# enum member names (JsonStringEnumConverter). */ +export type HealthRecordType = + | 'Examination' + | 'Treatment' + | 'Injury' + | 'Vaccination' + | 'Other' +export const HEALTH_RECORD_TYPES: HealthRecordType[] = [ + 'Examination', + 'Treatment', + 'Injury', + 'Vaccination', + 'Other', +] + +export interface HealthRecord { + id: string + gerbilId: string + date: DateOnlyString + type: HealthRecordType + description: string + veterinarian: string | null + createdAt: string +} + +/** Payload for POST /health-records. */ +export interface CreateHealthRecord { + gerbilId: string + date: DateOnlyString + type: HealthRecordType + description: string + veterinarian?: string | null +} + +/** Payload for PUT /health-records/{id}. */ +export type UpdateHealthRecord = Partial + +/** Alle Einträge eines Tieres, neueste zuerst. */ +export async function listHealthRecords(gerbilId: string): Promise { + const paged = await api.get>( + `${RESOURCE}${toQueryString({ + filter: condition({ field: 'gerbilId', op: '==', value: gerbilId }), + orderBy: 'date desc', + page: 1, + pageSize: 500, + })}`, + ) + return paged.items +} + +export function createHealthRecord(body: CreateHealthRecord): Promise { + return api.post(RESOURCE, body) +} + +export function updateHealthRecord(id: string, body: UpdateHealthRecord): Promise { + return api.put(`${RESOURCE}/${id}`, body) +} + +export function deleteHealthRecord(id: string): Promise { + return api.delete(`${RESOURCE}/${id}`) +} diff --git a/gerbil-manager-web/src/api/photos.ts b/gerbil-manager-web/src/api/photos.ts new file mode 100644 index 0000000..bd9785a --- /dev/null +++ b/gerbil-manager-web/src/api/photos.ts @@ -0,0 +1,72 @@ +/** + * Typed API functions for Fotos (GerbilPhoto). (FEAT-6) + * + * Contract (FEAT-1b Phase 2, Pam): + * GET /gerbils/{id}/photos -> [{id, fileName, caption, sortOrder, url}] + * POST /gerbils/{id}/photos -> multipart, Feld 'file' (+ optional 'caption') + * DELETE /photos/{id} + * Erstes Foto nach sortOrder = Profilfoto. + * + * Der Upload geht NICHT über api.post (das setzt Content-Type: application/json); + * bei FormData muss der Browser den multipart-Boundary-Header selbst setzen. + */ +import { API_BASE_URL, ApiError, api } from './client' +import { de } from '../strings/de' + +export interface GerbilPhoto { + id: string + fileName: string + caption: string | null + sortOrder: number + /** Absoluter oder API-relativer Pfad zur Bilddatei. */ + url: string +} + +export function listGerbilPhotos(gerbilId: string): Promise { + return api.get(`/gerbils/${gerbilId}/photos`) +} + +export async function uploadGerbilPhoto( + gerbilId: string, + file: File, + caption: string | null, +): Promise { + const form = new FormData() + form.append('file', file) + if (caption) form.append('caption', caption) + + let response: Response + try { + response = await fetch(`${API_BASE_URL}/gerbils/${gerbilId}/photos`, { + method: 'POST', + body: form, + }) + } catch { + throw new ApiError(de.api.errors.network, null) + } + if (!response.ok) { + const message = + response.status === 404 + ? de.api.errors.notFound + : response.status >= 500 + ? de.api.errors.server + : de.api.errors.unknown + throw new ApiError(message, response.status) + } + return (await response.json()) as GerbilPhoto +} + +export function deleteGerbilPhoto(photoId: string): Promise { + return api.delete(`/photos/${photoId}`) +} + +/** Bild-URL auflösen: API liefert ggf. einen API-relativen Pfad. */ +export function photoSrc(photo: GerbilPhoto): string { + return photo.url.startsWith('http') ? photo.url : `${API_BASE_URL}${photo.url}` +} + +/** Profilfoto = erstes Foto nach sortOrder (null, wenn keine Fotos). */ +export function profilePhoto(photos: GerbilPhoto[]): GerbilPhoto | null { + if (photos.length === 0) return null + return [...photos].sort((a, b) => a.sortOrder - b.sortOrder)[0] +} diff --git a/gerbil-manager-web/src/api/weightRecords.ts b/gerbil-manager-web/src/api/weightRecords.ts new file mode 100644 index 0000000..4ff00cf --- /dev/null +++ b/gerbil-manager-web/src/api/weightRecords.ts @@ -0,0 +1,43 @@ +/** Typed API functions for the Gewicht (WeightRecord) resource. (FEAT-6) */ +import { api } from './client' +import { condition, toQueryString } from './gridify' +import type { DateOnlyString, Paged } from './types' + +const RESOURCE = '/weight-records' + +export interface WeightRecord { + id: string + gerbilId: string + date: DateOnlyString + weightGrams: number + notes: string | null +} + +/** Payload for POST /weight-records. */ +export interface CreateWeightRecord { + gerbilId: string + date: DateOnlyString + weightGrams: number + notes?: string | null +} + +/** Alle Einträge eines Tieres, neueste zuerst (Chart sortiert selbst aufsteigend). */ +export async function listWeightRecords(gerbilId: string): Promise { + const paged = await api.get>( + `${RESOURCE}${toQueryString({ + filter: condition({ field: 'gerbilId', op: '==', value: gerbilId }), + orderBy: 'date desc', + page: 1, + pageSize: 1000, + })}`, + ) + return paged.items +} + +export function createWeightRecord(body: CreateWeightRecord): Promise { + return api.post(RESOURCE, body) +} + +export function deleteWeightRecord(id: string): Promise { + return api.delete(`${RESOURCE}/${id}`) +}