-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
74 lines (60 loc) · 1.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
from flask import Flask, request, jsonify, abort
import boto3
app = Flask(__name__)
TODOS_TABLE = os.environ['TODOS_TABLE']
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(TODOS_TABLE)
def get_todo(id):
res = table.get_item(Key={ 'id': id })
if 'Item' not in res:
abort(404)
return res['Item']
def new_id():
# bad implementation
# maybe use numbering table or use uuid as id
res = table.scan()
ary = res['Items']
if len(ary) == 0:
return '1'
else:
ary = sorted(ary, key=lambda x: x['id'], reverse=True)
return str(int(ary[0]['id']) + 1)
@app.route("/todos")
def index():
res = table.scan()
return jsonify(res['Items'])
@app.route("/todos/<id>")
def show(id):
todo = get_todo(id)
return jsonify(todo)
@app.route("/todos", methods=["POST"])
def create():
title = request.json.get('title')
if not title:
return jsonify({'error': 'Please provider todo title'}), 422
new_todo = { 'id': new_id(), 'title': title }
res = table.put_item(Item=new_todo)
return jsonify(new_todo), 201
@app.route("/todos/<id>", methods=["PUT"])
def update(id):
edit_todo = get_todo(id)
title = request.json.get('title')
if not title:
return jsonify({'error': 'Please provider todo title'}), 422
table.update_item(
Key={ 'id': edit_todo['id'] },
UpdateExpression="set title = :n",
ExpressionAttributeValues={ ':n': title }
)
return jsonify(get_todo(id)), 201
@app.route("/todos/<id>", methods=["DELETE"])
def destroy(id):
todo = get_todo(id)
res = table.delete_item(Key={ 'id': todo['id'] })
return jsonify({'success': True}), 200
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Not found'}), 404
if __name__ == '__main__':
app.run(debug=True)