From a90d858ae551d77c51fcd8e8e90aaf40e03ddab4 Mon Sep 17 00:00:00 2001 From: Alex TYRODE Date: Mon, 5 May 2025 05:56:57 +0000 Subject: [PATCH] feat: add backward compatibility endpoint for updating the first pad - Introduced a new endpoint to allow users to update their first pad, ensuring backward compatibility. - Implemented logic to create a default pad if the user has no existing pads. - Added ownership verification and metadata handling for the pad update process. - Included a deprecation notice in the response, advising users to transition to the new endpoint for specific pad updates. --- src/backend/routers/pad_router.py | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/backend/routers/pad_router.py b/src/backend/routers/pad_router.py index 4ebdca8..de3b669 100644 --- a/src/backend/routers/pad_router.py +++ b/src/backend/routers/pad_router.py @@ -2,6 +2,7 @@ from typing import Dict, Any from fastapi import APIRouter, HTTPException, Depends, Request +from fastapi.responses import JSONResponse from dependencies import UserSession, require_auth from database import get_pad_service, get_backup_service, get_template_pad_service @@ -34,6 +35,73 @@ def ensure_pad_metadata(data: Dict[str, Any], pad_id: str, display_name: str) -> return data +@pad_router.post("") +async def update_first_pad( + data: Dict[str, Any], + user: UserSession = Depends(require_auth), + pad_service: PadService = Depends(get_pad_service), + backup_service: BackupService = Depends(get_backup_service), + template_pad_service: TemplatePadService = Depends(get_template_pad_service), +): + """ + Update the first pad for the authenticated user. + + This is a backward compatibility endpoint that assumes the user is trying to update their first pad. + It will be deprecated in the future. Please use POST /api/pad/{pad_id} instead. + """ + try: + # Get user's pads + user_pads = await pad_service.get_pads_by_owner(user.id) + + # If user has no pads, create a default one + if not user_pads: + new_pad = await create_pad_from_template( + name=DEFAULT_TEMPLATE_NAME, + display_name=DEFAULT_PAD_NAME, + user=user, + pad_service=pad_service, + template_pad_service=template_pad_service, + backup_service=backup_service + ) + pad_id = new_pad["id"] + else: + # Use the first pad + pad_id = user_pads[0]["id"] + + # Get the pad to verify ownership + pad = await pad_service.get_pad(pad_id) + + if not pad: + raise HTTPException(status_code=404, detail="Pad not found") + + # Verify the user owns this pad + if str(pad["owner_id"]) != str(user.id): + raise HTTPException(status_code=403, detail="You don't have permission to update this pad") + + # Ensure the uniqueId and displayName are set in the data + data = ensure_pad_metadata(data, str(pad_id), pad["display_name"]) + + # Update the pad + await pad_service.update_pad_data(pad_id, data) + + # Create a backup if needed + await backup_service.create_backup_if_needed( + source_id=pad_id, + data=data, + min_interval_minutes=MIN_INTERVAL_MINUTES, + max_backups=MAX_BACKUPS_PER_USER + ) + + # Return success with deprecation notice + return JSONResponse( + content={"status": "success", "message": "This endpoint is deprecated. Please use POST /api/pad/{pad_id} instead."}, + headers={"Deprecation": "true", "Sunset": "Mon, 10 May 2025 00:00:00 GMT"} + ) + except Exception as e: + print(f"Error updating pad: {str(e)}") + raise HTTPException(status_code=500, detail=f"Failed to update pad: {str(e)}") + + @pad_router.post("/{pad_id}") async def update_specific_pad( pad_id: UUID,