-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
161 lines (108 loc) · 3.26 KB
/
api.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pymysql
from app import app
from db_config import mysql
from flask import jsonify
from flask import flash, request
@app.route('/add', methods=['POST'])
def add_user():
try:
json = request.json
name = json['name']
task = json['task']
if name and task and request.method == 'POST':
#Initialize sql query and data
sql = "INSERT INTO todo (name, task) VALUES (%s, %s)"
data = (name, task)
#Connect to db
con = mysql.connect()
cur = con.cursor()
cur.execute(sql, data)
con.commit()
#JSON response
resp = jsonify('Task added succesfully!')
resp.status_code = 200
return resp
else:
return not_found()
except Exception as e:
print (e)
@app.route('/update', methods=['POST'])
def update_user():
try:
json = request.json
id = json['id']
name = json['name']
task = json['task']
if id and name and task and request.method == 'POST':
#Connect to db
con = mysql.connect()
cur = con.cursor()
sql = "SELECT * FROM todo.todo where id="+str(id)
cur.execute(sql)
rec = cur.fetchall()
if len(rec) > 0:
#Initialize sql query and data
sql = "UPDATE todo.todo SET name=%s, task=%s WHERE id=%s"
data = (name, task, id)
cur.execute(sql, data)
con.commit()
#JSON response
resp = jsonify('Task updated succesfully!')
resp.status_code = 200
else:
resp = jsonify('Id does not exist!')
resp.status_code = 404
return resp
else:
return not_found()
except Exception as e:
return not_found(e)
@app.route('/todos', methods=['GET'])
def todos():
try:
con = mysql.connect()
cur = con.cursor(pymysql.cursors.DictCursor)
cur.execute("SELECT * FROM todo.todo")
rows = cur.fetchall()
resp = jsonify(rows)
resp.status_code = 200
return resp
except Exception as e:
return not_found(e)
@app.route('/todo/<id>', methods=['GET'])
def todo(id):
try:
con = mysql.connect()
cur = con.cursor(pymysql.cursors.DictCursor)
cur.execute("SELECT * FROM todo.todo WHERE id= %s", id)
row = cur.fetchone()
resp = jsonify(row)
resp.status_code = 200
return resp
except Exception as e:
return not_found(e)
@app.route('/delete/<id>', methods=['GET'])
def delete_todo(id):
try:
con = mysql.connect()
cur = con.cursor()
cur.execute("DELETE FROM todo.todo WHERE id = %s", id)
con.commit()
resp = jsonify('Task deleted successfully!')
resp.status_code = 200
return resp
except Exception as e:
return not_found(e)
@app.errorhandler(404)
def not_found(error):
message = {
'status': 404,
'message': 'Not Found: ' + request.url,
'error': str(error),
}
resp = jsonify(message)
resp.status_code = 404
return resp
if __name__ == '__main__':
app.debug = True
app.run(port=5000)