feat: add hosts and params files, fix rules SECTION NEW header
All checks were successful
Build containers when image tags change / build-if-image-changed (backend, shorefront-backend, shorefront backend, backend/Dockerfile, git.baumann.gr/adebaumann/shorefront-backend, .backend.image) (push) Successful in 44s
Build containers when image tags change / build-if-image-changed (frontend, shorefront-frontend, shorefront frontend, frontend/Dockerfile, git.baumann.gr/adebaumann/shorefront-frontend, .frontend.image) (push) Successful in 1m32s
All checks were successful
Build containers when image tags change / build-if-image-changed (backend, shorefront-backend, shorefront backend, backend/Dockerfile, git.baumann.gr/adebaumann/shorefront-backend, .backend.image) (push) Successful in 44s
Build containers when image tags change / build-if-image-changed (frontend, shorefront-frontend, shorefront frontend, frontend/Dockerfile, git.baumann.gr/adebaumann/shorefront-frontend, .frontend.image) (push) Successful in 1m32s
This commit is contained in:
64
backend/app/api/hosts.py
Normal file
64
backend/app/api/hosts.py
Normal file
@@ -0,0 +1,64 @@
|
||||
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}/hosts", response_model=list[schemas.HostOut])
|
||||
def list_hosts(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.Host).filter(models.Host.config_id == config_id).all()
|
||||
|
||||
|
||||
@router.post("/{config_id}/hosts", response_model=schemas.HostOut, status_code=201)
|
||||
def create_host(config_id: int, body: schemas.HostCreate, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
||||
_owner_config(config_id, db, user)
|
||||
host = models.Host(**body.model_dump(), config_id=config_id)
|
||||
db.add(host)
|
||||
db.commit()
|
||||
db.refresh(host)
|
||||
return host
|
||||
|
||||
|
||||
@router.get("/{config_id}/hosts/{host_id}", response_model=schemas.HostOut)
|
||||
def get_host(config_id: int, host_id: int, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
||||
_owner_config(config_id, db, user)
|
||||
host = db.query(models.Host).filter(models.Host.id == host_id, models.Host.config_id == config_id).first()
|
||||
if not host:
|
||||
raise HTTPException(status_code=404, detail="Host entry not found")
|
||||
return host
|
||||
|
||||
|
||||
@router.put("/{config_id}/hosts/{host_id}", response_model=schemas.HostOut)
|
||||
def update_host(config_id: int, host_id: int, body: schemas.HostUpdate, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
||||
_owner_config(config_id, db, user)
|
||||
host = db.query(models.Host).filter(models.Host.id == host_id, models.Host.config_id == config_id).first()
|
||||
if not host:
|
||||
raise HTTPException(status_code=404, detail="Host entry not found")
|
||||
for field, value in body.model_dump(exclude_none=True).items():
|
||||
setattr(host, field, value)
|
||||
db.commit()
|
||||
db.refresh(host)
|
||||
return host
|
||||
|
||||
|
||||
@router.delete("/{config_id}/hosts/{host_id}", status_code=204)
|
||||
def delete_host(config_id: int, host_id: int, db: Session = Depends(get_db), user: models.User = Depends(get_current_user)):
|
||||
_owner_config(config_id, db, user)
|
||||
host = db.query(models.Host).filter(models.Host.id == host_id, models.Host.config_id == config_id).first()
|
||||
if not host:
|
||||
raise HTTPException(status_code=404, detail="Host entry not found")
|
||||
db.delete(host)
|
||||
db.commit()
|
||||
Reference in New Issue
Block a user