@@ -73,10 +73,7 @@ class UpdateTodoRequest(BaseModel):
73
73
74
74
# PATCH endpoint to update a todo item
75
75
@app .patch ("/todos/{id}" , response_model = TodoItem )
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 ())
79
-
76
+ async def update_todo (id : str , request : UpdateTodoRequest ):
80
77
# Initialize the list of update expressions and expression attributes
81
78
update_expressions = []
82
79
expression_attribute_names = {}
@@ -100,19 +97,21 @@ async def update_todo(id: str, request: UpdateTodoRequest, timestamp: int = None
100
97
update_expression = "SET " + ", " .join (update_expressions )
101
98
102
99
try :
100
+ # First, check if the item exists
101
+ response = table .get_item (Key = {"id" : id })
102
+ if "Item" not in response :
103
+ raise HTTPException (status_code = 404 , detail = "Todo not found" )
104
+
103
105
# Update the item in the DynamoDB table
104
106
response = table .update_item (
105
- Key = {"id" : id , "timestamp" : timestamp }, # Use `id` and `timestamp` to identify the item
107
+ Key = {"id" : id }, # Only using `id` as the partition key
106
108
UpdateExpression = update_expression ,
107
109
ExpressionAttributeNames = expression_attribute_names ,
108
110
ExpressionAttributeValues = expression_attribute_values ,
109
111
ReturnValues = "ALL_NEW"
110
112
)
111
113
112
114
updated_todo = response .get ("Attributes" )
113
- if not updated_todo :
114
- raise HTTPException (status_code = 404 , detail = "Todo not found" )
115
-
116
115
logging .debug (f"Updated item: { updated_todo } " )
117
116
return updated_todo
118
117
@@ -126,32 +125,29 @@ async def update_todo(id: str, request: UpdateTodoRequest, timestamp: int = None
126
125
127
126
# Delete a todo item in the DynamoDB table (using only `id` as the partition key)
128
127
@app .delete ("/todos/{id}" , status_code = 204 )
129
- async def delete_todo (id : str , timestamp : int = None ):
128
+ async def delete_todo (id : str ):
130
129
try :
131
- # If timestamp is not provided, use the current timestamp
132
- timestamp = timestamp or int ( time . time () )
130
+ # Attempt to delete the item using the partition key (id)
131
+ response = table . delete_item ( Key = { "id" : id } )
133
132
134
- # Attempt to delete the item using both the partition key (id) and sort key (timestamp)
135
- response = table .delete_item (Key = {"id" : id , "timestamp" : timestamp })
136
-
137
- # Check if the HTTP status code indicates a successful deletion
138
- if "ResponseMetadata" not in response or response ["ResponseMetadata" ].get ("HTTPStatusCode" ) != 200 :
139
- logging .warning (f"Delete operation failed for id { id } and timestamp { timestamp } : { response } " )
133
+ # If no item was deleted (i.e., no such key exists), return a 404 error
134
+ if not response .get ("Attributes" ):
140
135
raise HTTPException (status_code = 404 , detail = "Todo not found" )
141
136
142
- logging .debug (f"Deleted item with id: { id } and timestamp: { timestamp } " )
143
- # Return nothing (status code 204)
137
+ logging .debug (f"Deleted item with id: { id } " )
138
+ # Return nothing (status code 204) to indicate successful deletion
144
139
return {"detail" : "Todo deleted successfully" }
145
140
146
141
except ClientError as e :
147
- logging .error (f"ClientError deleting todo with id { id } and timestamp { timestamp } : { e } " )
142
+ logging .error (f"ClientError deleting todo with id { id } : { e } " )
148
143
raise HTTPException (status_code = 500 , detail = f"Error deleting todo: { str (e )} " )
149
144
150
145
except Exception as e :
151
- logging .error (f"Unexpected error deleting todo with id { id } and timestamp { timestamp } : { e } " )
146
+ logging .error (f"Unexpected error deleting todo with id { id } : { e } " )
152
147
raise HTTPException (status_code = 500 , detail = "Error deleting todo" )
153
148
154
149
150
+
155
151
@app .get ("/health" )
156
152
async def health ():
157
153
try :
0 commit comments