Created
December 4, 2022 18:05
-
-
Save chapimenge3/f7b5ef2e402d82ec11dcd7fee283fc33 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 update_todo function | |
''' | |
def delete_todo(event, context): | |
''' | |
Delete 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') | |
# delete todo | |
table.delete_item(Key={'todo': todo}) | |
# return todoId | |
response = { | |
'statusCode': 200, | |
'body': json.dumps({ | |
'message': 'Todo deleted', | |
'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