| 6 | |
| 7 | |
| 8 | def create(event, context): |
| 9 | print(event['body']) |
| 10 | data = json.loads(event['body']) |
| 11 | if 'text' not in data: |
| 12 | logging.error('Validation Failed') |
| 13 | return {'statusCode': 422, |
| 14 | 'body': json.dumps({'error_message': 'Couldn\'t create the todo item.'})} |
| 15 | |
| 16 | if not data['text']: |
| 17 | logging.error('Validation Failed - text was empty. %s', data) |
| 18 | return {'statusCode': 422, |
| 19 | 'body': json.dumps({'error_message': 'Couldn\'t create the todo item. As text was empty.'})} |
| 20 | |
| 21 | a_todo = TodoModel(todo_id=str(uuid.uuid1()), |
| 22 | text=data['text'], |
| 23 | checked=False) |
| 24 | |
| 25 | # write the todo to the database |
| 26 | a_todo.save() |
| 27 | |
| 28 | # create a response |
| 29 | return {'statusCode': 201, |
| 30 | 'body': json.dumps(dict(a_todo))} |
| 31 | |