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:
@@ -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()
|
db_vlan = db.query(models.Vlan).filter(models.Vlan.id == vlan_pk).first()
|
||||||
if not db_vlan:
|
if not db_vlan:
|
||||||
raise HTTPException(status_code=404, detail="VLAN introuvable")
|
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():
|
for k, v in vlan.model_dump().items():
|
||||||
setattr(db_vlan, k, v)
|
setattr(db_vlan, k, v)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user