gateway/modules/routes/routeMandates.py
2025-05-21 19:38:06 +02:00

174 lines
No EOL
5.8 KiB
Python

from fastapi import APIRouter, HTTPException, Depends, Body, Path, Request
from typing import List, Dict, Any, Optional
from fastapi import status
import logging
# Import auth module
import modules.security.auth as auth
# Import interfaces
import modules.interfaces.gatewayInterface as gatewayInterface
from modules.interfaces.gatewayModel import Mandate, getModelAttributes
# Configure logger
logger = logging.getLogger(__name__)
# Model attributes for Mandate
mandateAttributes = getModelAttributes(Mandate)
router = APIRouter(
prefix="/api/mandates",
tags=["Mandates"],
responses={404: {"description": "Not found"}}
)
@router.get("/", response_model=List[Dict[str, Any]], tags=["Mandates"])
async def get_mandates(currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)):
"""Get all mandates"""
try:
interfaceGateway = gatewayInterface.getInterface(currentUser)
return interfaceGateway.getMandates()
except Exception as e:
logger.error(f"Error getting mandates: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get mandates: {str(e)}"
)
@router.get("/{mandateId}", response_model=Dict[str, Any], tags=["Mandates"])
async def get_mandate(
mandateId: str,
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
):
"""Get a specific mandate by ID"""
try:
interfaceGateway = gatewayInterface.getInterface(currentUser)
mandate = interfaceGateway.getMandateById(mandateId)
if not mandate:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Mandate {mandateId} not found"
)
return mandate
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting mandate {mandateId}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get mandate: {str(e)}"
)
@router.post("/", response_model=Mandate, tags=["Mandates"])
async def create_mandate(
mandateData: Mandate,
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
):
"""Create a new mandate"""
try:
interfaceGateway = gatewayInterface.getInterface(currentUser)
try:
createdMandate = interfaceGateway.createMandate(mandateData)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
if not createdMandate:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create mandate"
)
return createdMandate
except HTTPException:
raise
except Exception as e:
logger.error(f"Error creating mandate: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create mandate: {str(e)}"
)
@router.put("/{mandateId}", response_model=Mandate, tags=["Mandates"])
async def update_mandate(
mandateId: str,
mandateData: Mandate,
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
):
"""Update an existing mandate"""
try:
interfaceGateway = gatewayInterface.getInterface(currentUser)
# Check if mandate exists
existingMandate = interfaceGateway.getMandateById(mandateId)
if not existingMandate:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Mandate {mandateId} not found"
)
# Update mandate data
try:
updatedMandate = interfaceGateway.updateMandate(mandateId, mandateData)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
if not updatedMandate:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update mandate"
)
return updatedMandate
except HTTPException:
raise
except Exception as e:
logger.error(f"Error updating mandate {mandateId}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to update mandate: {str(e)}"
)
@router.delete("/{mandateId}", response_model=Dict[str, Any], tags=["Mandates"])
async def delete_mandate(
mandateId: str,
currentUser: Dict[str, Any] = Depends(auth.getCurrentActiveUser)
):
"""Delete a mandate"""
try:
interfaceGateway = gatewayInterface.getInterface(currentUser)
# Check if mandate exists
existingMandate = interfaceGateway.getMandateById(mandateId)
if not existingMandate:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Mandate {mandateId} not found"
)
# Delete mandate
try:
interfaceGateway.deleteMandate(mandateId)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
return {"message": f"Mandate {mandateId} deleted successfully"}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error deleting mandate {mandateId}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to delete mandate: {str(e)}"
)