Skip to content

Commit 5e97bc8

Browse files
authored
feat: add backward compatibility endpoint for updating the first pad (#83)
- 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.
1 parent b087f66 commit 5e97bc8

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/backend/routers/pad_router.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Dict, Any
33

44
from fastapi import APIRouter, HTTPException, Depends, Request
5+
from fastapi.responses import JSONResponse
56

67
from dependencies import UserSession, require_auth
78
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) ->
3435
return data
3536

3637

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+
37105
@pad_router.post("/{pad_id}")
38106
async def update_specific_pad(
39107
pad_id: UUID,

0 commit comments

Comments
 (0)