111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
# 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, PlantVariety, PlantWithVarieties
|
|
|
|
router = APIRouter(tags=["plantes"])
|
|
|
|
|
|
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.id)
|
|
if categorie:
|
|
q = q.where(Plant.categorie == categorie)
|
|
return [_with_varieties(p, session) for p in session.exec(q).all()]
|
|
|
|
|
|
@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 _with_varieties(p, session)
|
|
|
|
|
|
@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 _with_varieties(p, session)
|
|
|
|
|
|
@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:
|
|
raise HTTPException(404, "Plante introuvable")
|
|
for k, v in data.model_dump(exclude_unset=True, exclude={"id", "created_at"}).items():
|
|
setattr(p, k, v)
|
|
session.add(p)
|
|
session.commit()
|
|
session.refresh(p)
|
|
return _with_varieties(p, session)
|
|
|
|
|
|
@router.delete("/plants/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
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()
|