tueniuu commited on
Commit
11f8d5c
·
verified ·
1 Parent(s): ad8d35f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py CHANGED
@@ -80,5 +80,43 @@ async def get_itinerary_data(authorization: str = Header(...)):
80
  raise e
81
  except Exception as e:
82
  # Catch any other unexpected errors
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  print(f"An unexpected error occurred: {e}")
84
  raise HTTPException(status_code=500, detail="Internal server error")
 
80
  raise e
81
  except Exception as e:
82
  # Catch any other unexpected errors
83
+ print(f"An unexpected error occurred: {e}")
84
+ raise HTTPException(status_code=500, detail="Internal server error")
85
+
86
+ @app.delete("/itinerary/{itinerary_id}")
87
+ async def delete_itinerary(itinerary_id: int, authorization: str = Header(...)):
88
+ """
89
+ Deletes a specific itinerary, ensuring the user owns it.
90
+ """
91
+ if not supabase:
92
+ raise HTTPException(status_code=500, detail="Database connection not configured.")
93
+
94
+ try:
95
+ # First, verify the user's token to get their ID
96
+ token_type, token = authorization.split()
97
+ if token_type.lower() != "bearer":
98
+ raise HTTPException(status_code=401, detail="Invalid token type")
99
+
100
+ payload = jwt.decode(token, JWT_SECRET_KEY, algorithms=[ALGORITHM])
101
+ user_id = payload.get("user_id")
102
+ if user_id is None:
103
+ raise HTTPException(status_code=401, detail="User ID not found in token payload")
104
+
105
+ # Securely delete the itinerary by matching both its ID and the user's ID
106
+ # This prevents a user from deleting someone else's itinerary
107
+ response = supabase.table("itineraries").delete().match({
108
+ "id": itinerary_id,
109
+ "user_id": user_id
110
+ }).execute()
111
+
112
+ # Check if the delete operation affected any rows
113
+ if not response.data:
114
+ raise HTTPException(status_code=404, detail="Itinerary not found or you do not have permission to delete it.")
115
+
116
+ return {"message": "Itinerary deleted successfully"}
117
+
118
+ except JWTError:
119
+ raise HTTPException(status_code=401, detail="Could not validate credentials")
120
+ except Exception as e:
121
  print(f"An unexpected error occurred: {e}")
122
  raise HTTPException(status_code=500, detail="Internal server error")