| 6 | |
| 7 | |
| 8 | def update(event, context): |
| 9 | # TODO: Figure out why this is behaving differently to the other endpoints |
| 10 | # data = json.loads(event['body']) |
| 11 | data = event['body'] |
| 12 | |
| 13 | if 'text' not in data and 'checked' not in data: |
| 14 | logging.error('Validation Failed %s', data) |
| 15 | return {'statusCode': 422, |
| 16 | 'body': json.dumps({'error_message': 'Couldn\'t update the todo item.'})} |
| 17 | |
| 18 | try: |
| 19 | found_todo = TodoModel.get(hash_key=event['path']['todo_id']) |
| 20 | except DoesNotExist: |
| 21 | return {'statusCode': 404, |
| 22 | 'body': json.dumps({'error_message': 'TODO was not found'})} |
| 23 | |
| 24 | todo_changed = False |
| 25 | if 'text' in data and data['text'] != found_todo.text: |
| 26 | found_todo.text = data['text'] |
| 27 | todo_changed = True |
| 28 | if 'checked' in data and data['checked'] != found_todo.checked: |
| 29 | found_todo.checked = data['checked'] |
| 30 | todo_changed = True |
| 31 | |
| 32 | if todo_changed: |
| 33 | found_todo.save() |
| 34 | else: |
| 35 | logging.info('Nothing changed did not update') |
| 36 | |
| 37 | # create a response |
| 38 | return {'statusCode': 200, |
| 39 | 'body': json.dumps(dict(found_todo))} |
| 40 | |