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