|
2 | 2 | from typing import Dict, Any |
3 | 3 |
|
4 | 4 | from fastapi import APIRouter, HTTPException, Depends, Request |
| 5 | +from fastapi.responses import JSONResponse |
5 | 6 |
|
6 | 7 | from dependencies import UserSession, require_auth |
7 | 8 | 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) -> |
34 | 35 | return data |
35 | 36 |
|
36 | 37 |
|
| 38 | +@pad_router.post("") |
| 39 | +async def update_first_pad( |
| 40 | + data: Dict[str, Any], |
| 41 | + user: UserSession = Depends(require_auth), |
| 42 | + pad_service: PadService = Depends(get_pad_service), |
| 43 | + backup_service: BackupService = Depends(get_backup_service), |
| 44 | + template_pad_service: TemplatePadService = Depends(get_template_pad_service), |
| 45 | +): |
| 46 | + """ |
| 47 | + Update the first pad for the authenticated user. |
| 48 | + |
| 49 | + This is a backward compatibility endpoint that assumes the user is trying to update their first pad. |
| 50 | + It will be deprecated in the future. Please use POST /api/pad/{pad_id} instead. |
| 51 | + """ |
| 52 | + try: |
| 53 | + # Get user's pads |
| 54 | + user_pads = await pad_service.get_pads_by_owner(user.id) |
| 55 | + |
| 56 | + # If user has no pads, create a default one |
| 57 | + if not user_pads: |
| 58 | + new_pad = await create_pad_from_template( |
| 59 | + name=DEFAULT_TEMPLATE_NAME, |
| 60 | + display_name=DEFAULT_PAD_NAME, |
| 61 | + user=user, |
| 62 | + pad_service=pad_service, |
| 63 | + template_pad_service=template_pad_service, |
| 64 | + backup_service=backup_service |
| 65 | + ) |
| 66 | + pad_id = new_pad["id"] |
| 67 | + else: |
| 68 | + # Use the first pad |
| 69 | + pad_id = user_pads[0]["id"] |
| 70 | + |
| 71 | + # Get the pad to verify ownership |
| 72 | + pad = await pad_service.get_pad(pad_id) |
| 73 | + |
| 74 | + if not pad: |
| 75 | + raise HTTPException(status_code=404, detail="Pad not found") |
| 76 | + |
| 77 | + # Verify the user owns this pad |
| 78 | + if str(pad["owner_id"]) != str(user.id): |
| 79 | + raise HTTPException(status_code=403, detail="You don't have permission to update this pad") |
| 80 | + |
| 81 | + # Ensure the uniqueId and displayName are set in the data |
| 82 | + data = ensure_pad_metadata(data, str(pad_id), pad["display_name"]) |
| 83 | + |
| 84 | + # Update the pad |
| 85 | + await pad_service.update_pad_data(pad_id, data) |
| 86 | + |
| 87 | + # Create a backup if needed |
| 88 | + await backup_service.create_backup_if_needed( |
| 89 | + source_id=pad_id, |
| 90 | + data=data, |
| 91 | + min_interval_minutes=MIN_INTERVAL_MINUTES, |
| 92 | + max_backups=MAX_BACKUPS_PER_USER |
| 93 | + ) |
| 94 | + |
| 95 | + # Return success with deprecation notice |
| 96 | + return JSONResponse( |
| 97 | + content={"status": "success", "message": "This endpoint is deprecated. Please use POST /api/pad/{pad_id} instead."}, |
| 98 | + headers={"Deprecation": "true", "Sunset": "Mon, 10 May 2025 00:00:00 GMT"} |
| 99 | + ) |
| 100 | + except Exception as e: |
| 101 | + print(f"Error updating pad: {str(e)}") |
| 102 | + raise HTTPException(status_code=500, detail=f"Failed to update pad: {str(e)}") |
| 103 | + |
| 104 | + |
37 | 105 | @pad_router.post("/{pad_id}") |
38 | 106 | async def update_specific_pad( |
39 | 107 | pad_id: UUID, |
|
0 commit comments