Skip to content

Commit 8ca1cf5

Browse files
author
Adetokunbo Ige
committed
feat: code improvements
Signed-off-by: Adetokunbo Ige <[email protected]>
1 parent 3445ffe commit 8ca1cf5

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

todo-app/lambda_function/lambda.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ async def update_todo(id: str, request: UpdateTodoRequest):
127127
@app.delete("/todos/{id}", status_code=204)
128128
async def delete_todo(id: str):
129129
try:
130-
# Attempt to delete the item using the partition key (id)
131-
response = table.delete_item(Key={"id": id})
132-
133-
# If no item was deleted (i.e., no such key exists), return a 404 error
134-
if not response.get("Attributes"):
130+
# Attempt to delete the item using only the partition key (`id`)
131+
response = table.delete_item(Key={"id": id}) # Using only `id` to identify the item
132+
133+
# Check if the HTTP status code indicates a successful deletion
134+
if response.get("ResponseMetadata", {}).get("HTTPStatusCode") != 200:
135+
logging.warning(f"Delete operation failed for id {id}: {response}")
135136
raise HTTPException(status_code=404, detail="Todo not found")
136137

137138
logging.debug(f"Deleted item with id: {id}")
138-
# Return nothing (status code 204) to indicate successful deletion
139139
return {"detail": "Todo deleted successfully"}
140140

141141
except ClientError as e:
@@ -147,7 +147,6 @@ async def delete_todo(id: str):
147147
raise HTTPException(status_code=500, detail="Error deleting todo")
148148

149149

150-
151150
@app.get("/health")
152151
async def health():
153152
try:

0 commit comments

Comments
 (0)