-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWebServiceProgram.py
360 lines (256 loc) · 7.56 KB
/
WebServiceProgram.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import sqlite3
from flask import Flask, jsonify, request, abort
from argparse import ArgumentParser
DB = 'db.sqlite'
def users_get_row_as_dict(row):
row_dict = {
'user_id': row[0],
'name': row[1],
'email': row[2],
'password': row[3],
'age': row[4],
'gender': row[5],
}
return row_dict
def meals_get_row_as_dict(row):
row_dict = {
'meal_id': row[0],
'user_email': row[1],
'meal_name': row[2],
'meal_cal': row[3],
'record_date': row[4],
}
return row_dict
def plans_get_row_as_dict(row):
row_dict = {
'plan_id': row[0],
'plan_name': row[1],
'plan_name_photo': row[2],
}
return row_dict
def plan_details_get_row_as_dict(row):
row_dict = {
'plan_details_id': row[0],
'plan_id': row[1],
'plan_days': row[2],
'breakfast': row[3],
'lunch': row[4],
'dinner': row[5],
'plan_photo': row[6],
}
return row_dict
def recipes_get_row_as_dict(row):
row_dict = {
'recipe_id': row[0],
'recipe_name': row[1],
'recipe_photo': row[2],
}
return row_dict
def ingredients_get_row_as_dict(row):
row_dict = {
'ingredient_id': row[0],
'recipe_id': row[1],
'ingredient_name': row[2],
}
return row_dict
app = Flask(__name__)
@app.route('/api/users/<email>', methods=['GET'])
def get_user(email):
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT * FROM users WHERE email=?', (email,))
row = cursor.fetchone()
db.close()
if row:
row_as_dict = users_get_row_as_dict(row)
return jsonify(row_as_dict), 200
else:
return jsonify(None), 200
@app.route('/api/users', methods=['POST'])
def store_user():
if not request.json:
abort(404)
new_user = (
request.json['name'],
request.json['email'],
request.json['password'],
request.json['age'],
request.json['gender'],
)
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('''
INSERT INTO users(name,email,password,age,gender)
VALUES(?,?,?,?,?)
''', new_user)
user_id = cursor.lastrowid
db.commit()
response = {
'user_id': user_id,
'affected': db.total_changes,
}
db.close()
return jsonify(response), 201
@app.route('/api/users/<email>', methods=['PUT'])
def update_user(email):
if not request.json:
abort(400)
if 'email' not in request.json:
abort(400)
if request.json['email'] != email:
abort(400)
update_user = (
request.json['password'],
email,
)
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('''
UPDATE users SET password=? WHERE email=?
''', update_user)
db.commit()
response = {
'user_email': email,
'affected': db.total_changes,
}
db.close()
return jsonify(response), 201
@app.route('/api/meals/<user_email>', methods=['GET'])
def get_all_meals(user_email):
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT * FROM meals WHERE user_email=?', (user_email,))
rows = cursor.fetchall()
print(rows)
db.close()
rows_as_dict = []
for row in rows:
row_as_dict = meals_get_row_as_dict(row)
rows_as_dict.append(row_as_dict)
return jsonify(rows_as_dict), 200
@app.route('/api/meals/<user_email>/<int:meal_id>', methods=['GET'])
def get_meal(user_email,meal_id):
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT * FROM meals WHERE user_email=? AND meal_id=?', (user_email,str(meal_id),))
row = cursor.fetchone()
db.close()
if row:
row_as_dict = meals_get_row_as_dict(row)
return jsonify(row_as_dict), 200
else:
return jsonify(None), 200
@app.route('/api/meals/<user_email>', methods=['POST'])
def store_meal(user_email):
if not request.json:
abort(404)
new_meal = (
user_email,
request.json['meal_name'],
request.json['meal_cal'],
request.json['record_date'],
)
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('''
INSERT INTO meals(user_email,meal_name,meal_cal,record_date)
VALUES(?,?,?,?)
''', new_meal)
meal_id = cursor.lastrowid
db.commit()
response = {
'meal_id': meal_id,
'affected': db.total_changes,
}
db.close()
return jsonify(response), 201
@app.route('/api/meals/<int:meal>', methods=['DELETE'])
def delete(meal):
if not request.json:
abort(400)
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('DELETE FROM meals WHERE meal_id=?', (str(meal),))
db.commit()
response = {
'meal_id': meal,
'affected': db.total_changes,
}
db.close()
return jsonify(response), 201
@app.route('/api/plans', methods=['GET'])
def get_all_plans():
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT * FROM plans')
rows = cursor.fetchall()
print(rows)
db.close()
rows_as_dict = []
for row in rows:
row_as_dict = plans_get_row_as_dict(row)
rows_as_dict.append(row_as_dict)
return jsonify(rows_as_dict), 200
@app.route('/api/plans/<int:plan_id>', methods=['GET'])
def get_plan(plan_id):
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT * FROM plan_details WHERE plan_id=?', (str(plan_id),))
rows = cursor.fetchall()
print(rows)
db.close()
rows_as_dict = []
for row in rows:
row_as_dict = plan_details_get_row_as_dict(row)
rows_as_dict.append(row_as_dict)
return jsonify(rows_as_dict), 200
@app.route('/api/recipes', methods=['GET'])
def get_all_recipes():
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT recipe_id, recipe_name, recipe_photo FROM recipes')
rows = cursor.fetchall()
print(rows)
db.close()
rows_as_dict = []
for row in rows:
row_as_dict = recipes_get_row_as_dict(row)
rows_as_dict.append(row_as_dict)
return jsonify(rows_as_dict), 200
@app.route('/api/recipes/<int:recipe_id>', methods=['GET'])
def get_recipes(recipe_id):
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT * FROM ingredients WHERE recipe_id=?', (str(recipe_id),))
rows = cursor.fetchall()
print(rows)
db.close()
rows_as_dict = []
for row in rows:
row_as_dict = ingredients_get_row_as_dict(row)
rows_as_dict.append(row_as_dict)
return jsonify(rows_as_dict), 200
@app.route('/api/instruction/<int:recipe_id>', methods=['GET'])
def get_instruction(recipe_id):
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT recipe_instructions FROM recipes WHERE recipe_id=?', (str(recipe_id),))
row = cursor.fetchone()
print(row)
db.close()
return jsonify(row), 200
@app.route('/api/recipeImage/<int:recipe_id>', methods=['GET'])
def get_image(recipe_id):
db = sqlite3.connect(DB)
cursor = db.cursor()
cursor.execute('SELECT recipe_photo FROM recipes WHERE recipe_id=?', (str(recipe_id),))
row = cursor.fetchone()
print(row)
db.close()
return jsonify(row), 200
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
app.run(host='0.0.0.0', port=port, debug=True)