From 05b2ddc27c1902a290a850505885d47d73acc1a9 Mon Sep 17 00:00:00 2001 From: gilles Date: Sun, 8 Mar 2026 19:29:58 +0100 Subject: [PATCH] =?UTF-8?q?feat(plantes):=20store=20plants=20=E2=80=94=20a?= =?UTF-8?q?ctions=20variety=20CRUD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/stores/plants.ts | 40 +++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/frontend/src/stores/plants.ts b/frontend/src/stores/plants.ts index f6479b2..35e590f 100644 --- a/frontend/src/stores/plants.ts +++ b/frontend/src/stores/plants.ts @@ -1,6 +1,7 @@ +// frontend/src/stores/plants.ts import { defineStore } from 'pinia' import { ref } from 'vue' -import { plantsApi, type Plant } from '@/api/plants' +import { plantsApi, type Plant, type PlantVariety } from '@/api/plants' export const usePlantsStore = defineStore('plants', () => { const plants = ref([]) @@ -18,10 +19,45 @@ export const usePlantsStore = defineStore('plants', () => { return created } + async function update(id: number, p: Partial) { + const updated = await plantsApi.update(id, p) + const idx = plants.value.findIndex(x => x.id === id) + if (idx !== -1) plants.value[idx] = updated + return updated + } + async function remove(id: number) { await plantsApi.delete(id) plants.value = plants.value.filter(p => p.id !== id) } - return { plants, loading, fetchAll, create, remove } + async function createVariety(plantId: number, v: Partial) { + const created = await plantsApi.createVariety(plantId, v) + const plant = plants.value.find(p => p.id === plantId) + if (plant) { + if (!plant.varieties) plant.varieties = [] + plant.varieties.push(created) + } + return created + } + + async function updateVariety(plantId: number, vid: number, v: Partial) { + const updated = await plantsApi.updateVariety(plantId, vid, v) + const plant = plants.value.find(p => p.id === plantId) + if (plant?.varieties) { + const idx = plant.varieties.findIndex(x => x.id === vid) + if (idx !== -1) plant.varieties[idx] = updated + } + return updated + } + + async function removeVariety(plantId: number, vid: number) { + await plantsApi.deleteVariety(plantId, vid) + const plant = plants.value.find(p => p.id === plantId) + if (plant?.varieties) { + plant.varieties = plant.varieties.filter(v => v.id !== vid) + } + } + + return { plants, loading, fetchAll, create, update, remove, createVariety, updateVariety, removeVariety } })