| """ | |
| Defines Flask routes for handling call by category | |
| This module contains API endpoints for managing and retrieving categories | |
| from the MongoDB database. | |
| Routes: | |
| - GET /category: Fetch all categories. | |
| """ | |
| from fastapi import APIRouter | |
| from fastapi.responses import JSONResponse | |
| from controllers.category import get_categories # pylint: disable=import-error | |
| router = APIRouter(prefix="/category", tags=["category"]) | |
| async def fetch_categories() -> JSONResponse: | |
| """ | |
| Handles GET requests to retrieve all categories. | |
| Returns: | |
| list[dict]: JSON response containing categories and their associated sites. | |
| """ | |
| try: | |
| category_data = get_categories() | |
| return JSONResponse(content=category_data) | |
| except (ConnectionError, TimeoutError, ValueError) as e: | |
| return JSONResponse(content={"error": str(e)}, status_code=400) | |
| except Exception as e: #pylint: disable=broad-except | |
| return JSONResponse(content={"error": str(e)}, status_code=500) | |