65 lines
2.9 KiB
Python
65 lines
2.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app import models, schemas
|
|
from app.auth import get_current_user
|
|
from app.database import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _owner_config(config_id: int, db: Session, user: models.User) -> models.Config:
|
|
config = db.query(models.Config).filter(
|
|
models.Config.id == config_id, models.Config.owner_id == user.id
|
|
).first()
|
|
if not config:
|
|
raise HTTPException(status_code=404, detail="Config not found")
|
|
return config
|
|
|
|
|
|
@router.get("/{config_id}/interfaces", response_model=list[schemas.InterfaceOut])
|
|
def list_interfaces(config_id: int, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
|
_owner_config(config_id, db, user)
|
|
return db.query(models.Interface).filter(models.Interface.config_id == config_id).all()
|
|
|
|
|
|
@router.post("/{config_id}/interfaces", response_model=schemas.InterfaceOut, status_code=201)
|
|
def create_interface(config_id: int, body: schemas.InterfaceCreate, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
|
_owner_config(config_id, db, user)
|
|
iface = models.Interface(**body.model_dump(), config_id=config_id)
|
|
db.add(iface)
|
|
db.commit()
|
|
db.refresh(iface)
|
|
return iface
|
|
|
|
|
|
@router.get("/{config_id}/interfaces/{interface_id}", response_model=schemas.InterfaceOut)
|
|
def get_interface(config_id: int, interface_id: int, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
|
_owner_config(config_id, db, user)
|
|
iface = db.query(models.Interface).filter(models.Interface.id == interface_id, models.Interface.config_id == config_id).first()
|
|
if not iface:
|
|
raise HTTPException(status_code=404, detail="Interface not found")
|
|
return iface
|
|
|
|
|
|
@router.put("/{config_id}/interfaces/{interface_id}", response_model=schemas.InterfaceOut)
|
|
def update_interface(config_id: int, interface_id: int, body: schemas.InterfaceUpdate, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
|
_owner_config(config_id, db, user)
|
|
iface = db.query(models.Interface).filter(models.Interface.id == interface_id, models.Interface.config_id == config_id).first()
|
|
if not iface:
|
|
raise HTTPException(status_code=404, detail="Interface not found")
|
|
for field, value in body.model_dump(exclude_none=True).items():
|
|
setattr(iface, field, value)
|
|
db.commit()
|
|
db.refresh(iface)
|
|
return iface
|
|
|
|
|
|
@router.delete("/{config_id}/interfaces/{interface_id}", status_code=204)
|
|
def delete_interface(config_id: int, interface_id: int, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
|
_owner_config(config_id, db, user)
|
|
iface = db.query(models.Interface).filter(models.Interface.id == interface_id, models.Interface.config_id == config_id).first()
|
|
if not iface:
|
|
raise HTTPException(status_code=404, detail="Interface not found")
|
|
db.delete(iface)
|
|
db.commit()
|