-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (59 loc) · 2.08 KB
/
app.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
import json
import pymysql.cursors
from chalice import Chalice
CONNECTION_STRING = {
"host": "todo.cylxcbpu6bdo.ap-south-1.rds.amazonaws.com",
"user": "todo",
"password": "todo1234",
"db": "todo"
}
INSERT = "INSERT INTO TASK (`NAME`, `DESC`, `STATUS`, `DUE_DATE`) VALUES ('{name}', '{desc}', '{status}', {date})"
SELECT_ALL = "SELECT `ID`, `NAME`, `DESC`, `STATUS`, `DUE_DATE` FROM task"
SELECT_ONE = "SELECT `ID`, `NAME`, `DESC`, `STATUS`, `DUE_DATE` FROM task WHERE id = {id}"
app = Chalice(app_name='todo')
db = pymysql.connect(**CONNECTION_STRING)
@app.route('/')
def index():
return {'hello': 'world'}
@app.route('/tasks', methods=["GET"], cors=True)
def get_tasks():
"""Get the list of all tasks"""
with db.cursor() as cursor:
cursor.execute(SELECT_ALL)
tasks = []
keys = ('id', 'name', 'desc', 'status', 'due_date')
for row in cursor.fetchall(): # Fetch as dictionary
dct = dict(zip(keys, row))
dct['due_date'] = str(dct['due_date'])
tasks.append(dct)
print(tasks)
return tasks
@app.route('/tasks/{id}', methods=["GET"])
def get(task_id):
"""Get a task by Id"""
pass
@app.route('/tasks/add', methods=['POST'])
def add_task():
"""Can use a forms and orm layer like wtforms and sqlalchemy to perform validation and provide querying mechanism. Due to lack of time, going
ahead with raw queries"""
request = app.current_request
details = request.json_body
# The view function above will return {"hello": "world"}
# whenever you make an HTTP GET request to '/'.
#
# Here are a few more examples:
#
# @app.route('/hello/{name}')
# def hello_name(name):
# # '/hello/james' -> {"hello": "james"}
# return {'hello': name}
#
# @app.route('/users', methods=['POST'])
# def create_user():
# # This is the JSON body the user sent in their POST request.
# user_as_json = app.current_request.json_body
# # We'll echo the json body back to the user in a 'user' key.
# return {'user': user_as_json}
#
# See the README documentation for more examples.
#