gateway/gwserver/routes/workspaces.py
2025-03-23 01:19:05 +01:00

133 lines
4.6 KiB
Python

from fastapi import APIRouter, HTTPException, Depends, Body, Path
from typing import List, Dict, Any
from fastapi import status
# Import auth module
from auth import get_current_active_user, get_user_context
# Import interfaces
from modules.lucydom_interface import get_lucydom_interface
# Router für Workspace-Endpunkte erstellen
router = APIRouter(
prefix="/api/workspaces",
tags=["Workspaces"],
responses={404: {"description": "Not found"}}
)
@router.get("", response_model=List[Dict[str, Any]])
async def get_workspaces(current_user: Dict[str, Any] = Depends(get_current_active_user)):
"""Alle verfügbaren Workspaces abrufen"""
mandate_id, user_id = await get_user_context(current_user)
# LucyDOM-Interface mit Benutzerkontext initialisieren
lucy_interface = get_lucydom_interface(mandate_id, user_id)
return lucy_interface.get_all_workspaces()
@router.get("/{workspace_id}", response_model=Dict[str, Any])
async def get_workspace(
workspace_id: int,
current_user: Dict[str, Any] = Depends(get_current_active_user)
):
"""Einen bestimmten Workspace mit allen Details abrufen"""
mandate_id, user_id = await get_user_context(current_user)
# LucyDOM-Interface mit Benutzerkontext initialisieren
lucy_interface = get_lucydom_interface(mandate_id, user_id)
workspace = lucy_interface.get_workspace(workspace_id)
if not workspace:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Workspace mit ID {workspace_id} nicht gefunden"
)
return workspace
@router.post("", response_model=Dict[str, Any])
async def create_workspace(
workspace: Dict[str, Any] = Body(...),
current_user: Dict[str, Any] = Depends(get_current_active_user)
):
"""Einen neuen Workspace erstellen"""
mandate_id, user_id = await get_user_context(current_user)
# LucyDOM-Interface mit Benutzerkontext initialisieren
lucy_interface = get_lucydom_interface(mandate_id, user_id)
new_workspace = lucy_interface.create_workspace(name=workspace.get("name", "Neuer Workspace"))
return new_workspace
@router.put("/{workspace_id}", response_model=Dict[str, Any])
async def update_workspace(
workspace_id: int,
workspace: Dict[str, Any] = Body(...),
current_user: Dict[str, Any] = Depends(get_current_active_user)
):
"""Einen bestehenden Workspace aktualisieren"""
mandate_id, user_id = await get_user_context(current_user)
# LucyDOM-Interface mit Benutzerkontext initialisieren
lucy_interface = get_lucydom_interface(mandate_id, user_id)
# Prüfe, ob der Workspace existiert
existing_workspace = lucy_interface.get_workspace(workspace_id)
if not existing_workspace:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Workspace mit ID {workspace_id} nicht gefunden"
)
updated_workspace = lucy_interface.update_workspace(
workspace_id=workspace_id,
name=workspace.get("name", existing_workspace.get("name"))
)
if not updated_workspace:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Fehler beim Aktualisieren des Workspaces"
)
return updated_workspace
@router.delete("/{workspace_id}", response_model=Dict[str, Any])
async def delete_workspace(
workspace_id: int,
current_user: Dict[str, Any] = Depends(get_current_active_user)
):
"""Einen Workspace löschen"""
mandate_id, user_id = await get_user_context(current_user)
# LucyDOM-Interface mit Benutzerkontext initialisieren
lucy_interface = get_lucydom_interface(mandate_id, user_id)
# Prüfe, ob der Workspace existiert
existing_workspace = lucy_interface.get_workspace(workspace_id)
if not existing_workspace:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Workspace mit ID {workspace_id} nicht gefunden"
)
# Prüfe, ob es der initiale Workspace ist
initial_workspace_id = lucy_interface.get_initial_id("workspaces")
if initial_workspace_id == workspace_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Der Default Workspace kann nicht gelöscht werden"
)
success = lucy_interface.delete_workspace(workspace_id)
if not success:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Fehler beim Löschen des Workspaces"
)
return {"message": f"Workspace mit ID {workspace_id} wurde erfolgreich gelöscht"}