diff --git a/backend/app/routers/plants.py b/backend/app/routers/plants.py index 3560f93..39860c7 100644 --- a/backend/app/routers/plants.py +++ b/backend/app/routers/plants.py @@ -1,40 +1,50 @@ +# backend/app/routers/plants.py from typing import List, Optional from fastapi import APIRouter, Depends, HTTPException, Query, status from sqlmodel import Session, select from app.database import get_session -from app.models.plant import Plant +from app.models.plant import Plant, PlantVariety, PlantWithVarieties router = APIRouter(tags=["plantes"]) -@router.get("/plants", response_model=List[Plant]) +def _with_varieties(p: Plant, session: Session) -> PlantWithVarieties: + varieties = session.exec( + select(PlantVariety).where(PlantVariety.plant_id == p.id) + ).all() + data = p.model_dump() + data["varieties"] = [v.model_dump() for v in varieties] + return PlantWithVarieties(**data) + + +@router.get("/plants", response_model=List[PlantWithVarieties]) def list_plants( categorie: Optional[str] = Query(None), session: Session = Depends(get_session), ): - q = select(Plant).order_by(Plant.nom_commun, Plant.variete, Plant.id) + q = select(Plant).order_by(Plant.nom_commun, Plant.id) if categorie: q = q.where(Plant.categorie == categorie) - return session.exec(q).all() + return [_with_varieties(p, session) for p in session.exec(q).all()] -@router.post("/plants", response_model=Plant, status_code=status.HTTP_201_CREATED) +@router.post("/plants", response_model=PlantWithVarieties, status_code=status.HTTP_201_CREATED) def create_plant(p: Plant, session: Session = Depends(get_session)): session.add(p) session.commit() session.refresh(p) - return p + return _with_varieties(p, session) -@router.get("/plants/{id}", response_model=Plant) +@router.get("/plants/{id}", response_model=PlantWithVarieties) def get_plant(id: int, session: Session = Depends(get_session)): p = session.get(Plant, id) if not p: raise HTTPException(404, "Plante introuvable") - return p + return _with_varieties(p, session) -@router.put("/plants/{id}", response_model=Plant) +@router.put("/plants/{id}", response_model=PlantWithVarieties) def update_plant(id: int, data: Plant, session: Session = Depends(get_session)): p = session.get(Plant, id) if not p: @@ -44,7 +54,7 @@ def update_plant(id: int, data: Plant, session: Session = Depends(get_session)): session.add(p) session.commit() session.refresh(p) - return p + return _with_varieties(p, session) @router.delete("/plants/{id}", status_code=status.HTTP_204_NO_CONTENT) @@ -52,5 +62,49 @@ def delete_plant(id: int, session: Session = Depends(get_session)): p = session.get(Plant, id) if not p: raise HTTPException(404, "Plante introuvable") + for v in session.exec(select(PlantVariety).where(PlantVariety.plant_id == id)).all(): + session.delete(v) session.delete(p) session.commit() + + +# ---- CRUD Variétés ---- + +@router.get("/plants/{id}/varieties", response_model=List[PlantVariety]) +def list_varieties(id: int, session: Session = Depends(get_session)): + if not session.get(Plant, id): + raise HTTPException(404, "Plante introuvable") + return session.exec(select(PlantVariety).where(PlantVariety.plant_id == id)).all() + + +@router.post("/plants/{id}/varieties", response_model=PlantVariety, status_code=status.HTTP_201_CREATED) +def create_variety(id: int, v: PlantVariety, session: Session = Depends(get_session)): + if not session.get(Plant, id): + raise HTTPException(404, "Plante introuvable") + v.plant_id = id + session.add(v) + session.commit() + session.refresh(v) + return v + + +@router.put("/plants/{id}/varieties/{vid}", response_model=PlantVariety) +def update_variety(id: int, vid: int, data: PlantVariety, session: Session = Depends(get_session)): + v = session.get(PlantVariety, vid) + if not v or v.plant_id != id: + raise HTTPException(404, "Variété introuvable") + for k, val in data.model_dump(exclude_unset=True, exclude={"id", "plant_id", "created_at"}).items(): + setattr(v, k, val) + session.add(v) + session.commit() + session.refresh(v) + return v + + +@router.delete("/plants/{id}/varieties/{vid}", status_code=status.HTTP_204_NO_CONTENT) +def delete_variety(id: int, vid: int, session: Session = Depends(get_session)): + v = session.get(PlantVariety, vid) + if not v or v.plant_id != id: + raise HTTPException(404, "Variété introuvable") + session.delete(v) + session.commit()