def add_todo(): user_id = get_user_id() todo_id = generate_todo_id() req_body = get_body_as_dict() if 'title' not in req_body or req_body['title'] == '': return httpres.response_400( 'key error: the following parameter is required: title') new_todo = { 'user_id': user_id, 'todo_id': todo_id, 'title': req_body['title'], 'done': '0', 'l_idx_done': '{}#{}'.format('0', todo_id) } if 'content' in req_body: new_todo['content'] = req_body['content'] try: res = db.put_todo(new_todo) if res['ResponseMetadata']['HTTPStatusCode'] != 200: return httpres.response({'message': 'Failed to add a todo.'}, res['ResponseMetadata']['HTTPStatusCode']) return httpres.response_200(new_todo) except Exception as e: return httpres.response_500(e)
def get_todo(todo_id): user_id = get_user_id() res = db.get_todo(user_id, todo_id) if 'Item' not in res: return httpres.response_404(todo_id) return httpres.response_200(res['Item'])
def get_todos(): user_id = get_user_id() query_params = get_query_params() if 'done' in query_params \ and str(query_params['done']) in ['0', '1']: todos = db.get_todos_with_done(user_id, query_params['done']) else: todos = db.get_todos(user_id) filtered_todos = filter_todos(todos['Items'], query_params) return httpres.response_200(filtered_todos)
def delete_todo(todo_id): user_id = get_user_id() if db.todo_exists(user_id, todo_id) is False: return httpres.response_404(todo_id) try: res = db.delete_todo(user_id, todo_id) if res['ResponseMetadata']['HTTPStatusCode'] != 200: return httpres.response({'message': 'Failed to delete a todo.'}, res['ResponseMetadata']['HTTPStatusCode']) return httpres.response_200(res['Attributes']) except Exception as e: return httpres.response_500(e)
def update_todo(todo_id): user_id = get_user_id() if db.todo_exists(user_id, todo_id) is False: return httpres.response_404(todo_id) params = get_body_as_dict() if 'title' not in params and 'content' not in params and 'done' not in params: return httpres.response_400( ('parameter error: one of the following parameters is required: ' 'title, content, done')) if ('title' in params and params['title'] == '') \ or ('done' in params and str(params['done']) not in ['0', '1']): return httpres.response_400(( 'parameter error: invalid value for one of the following parameters' ': title, done')) patch_data = {} if 'title' in params: patch_data['title'] = params['title'] if 'content' in params: patch_data['content'] = params['content'] if 'done' in params: patch_data['done'] = str(params['done']) patch_data['l_idx_done'] = '{}#{}'.format(str(params['done']), todo_id) try: res = db.update_todo(user_id, todo_id, patch_data) if res['ResponseMetadata']['HTTPStatusCode'] != 200: return httpres.response({'message': 'Failed to update a todo.'}, res['ResponseMetadata']['HTTPStatusCode']) return httpres.response_200(res['Attributes']) except Exception as e: return httpres.response_500(e)
def test_response_200(self, monkeypatch): expected_body = {'message': 'Expected body'} actual_res = httpres.response_200(expected_body) assert actual_res.body == expected_body assert actual_res.status_code == 200