Created
December 4, 2022 18:00
-
-
Save chapimenge3/0f020a10ff5101935675705f6aaaf1df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
this method goes to below the list_todo function | |
''' | |
def update(event, context): | |
''' | |
Update Todo Application in Dynamo DB | |
Params: | |
event: API Gateway Event | |
context: Lambda Context | |
Return: | |
response: API Gateway Response | |
response.body: JSON String | |
message: String | |
error: Boolean|String | |
''' | |
try: | |
body = event['body'] | |
# check if the body is empty | |
if body is None: | |
raise Exception('Body is empty') | |
# check if the body is a string and convert it to a dict | |
if isinstance(body, str): | |
body = json.loads(body) | |
todo = body['todo'] | |
# check if the todo is empty | |
if todo is None: | |
raise Exception('Todo is empty') | |
# check if todo is already in the table | |
if table.get_item(Key={'todo': todo}).get('Item') is None: | |
raise Exception('Todo does not exist') | |
# update todo | |
table.update_item( | |
Key={'todo': todo}, | |
UpdateExpression='SET done = :done', | |
ExpressionAttributeValues={ | |
':done': True | |
} | |
) | |
# return todoId | |
response = { | |
'statusCode': 200, | |
'body': json.dumps({ | |
'message': 'Todo updated', | |
'error': False | |
}) | |
} | |
return response | |
except Exception as e: | |
# return error | |
response = { | |
'statusCode': 400, | |
'body': json.dumps({ | |
'message': str(e), | |
'error': True | |
}) | |
} | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment