62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.core.db import get_db
|
|
from app.schemas.param import ParamOut, ParamCreate, ParamUpdate
|
|
from app.models import SystemParam
|
|
|
|
router = APIRouter(prefix="/params", tags=["params"])
|
|
|
|
|
|
@router.get("", response_model=list[ParamOut])
|
|
def list_params(db: Session = Depends(get_db)):
|
|
return db.query(SystemParam).all()
|
|
|
|
|
|
@router.post("", response_model=ParamOut)
|
|
def create_param(payload: ParamCreate, db: Session = Depends(get_db)):
|
|
exists = db.query(SystemParam).filter(SystemParam.param_key == payload.param_key).first()
|
|
if exists:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Param key exists")
|
|
item = SystemParam(
|
|
param_key=payload.param_key,
|
|
param_value=payload.param_value,
|
|
param_type=payload.param_type,
|
|
status=payload.status,
|
|
is_system=payload.is_system,
|
|
description=payload.description,
|
|
)
|
|
db.add(item)
|
|
db.commit()
|
|
db.refresh(item)
|
|
return item
|
|
|
|
|
|
@router.put("/{param_id}", response_model=ParamOut)
|
|
def update_param(param_id: int, payload: ParamUpdate, db: Session = Depends(get_db)):
|
|
item = db.query(SystemParam).filter(SystemParam.param_id == param_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Param not found")
|
|
if payload.param_value is not None:
|
|
item.param_value = payload.param_value
|
|
if payload.param_type is not None:
|
|
item.param_type = payload.param_type
|
|
if payload.status is not None:
|
|
item.status = payload.status
|
|
if payload.is_system is not None:
|
|
item.is_system = payload.is_system
|
|
if payload.description is not None:
|
|
item.description = payload.description
|
|
db.commit()
|
|
db.refresh(item)
|
|
return item
|
|
|
|
|
|
@router.delete("/{param_id}")
|
|
def delete_param(param_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(SystemParam).filter(SystemParam.param_id == param_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Param not found")
|
|
db.delete(item)
|
|
db.commit()
|
|
return {"status": "ok"}
|