-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.py
115 lines (92 loc) · 2.81 KB
/
connect.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
# import the necessary packages
import flask
import json
import pymysql
import requests
from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True
# configuration used to connect to MariaDB
config = {
'host':'192.168.4.23',
'port':36033,
'user':'root',
'password':'gems729',
'db':'bearfood',
'charset':'utf8mb4',
'cursorclass':pymysql.cursors.DictCursor
}
# route to return all people
@app.route('/api/users', methods=['GET'])
def users():
conn = pymysql.connect(**config)
cur = conn.cursor()
cur.execute("select * from users")
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for r in rv:
json_data.append(dict(r))
return json.dumps(json_data)
# route to return ingredients
@app.route('/api/ingredients', methods=['GET'])
def ingredients():
conn = pymysql.connect(**config)
cur = conn.cursor()
cur.execute("select * from ingredients")
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for r in rv:
json_data.append(dict(r))
return json.dumps(json_data)
# route to return tours
@app.route('/api/<user_id>/tours', methods=['GET'])
def tours():
conn = pymysql.connect(**config)
cur = conn.cursor()
cur.execute("select * from tours where user_id = {:s}".format(user_id))
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for r in rv:
json_data.append(dict(r))
return json.dumps(json_data)
# route to return meals
@app.route('/api/<user_id>/meals', methods=['GET'])
def meals():
conn = pymysql.connect(**config)
cur = conn.cursor()
cur.execute("select * from meals where user_id = {:s}".format(user_id))
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for r in rv:
json_data.append(dict(r))
return json.dumps(json_data)
# route to return tours
@app.route('/api/<user_id>/<tour_id>', methods=['GET'])
def get_tour():
conn = pymysql.connect(**config)
cur = conn.cursor()
cur.execute("select * from tours where user_id = {:s} and id = {:s}".format(user_id, tour_id))
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for r in rv:
json_data.append(dict(r))
return json.dumps(json_data)
# route to add user
@app.route('/api/add_user/', methods=['POST'])
def add_user():
item = request.get_json()
print("insert into users (name, email) values ('{:s}','{:s}');".format(item['name'],item['email']))
conn = pymysql.connect(**config)
cur = conn.cursor()
cur.execute("insert into users (name, email) values ('{:s}','{:s}');".format(item['name'],item['email']))
conn.commit()
return item
app.run()
# x = [{"id": 2, "email": "[email protected]", "name": "Katy"}]
# for item in x:
# item['name']