Skip to content

settings

create_setting(payload, stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.WRITE)))

Create a new setting in the database

Source code in cat/routes/settings.py
@router.post("/")
def create_setting(
    payload: models.SettingBody,
    stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.WRITE)),
):
    """Create a new setting in the database"""

    # complete the payload with setting_id and updated_at
    payload = models.Setting(**payload.model_dump())

    # save to DB
    new_setting = crud.create_setting(payload)

    return {"setting": new_setting}

delete_setting(settingId, stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.DELETE)))

Delete a specific setting in the database

Source code in cat/routes/settings.py
@router.delete("/{settingId}")
def delete_setting(
    settingId: str,
    stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.DELETE)),
):
    """Delete a specific setting in the database"""

    # does the setting exist?
    setting = crud.get_setting_by_id(settingId)
    if not setting:
        raise HTTPException(
            status_code=404,
            detail={
                "error": f"No setting with this id: {settingId}",
            },
        )

    # delete
    crud.delete_setting_by_id(settingId)

    return {"deleted": settingId}

get_setting(settingId, stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.READ)))

Get the a specific setting from the database

Source code in cat/routes/settings.py
@router.get("/{settingId}")
def get_setting(
    settingId: str, stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.READ))
):
    """Get the a specific setting from the database"""

    setting = crud.get_setting_by_id(settingId)
    if not setting:
        raise HTTPException(
            status_code=404,
            detail={
                "error": f"No setting with this id: {settingId}",
            },
        )
    return {"setting": setting}

get_settings(search='', stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.LIST)))

Get the entire list of settings available in the database

Source code in cat/routes/settings.py
@router.get("/")
def get_settings(
    search: str = "",
    stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.LIST)),
):
    """Get the entire list of settings available in the database"""

    settings = crud.get_settings(search=search)

    return {"settings": settings}

update_setting(settingId, payload, stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.EDIT)))

Update a specific setting in the database if it exists

Source code in cat/routes/settings.py
@router.put("/{settingId}")
def update_setting(
    settingId: str,
    payload: models.SettingBody,
    stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.EDIT)),
):
    """Update a specific setting in the database if it exists"""

    # does the setting exist?
    setting = crud.get_setting_by_id(settingId)
    if not setting:
        raise HTTPException(
            status_code=404,
            detail={
                "error": f"No setting with this id: {settingId}",
            },
        )

    # complete the payload with setting_id and updated_at
    payload = models.Setting(**payload.model_dump())
    payload.setting_id = settingId  # force this to be the setting_id

    # save to DB
    updated_setting = crud.update_setting_by_id(payload)

    return {"setting": updated_setting}