mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-29 19:06:24 +02:00
add custom quick-ask actions: model, migration, schemas, CRUD routes
This commit is contained in:
parent
407059ce84
commit
041401aefc
5 changed files with 218 additions and 0 deletions
|
|
@ -34,6 +34,7 @@ from .notifications_routes import router as notifications_router
|
|||
from .notion_add_connector_route import router as notion_add_connector_router
|
||||
from .podcasts_routes import router as podcasts_router
|
||||
from .public_chat_routes import router as public_chat_router
|
||||
from .quick_ask_actions_routes import router as quick_ask_actions_router
|
||||
from .rbac_routes import router as rbac_router
|
||||
from .reports_routes import router as reports_router
|
||||
from .sandbox_routes import router as sandbox_router
|
||||
|
|
@ -85,3 +86,4 @@ router.include_router(composio_router) # Composio OAuth and toolkit management
|
|||
router.include_router(public_chat_router) # Public chat sharing and cloning
|
||||
router.include_router(incentive_tasks_router) # Incentive tasks for earning free pages
|
||||
router.include_router(youtube_router) # YouTube playlist resolution
|
||||
router.include_router(quick_ask_actions_router)
|
||||
|
|
|
|||
94
surfsense_backend/app/routes/quick_ask_actions_routes.py
Normal file
94
surfsense_backend/app/routes/quick_ask_actions_routes.py
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.db import QuickAskAction, User, get_async_session
|
||||
from app.schemas.quick_ask_actions import (
|
||||
QuickAskActionCreate,
|
||||
QuickAskActionRead,
|
||||
QuickAskActionUpdate,
|
||||
)
|
||||
from app.users import current_active_user
|
||||
|
||||
router = APIRouter(tags=["Quick Ask Actions"])
|
||||
|
||||
|
||||
@router.get("/quick-ask-actions", response_model=list[QuickAskActionRead])
|
||||
async def list_actions(
|
||||
search_space_id: int | None = None,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
query = select(QuickAskAction).where(QuickAskAction.user_id == user.id)
|
||||
if search_space_id is not None:
|
||||
query = query.where(QuickAskAction.search_space_id == search_space_id)
|
||||
query = query.order_by(QuickAskAction.created_at.desc())
|
||||
result = await session.execute(query)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@router.post("/quick-ask-actions", response_model=QuickAskActionRead)
|
||||
async def create_action(
|
||||
body: QuickAskActionCreate,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
action = QuickAskAction(
|
||||
user_id=user.id,
|
||||
search_space_id=body.search_space_id,
|
||||
name=body.name,
|
||||
prompt=body.prompt,
|
||||
mode=body.mode,
|
||||
icon=body.icon,
|
||||
)
|
||||
session.add(action)
|
||||
await session.commit()
|
||||
await session.refresh(action)
|
||||
return action
|
||||
|
||||
|
||||
@router.put("/quick-ask-actions/{action_id}", response_model=QuickAskActionRead)
|
||||
async def update_action(
|
||||
action_id: int,
|
||||
body: QuickAskActionUpdate,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
result = await session.execute(
|
||||
select(QuickAskAction).where(
|
||||
QuickAskAction.id == action_id,
|
||||
QuickAskAction.user_id == user.id,
|
||||
)
|
||||
)
|
||||
action = result.scalar_one_or_none()
|
||||
if not action:
|
||||
raise HTTPException(status_code=404, detail="Action not found")
|
||||
|
||||
for field, value in body.model_dump(exclude_unset=True).items():
|
||||
setattr(action, field, value)
|
||||
|
||||
session.add(action)
|
||||
await session.commit()
|
||||
await session.refresh(action)
|
||||
return action
|
||||
|
||||
|
||||
@router.delete("/quick-ask-actions/{action_id}")
|
||||
async def delete_action(
|
||||
action_id: int,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
user: User = Depends(current_active_user),
|
||||
):
|
||||
result = await session.execute(
|
||||
select(QuickAskAction).where(
|
||||
QuickAskAction.id == action_id,
|
||||
QuickAskAction.user_id == user.id,
|
||||
)
|
||||
)
|
||||
action = result.scalar_one_or_none()
|
||||
if not action:
|
||||
raise HTTPException(status_code=404, detail="Action not found")
|
||||
|
||||
await session.delete(action)
|
||||
await session.commit()
|
||||
return {"success": True}
|
||||
Loading…
Add table
Add a link
Reference in a new issue