@@ -66,19 +66,16 @@ async def create_todo(todo: TodoItem):
66
66
logging .error (f"Unexpected error creating todo: { e } " )
67
67
raise HTTPException (status_code = 500 , detail = "Error creating todo" )
68
68
69
- # Define a Pydantic model for the update request body
69
+ # Define the request body model for updating a todo item
70
70
class UpdateTodoRequest (BaseModel ):
71
71
text : str
72
- completed : Optional [bool ] = None # Include the `completed` field
73
- timestamp : Optional [int ] = None # Make `timestamp` optional
72
+ completed : bool = None # Optional field
74
73
75
74
# PUT endpoint to update a todo item
76
75
@app .put ("/todos/{id}" , response_model = TodoItem )
77
- async def update_todo (id : str , request : UpdateTodoRequest ):
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." )
76
+ async def update_todo (id : str , request : UpdateTodoRequest , timestamp : int = None ):
77
+ # Use the current timestamp if one is not provided
78
+ timestamp = timestamp or int (time .time ())
82
79
83
80
if not request .text :
84
81
raise HTTPException (status_code = 400 , detail = "Missing 'text' in request body" )
@@ -98,13 +95,12 @@ async def update_todo(id: str, request: UpdateTodoRequest):
98
95
update_expression = "SET " + ", " .join (update_expressions )
99
96
100
97
try :
101
- # Use a ConditionExpression to ensure the item exists before updating
98
+ # Update the item in the DynamoDB table
102
99
response = table .update_item (
103
- Key = {"id" : id , "timestamp" : timestamp },
100
+ Key = {"id" : id , "timestamp" : timestamp }, # Use `id` and `timestamp` to identify the item
104
101
UpdateExpression = update_expression ,
105
102
ExpressionAttributeNames = expression_attribute_names ,
106
103
ExpressionAttributeValues = expression_attribute_values ,
107
- ConditionExpression = "attribute_exists(id) AND attribute_exists(timestamp)" ,
108
104
ReturnValues = "ALL_NEW"
109
105
)
110
106
@@ -116,29 +112,27 @@ async def update_todo(id: str, request: UpdateTodoRequest):
116
112
return updated_todo
117
113
118
114
except ClientError as e :
119
- if e .response ['Error' ]['Code' ] == 'ConditionalCheckFailedException' :
120
- raise HTTPException (status_code = 404 , detail = "Todo not found" )
121
115
logging .error (f"ClientError updating todo: { e } " )
122
116
raise HTTPException (status_code = 500 , detail = "Error updating todo" )
123
117
except Exception as e :
124
118
logging .error (f"Error updating todo: { e } " )
125
119
raise HTTPException (status_code = 500 , detail = "Error updating todo" )
126
120
127
- # Delete a todo item in the DynamoDB table
121
+ # Delete a todo item in the DynamoDB table (using only `id` as the partition key)
128
122
@app .delete ("/todos/{id}" , status_code = 204 )
129
- async def delete_todo (id : str , timestamp : int ):
123
+ async def delete_todo (id : str ):
130
124
try :
131
- # Attempt to delete the item using both the partition key (id) and sort key ( timestamp)
132
- response = table .delete_item (Key = {"id" : id , "timestamp" : timestamp })
125
+ # If the table uses only `id` as the partition key, we don't need a timestamp
126
+ response = table .delete_item (Key = {"id" : id })
133
127
134
128
# Check if the deletion was successful by checking the response metadata
135
129
if response .get ("ResponseMetadata" , {}).get ("HTTPStatusCode" ) != 200 :
136
130
logging .warning (f"Delete operation failed for id { id } : { response } " )
137
131
raise HTTPException (status_code = 404 , detail = "Todo not found" )
138
132
139
- logging .debug (f"Deleted item with id: { id } and timestamp: { timestamp } " )
133
+ logging .debug (f"Deleted item with id: { id } " )
140
134
141
- # No content needs to be returned for status code 204, just confirm the deletion was successful
135
+ # Return nothing ( status code 204)
142
136
return {"detail" : "Todo deleted successfully" }
143
137
144
138
except ClientError as e :
@@ -149,6 +143,7 @@ async def delete_todo(id: str, timestamp: int):
149
143
logging .error (f"Unexpected error deleting todo with id { id } : { e } " )
150
144
raise HTTPException (status_code = 500 , detail = "Error deleting todo" )
151
145
146
+
152
147
@app .get ("/health" )
153
148
async def health ():
154
149
try :
0 commit comments