@@ -75,8 +75,10 @@ class UpdateTodoRequest(BaseModel):
75
75
# PUT endpoint to update a todo item
76
76
@app .put ("/todos/{id}" , response_model = TodoItem )
77
77
async def update_todo (id : str , request : UpdateTodoRequest ):
78
- # Use the current timestamp if one is not provided
79
- timestamp = request .timestamp or int (time .time ())
78
+ # Ensure you are using the correct timestamp
79
+ timestamp = request .timestamp
80
+ if not timestamp :
81
+ raise HTTPException (status_code = 400 , detail = "Missing 'timestamp' for identifying the item to update." )
80
82
81
83
if not request .text :
82
84
raise HTTPException (status_code = 400 , detail = "Missing 'text' in request body" )
@@ -96,12 +98,13 @@ async def update_todo(id: str, request: UpdateTodoRequest):
96
98
update_expression = "SET " + ", " .join (update_expressions )
97
99
98
100
try :
99
- # Update the item in the DynamoDB table
101
+ # Use a ConditionExpression to ensure the item exists before updating
100
102
response = table .update_item (
101
103
Key = {"id" : id , "timestamp" : timestamp },
102
104
UpdateExpression = update_expression ,
103
105
ExpressionAttributeNames = expression_attribute_names ,
104
106
ExpressionAttributeValues = expression_attribute_values ,
107
+ ConditionExpression = "attribute_exists(id) AND attribute_exists(timestamp)" ,
105
108
ReturnValues = "ALL_NEW"
106
109
)
107
110
@@ -113,6 +116,8 @@ async def update_todo(id: str, request: UpdateTodoRequest):
113
116
return updated_todo
114
117
115
118
except ClientError as e :
119
+ if e .response ['Error' ]['Code' ] == 'ConditionalCheckFailedException' :
120
+ raise HTTPException (status_code = 404 , detail = "Todo not found" )
116
121
logging .error (f"ClientError updating todo: { e } " )
117
122
raise HTTPException (status_code = 500 , detail = "Error updating todo" )
118
123
except Exception as e :
@@ -121,23 +126,20 @@ async def update_todo(id: str, request: UpdateTodoRequest):
121
126
122
127
# Delete a todo item in the DynamoDB table
123
128
@app .delete ("/todos/{id}" , status_code = 204 )
124
- async def delete_todo (id : str , timestamp : Optional [int ] = None ):
125
- # Use the current timestamp if one is not provided
126
- timestamp = timestamp or int (time .time ())
127
-
129
+ async def delete_todo (id : str , timestamp : int ):
128
130
try :
129
131
# Attempt to delete the item using both the partition key (id) and sort key (timestamp)
130
132
response = table .delete_item (Key = {"id" : id , "timestamp" : timestamp })
131
133
132
- # Check if the HTTP status code indicates a successful deletion
133
- status_code = response .get ("ResponseMetadata" , {}).get ("HTTPStatusCode" )
134
- if status_code != 200 :
135
- logging .warning (f"Delete operation failed for id { id } and timestamp { timestamp } : { response } " )
134
+ # Check if the deletion was successful by checking the response metadata
135
+ if response .get ("ResponseMetadata" , {}).get ("HTTPStatusCode" ) != 200 :
136
+ logging .warning (f"Delete operation failed for id { id } : { response } " )
136
137
raise HTTPException (status_code = 404 , detail = "Todo not found" )
137
138
138
139
logging .debug (f"Deleted item with id: { id } and timestamp: { timestamp } " )
139
- # Return nothing (status code 204)
140
- return
140
+
141
+ # No content needs to be returned for status code 204, just confirm the deletion was successful
142
+ return {"detail" : "Todo deleted successfully" }
141
143
142
144
except ClientError as e :
143
145
logging .error (f"ClientError deleting todo with id { id } : { e } " )
0 commit comments