Skip to content

Commit

Permalink
feat: code improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Adetokunbo Ige <[email protected]>
  • Loading branch information
Adetokunbo Ige committed Nov 19, 2024
1 parent b06c28b commit 57208fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
13 changes: 4 additions & 9 deletions todo-app/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@

aws.config.region = region

# First, create the DynamoDB table
# First, create the DynamoDB table with just `id` as the primary key
dynamodb_table = aws.dynamodb.Table(
f"todo-{environment}",
name=f"todo-{environment}",
hash_key="id",
range_key="timestamp",
hash_key="id", # Only `id` as the partition key
attributes=[
aws.dynamodb.TableAttributeArgs(
name="id",
type="S"
),
aws.dynamodb.TableAttributeArgs(
name="timestamp",
type="N"
type="S" # `S` for string type (use appropriate type for `id`)
),
],
billing_mode="PAY_PER_REQUEST",
billing_mode="PAY_PER_REQUEST", # On-demand billing mode
tags={
"Environment": environment,
"Created_By": "Pulumi"
Expand Down
38 changes: 17 additions & 21 deletions todo-app/lambda_function/lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ class UpdateTodoRequest(BaseModel):

# PATCH endpoint to update a todo item
@app.patch("/todos/{id}", response_model=TodoItem)
async def update_todo(id: str, request: UpdateTodoRequest, timestamp: int = None):
# Use the current timestamp if one is not provided
timestamp = timestamp or int(time.time())

async def update_todo(id: str, request: UpdateTodoRequest):
# Initialize the list of update expressions and expression attributes
update_expressions = []
expression_attribute_names = {}
Expand All @@ -100,19 +97,21 @@ async def update_todo(id: str, request: UpdateTodoRequest, timestamp: int = None
update_expression = "SET " + ", ".join(update_expressions)

try:
# First, check if the item exists
response = table.get_item(Key={"id": id})
if "Item" not in response:
raise HTTPException(status_code=404, detail="Todo not found")

# Update the item in the DynamoDB table
response = table.update_item(
Key={"id": id, "timestamp": timestamp}, # Use `id` and `timestamp` to identify the item
Key={"id": id}, # Only using `id` as the partition key
UpdateExpression=update_expression,
ExpressionAttributeNames=expression_attribute_names,
ExpressionAttributeValues=expression_attribute_values,
ReturnValues="ALL_NEW"
)

updated_todo = response.get("Attributes")
if not updated_todo:
raise HTTPException(status_code=404, detail="Todo not found")

logging.debug(f"Updated item: {updated_todo}")
return updated_todo

Expand All @@ -126,32 +125,29 @@ async def update_todo(id: str, request: UpdateTodoRequest, timestamp: int = None

# Delete a todo item in the DynamoDB table (using only `id` as the partition key)
@app.delete("/todos/{id}", status_code=204)
async def delete_todo(id: str, timestamp: int = None):
async def delete_todo(id: str):
try:
# If timestamp is not provided, use the current timestamp
timestamp = timestamp or int(time.time())
# Attempt to delete the item using the partition key (id)
response = table.delete_item(Key={"id": id})

# Attempt to delete the item using both the partition key (id) and sort key (timestamp)
response = table.delete_item(Key={"id": id, "timestamp": timestamp})

# Check if the HTTP status code indicates a successful deletion
if "ResponseMetadata" not in response or response["ResponseMetadata"].get("HTTPStatusCode") != 200:
logging.warning(f"Delete operation failed for id {id} and timestamp {timestamp}: {response}")
# If no item was deleted (i.e., no such key exists), return a 404 error
if not response.get("Attributes"):
raise HTTPException(status_code=404, detail="Todo not found")

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

except ClientError as e:
logging.error(f"ClientError deleting todo with id {id} and timestamp {timestamp}: {e}")
logging.error(f"ClientError deleting todo with id {id}: {e}")
raise HTTPException(status_code=500, detail=f"Error deleting todo: {str(e)}")

except Exception as e:
logging.error(f"Unexpected error deleting todo with id {id} and timestamp {timestamp}: {e}")
logging.error(f"Unexpected error deleting todo with id {id}: {e}")
raise HTTPException(status_code=500, detail="Error deleting todo")



@app.get("/health")
async def health():
try:
Expand Down

0 comments on commit 57208fd

Please sign in to comment.