fix: return 400 on duplicate vlan_id in update_vlan

update_vlan now checks for vlan_id conflicts (excluding the current
record) before committing, matching the behaviour of create_vlan and
preventing an unhandled IntegrityError 500.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-18 18:05:06 +02:00
parent d5a90f6f1d
commit 28e7a3e3d2
+8
View File
@@ -85,6 +85,14 @@ def update_vlan(vlan_pk: int, vlan: VlanCreate, db: Session = Depends(get_db)):
db_vlan = db.query(models.Vlan).filter(models.Vlan.id == vlan_pk).first()
if not db_vlan:
raise HTTPException(status_code=404, detail="VLAN introuvable")
if vlan.vlan_id is not None:
conflict = (
db.query(models.Vlan)
.filter(models.Vlan.vlan_id == vlan.vlan_id, models.Vlan.id != vlan_pk)
.first()
)
if conflict:
raise HTTPException(status_code=400, detail=f"VLAN {vlan.vlan_id} existe déjà")
for k, v in vlan.model_dump().items():
setattr(db_vlan, k, v)
db.commit()