""" Simple test API for variable management """ from typing import List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from toardb.variables import crud, schemas from toardb.utils.database import ToarDbSession, get_db router= APIRouter() # plain views to post and get variables @router.post('/variables/', response_model=schemas.Variable) def create_variable(variable: schemas.VariableCreate, db: Session = Depends(get_db)): db_variable = crud.get_variable_by_name(db, name=variable.name) if db_variable: raise HTTPException(status_code=400, detail="Variable already registered.") return crud.create_variable(db=db, variable=variable) @router.get('/variables/', response_model=List[schemas.Variable]) def get_variables(skip: int = 0, limit: int = None, db: Session = Depends(get_db)): variables = crud.get_variables(db, skip=skip, limit=limit) return variables @router.get('/variables/{name}', response_model=schemas.Variable) def get_variable(name: str, db: Session = Depends(get_db)): db_variable = crud.get_variable_by_name(db, name=name) if db_variable is None: raise HTTPException(status_code=404, detail="Variable not found.") return db_variable @router.get('/variables/id/{variable_id}', response_model=schemas.Variable) def get_variable(variable_id: int, db: Session = Depends(get_db)): db_variable = crud.get_variable(db, variable_id=variable_id) if db_variable is None: raise HTTPException(status_code=404, detail="Variable not found.") return db_variable