awacke1 commited on
Commit
43fdc2a
·
verified ·
1 Parent(s): e6f6372

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -176,25 +176,32 @@ def update_record(container, updated_record):
176
  # 🗑️ Delete record - Saying goodbye to data (it's not you, it's me)
177
  def delete_record(container, id):
178
  try:
179
- # First try to read the document with id as both item id and partition key
180
- try:
181
- doc = container.read_item(item=id, partition_key=id)
182
- except exceptions.CosmosResourceNotFoundError:
183
- # If that fails, query to find the document and get its partition key
184
- query = f"SELECT * FROM c WHERE c.id = '{id}'"
185
- items = list(container.query_items(
186
- query=query,
187
- enable_cross_partition_query=True
188
- ))
189
- if not items:
190
- return False, f"Record with id {id} not found. 🕵️‍♂️"
191
- doc = items[0]
 
 
 
 
 
 
 
 
192
 
193
- # Now delete with the correct partition key
194
- partition_key = doc.get('id', id) # Default to id if no partition key found
195
- container.delete_item(item=id, partition_key=partition_key)
196
  return True, f"Successfully deleted record with id: {id} 🗑️"
197
 
 
 
198
  except exceptions.CosmosHttpResponseError as e:
199
  return False, f"HTTP error occurred: {str(e)} 🚨"
200
  except Exception as e:
 
176
  # 🗑️ Delete record - Saying goodbye to data (it's not you, it's me)
177
  def delete_record(container, id):
178
  try:
179
+ # First try to find the document using a query
180
+ query = f"SELECT * FROM c WHERE c.id = @id"
181
+ params = [{"name": "@id", "value": id}]
182
+ items = list(container.query_items(
183
+ query=query,
184
+ parameters=params,
185
+ enable_cross_partition_query=True
186
+ ))
187
+
188
+ if not items:
189
+ return False, f"Record with id {id} not found. 🕵️‍♂️"
190
+
191
+ doc = items[0]
192
+ partition_key = doc.get('id') # Using id as partition key
193
+
194
+ # Delete with explicit partition key
195
+ container.delete_item(
196
+ item=id,
197
+ partition_key=partition_key,
198
+ enable_cross_partition_query=True
199
+ )
200
 
 
 
 
201
  return True, f"Successfully deleted record with id: {id} 🗑️"
202
 
203
+ except exceptions.CosmosResourceNotFoundError:
204
+ return False, f"Record with id {id} not found or already deleted. 🕵️‍♂️"
205
  except exceptions.CosmosHttpResponseError as e:
206
  return False, f"HTTP error occurred: {str(e)} 🚨"
207
  except Exception as e: