128 lines
No EOL
4.1 KiB
Python
128 lines
No EOL
4.1 KiB
Python
from fastapi import APIRouter, HTTPException, Depends, Body, Query, Path, Request
|
|
from typing import List, Dict, Any, Optional
|
|
from fastapi import status
|
|
from datetime import datetime
|
|
import logging
|
|
|
|
# Import auth module
|
|
import modules.security.auth as auth
|
|
|
|
# Import interfaces
|
|
import modules.interfaces.lucydomInterface as lucydomInterface
|
|
from modules.interfaces.lucydomModel import Prompt, getModelAttributes
|
|
|
|
# Configure logger
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Model attributes for Prompt
|
|
promptAttributes = getModelAttributes(Prompt)
|
|
|
|
# Create router for prompt endpoints
|
|
router = APIRouter(
|
|
prefix="/api/prompts",
|
|
tags=["Prompts"],
|
|
responses={404: {"description": "Not found"}}
|
|
)
|
|
|
|
@router.get("", response_model=List[Prompt])
|
|
async def get_prompts(
|
|
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
|
|
):
|
|
"""Get all prompts"""
|
|
interfaceLucydom = lucydomInterface.getInterface(currentUser)
|
|
prompts = interfaceLucydom.getAllPrompts()
|
|
return [Prompt(**prompt) for prompt in prompts]
|
|
|
|
@router.post("", response_model=Prompt)
|
|
async def create_prompt(
|
|
prompt: Prompt,
|
|
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
|
|
):
|
|
"""Create a new prompt"""
|
|
interfaceLucydom = lucydomInterface.getInterface(currentUser)
|
|
|
|
# Convert Prompt to dict for interface
|
|
prompt_data = prompt.model_dump()
|
|
|
|
# Create prompt
|
|
newPrompt = interfaceLucydom.createPrompt(prompt_data)
|
|
|
|
# Set current time for createdAt if it exists in the model
|
|
if "createdAt" in promptAttributes and hasattr(newPrompt, "createdAt"):
|
|
newPrompt["createdAt"] = datetime.now().isoformat()
|
|
|
|
return Prompt(**newPrompt)
|
|
|
|
@router.get("/{promptId}", response_model=Prompt)
|
|
async def get_prompt(
|
|
promptId: str = Path(..., description="ID of the prompt"),
|
|
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
|
|
):
|
|
"""Get a specific prompt"""
|
|
interfaceLucydom = lucydomInterface.getInterface(currentUser)
|
|
|
|
# Get prompt
|
|
prompt = interfaceLucydom.getPrompt(promptId)
|
|
if not prompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Prompt with ID {promptId} not found"
|
|
)
|
|
|
|
return Prompt(**prompt)
|
|
|
|
@router.put("/{promptId}", response_model=Prompt)
|
|
async def update_prompt(
|
|
promptId: str = Path(..., description="ID of the prompt to update"),
|
|
promptData: Prompt = Body(...),
|
|
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
|
|
):
|
|
"""Update an existing prompt"""
|
|
interfaceLucydom = lucydomInterface.getInterface(currentUser)
|
|
|
|
# Check if the prompt exists
|
|
existingPrompt = interfaceLucydom.getPrompt(promptId)
|
|
if not existingPrompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Prompt with ID {promptId} not found"
|
|
)
|
|
|
|
# Convert Prompt to dict for interface
|
|
update_data = promptData.model_dump()
|
|
|
|
# Update prompt
|
|
updatedPrompt = interfaceLucydom.updatePrompt(promptId, update_data)
|
|
|
|
if not updatedPrompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Error updating the prompt"
|
|
)
|
|
|
|
return Prompt(**updatedPrompt)
|
|
|
|
@router.delete("/{promptId}", response_model=Dict[str, Any])
|
|
async def delete_prompt(
|
|
promptId: str = Path(..., description="ID of the prompt to delete"),
|
|
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
|
|
):
|
|
"""Delete a prompt"""
|
|
interfaceLucydom = lucydomInterface.getInterface(currentUser)
|
|
|
|
# Check if the prompt exists
|
|
existingPrompt = interfaceLucydom.getPrompt(promptId)
|
|
if not existingPrompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Prompt with ID {promptId} not found"
|
|
)
|
|
|
|
success = interfaceLucydom.deletePrompt(promptId)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Error deleting the prompt"
|
|
)
|
|
|
|
return {"message": f"Prompt with ID {promptId} successfully deleted"} |