51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
"""Favorite templates shared by the home page and MCP."""
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from apps.admin_console.services.favorites import (
|
|
FavoriteCreate,
|
|
FavoriteRun,
|
|
FavoriteUpdate,
|
|
favorites,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/automation/favorites", tags=["automation"])
|
|
|
|
|
|
@router.get("")
|
|
def list_favorites():
|
|
return favorites.store.records("favorite", 10000)
|
|
|
|
|
|
@router.post("")
|
|
def create_favorite(value: FavoriteCreate):
|
|
try:
|
|
return favorites.create(value)
|
|
except (KeyError, ValueError) as exc:
|
|
raise HTTPException(409, str(exc)) from exc
|
|
|
|
|
|
@router.patch("/{key}")
|
|
def update_favorite(key: str, value: FavoriteUpdate):
|
|
try:
|
|
return favorites.update(key, value)
|
|
except (KeyError, ValueError) as exc:
|
|
raise HTTPException(409, str(exc)) from exc
|
|
|
|
|
|
@router.delete("/{key}")
|
|
def delete_favorite(key: str):
|
|
try:
|
|
favorites.delete(key)
|
|
except ValueError as exc:
|
|
raise HTTPException(400, str(exc)) from exc
|
|
return {"status": "deleted"}
|
|
|
|
|
|
@router.post("/{key}/run")
|
|
async def run_favorite(key: str, value: FavoriteRun):
|
|
try:
|
|
return await favorites.run(key, value)
|
|
except (KeyError, ValueError) as exc:
|
|
raise HTTPException(409, str(exc)) from exc
|