feat(intrants): CRUD + statut router for fabrications
This commit is contained in:
78
backend/app/routers/fabrications.py
Normal file
78
backend/app/routers/fabrications.py
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
# backend/app/routers/fabrications.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.intrant import Fabrication, FabricationStatutUpdate
|
||||||
|
|
||||||
|
router = APIRouter(tags=["intrants"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/fabrications", response_model=List[Fabrication])
|
||||||
|
def list_fabrications(
|
||||||
|
type: Optional[str] = Query(None),
|
||||||
|
statut: Optional[str] = Query(None),
|
||||||
|
jardin_id: Optional[int] = Query(None),
|
||||||
|
session: Session = Depends(get_session),
|
||||||
|
):
|
||||||
|
q = select(Fabrication)
|
||||||
|
if type:
|
||||||
|
q = q.where(Fabrication.type == type)
|
||||||
|
if statut:
|
||||||
|
q = q.where(Fabrication.statut == statut)
|
||||||
|
if jardin_id:
|
||||||
|
q = q.where(Fabrication.jardin_id == jardin_id)
|
||||||
|
return session.exec(q.order_by(Fabrication.created_at.desc())).all()
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/fabrications", response_model=Fabrication, status_code=status.HTTP_201_CREATED)
|
||||||
|
def create_fabrication(f: Fabrication, session: Session = Depends(get_session)):
|
||||||
|
session.add(f)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(f)
|
||||||
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/fabrications/{id}", response_model=Fabrication)
|
||||||
|
def get_fabrication(id: int, session: Session = Depends(get_session)):
|
||||||
|
f = session.get(Fabrication, id)
|
||||||
|
if not f:
|
||||||
|
raise HTTPException(404, "Fabrication introuvable")
|
||||||
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/fabrications/{id}", response_model=Fabrication)
|
||||||
|
def update_fabrication(id: int, data: Fabrication, session: Session = Depends(get_session)):
|
||||||
|
f = session.get(Fabrication, id)
|
||||||
|
if not f:
|
||||||
|
raise HTTPException(404, "Fabrication introuvable")
|
||||||
|
for k, v in data.model_dump(exclude_unset=True, exclude={"id", "created_at"}).items():
|
||||||
|
setattr(f, k, v)
|
||||||
|
session.add(f)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(f)
|
||||||
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/fabrications/{id}/statut", response_model=Fabrication)
|
||||||
|
def update_statut(id: int, data: FabricationStatutUpdate, session: Session = Depends(get_session)):
|
||||||
|
f = session.get(Fabrication, id)
|
||||||
|
if not f:
|
||||||
|
raise HTTPException(404, "Fabrication introuvable")
|
||||||
|
valid = {"en_cours", "pret", "utilise", "echec"}
|
||||||
|
if data.statut not in valid:
|
||||||
|
raise HTTPException(400, f"Statut invalide. Valeurs: {valid}")
|
||||||
|
f.statut = data.statut
|
||||||
|
session.add(f)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(f)
|
||||||
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/fabrications/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
def delete_fabrication(id: int, session: Session = Depends(get_session)):
|
||||||
|
f = session.get(Fabrication, id)
|
||||||
|
if not f:
|
||||||
|
raise HTTPException(404, "Fabrication introuvable")
|
||||||
|
session.delete(f)
|
||||||
|
session.commit()
|
||||||
Reference in New Issue
Block a user