-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
272 lines (216 loc) · 7.59 KB
/
app.py
File metadata and controls
272 lines (216 loc) · 7.59 KB
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
from flask import Flask
from flask import render_template, request, redirect, url_for, jsonify, abort
from flask_cors import CORS
from sqlalchemy import text
from models import *
from dbFiller import *
from sqlQueries import *
import simplejson as json
import decimal, datetime
# Init app
app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://Pasoon:password123@localhost:5432/restaurant_db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
# db.create_all()
# To populate table, run each populate command one at a time.
# populateRestaurantTable(db)
# populateMenuTable(db)
# populateRaterTable(db)
# populateRatingTable(db)
# populateMenuItemRatingTable(db)
# Home
@app.route('/')
def index():
return render_template('index.html')
# Get all the restos
@app.route('/restaurants', defaults={'limit': None})
@app.route('/restaurants/limit=<limit>', methods=['GET'])
def getAllRestaurants(limit):
if(limit):
restaurants = Restaurant.query.limit(limit).all()
else:
restaurants = Restaurant.query.all()
if restaurants is None:
abort(404)
return jsonify(items=[i.serialize for i in restaurants])
# Get resto by key
@app.route('/restaurantById/<id>', methods=['GET'])
def getRestaurantByID(id):
restaurant = Restaurant.query.get(id)
if restaurant is None:
return abort(404)
return jsonify(restaurant.serialize)
# Get resto by name
@app.route('/restaurantByName/<name>', methods=['GET'])
def getRestaurantByName(name):
restaurants = Restaurant.query.filter(Restaurant.name.ilike("%" + name + "%")).all()
if restaurants is None:
return abort(404)
return jsonify(items=[i.serialize for i in restaurants])
# Get resto by type
@app.route('/restaurantByType/<type>', methods=['GET'])
def getRestaurantByType(type):
restaurants = Restaurant.query.filter(Restaurant.type.ilike("%" + type + "%")).all()
if restaurants is None:
return abort(404)
return jsonify(items=[i.serialize for i in restaurants])
# Get locations of a restaurant
@app.route('/locationByRestaurantId/<id>', methods=['GET'])
def getLocationsByRestaurantId(id):
locations = Location.query.filter_by(restaurantId=id).all()
if locations is None:
return abort(404)
return jsonify(items=[i.serialize for i in locations])
# Get menus by restaurant
@app.route('/menuByRestaurantId/<id>', methods=['GET'])
def getMenuByRestaurantId(id):
menu = MenuItem.query.filter_by(restaurantId=id).order_by(MenuItem.category).all()
if menu is None:
return abort(404)
return jsonify(items=[i.serialize for i in menu])
# Get rating for menu item
@app.route('/ratingByMenuItemId/<id>')
def getRatingByMenuItemId(id):
rating = RatingItem.query.filter_by(itemId=id).all()
if rating is None:
return abort(404)
return jsonify(items=[i.serialize for i in rating])
# Get raters
@app.route('/raters', methods=['GET'])
def getAllRaters():
raters = Rater.query.all()
if raters is not None:
return jsonify(items=[i.serialize for i in raters])
return abort(404)
# Get rater by key
@app.route('/rater/<id>', methods=['GET'])
def getRaterByID(id):
rater = Rater.query.get(id)
if rater is None:
return abort(404)
return jsonify(rater.serialize)
# Post Restaurant
@app.route('/post_resto', methods=['POST'])
def post_resto():
dataReceived = request.get_json()
print(dataReceived)
new_restaurant = Restaurant(name=dataReceived['name'], type=dataReceived['type'], url=dataReceived['url'], pic_url=dataReceived['pic_url'], overallrating=4)
db.session.add(new_restaurant)
db.session.commit()
return redirect(url_for('index'))
# Post Menu Item
@app.route('/post_menuitem', methods=['POST'])
def post_menuitem():
dataReceived = request.get_json()
print(dataReceived)
new_menuitem = MenuItem(restaurantId=dataReceived['restaurantId'], name=dataReceived['name'], foodtype=dataReceived['type'], category=dataReceived['category'], description=dataReceived['description'], price=dataReceived['price'])
db.session.add(new_menuitem)
db.session.commit()
return redirect(url_for('index'))
# Post Rater
@app.route('/post_rater', methods=['POST'])
def post_rater():
dataReceived = request.get_json()
print(dataReceived)
joinDate = str(datetime.date.today())[:10]
new_rater = Rater(userId=dataReceived['userId'], email=dataReceived['email'], name=dataReceived['name'], join_date=joinDate, raterType=dataReceived['type'], reputation=1)
db.session.add(new_rater)
db.session.commit()
return redirect(url_for('index'))
# Delete a Menu Item
@app.route('/deleteMenuItem/<id>')
def deleteMenuItem(id):
item = MenuItem.query.get(id)
db.session.delete(item)
db.session.commit()
return redirect(url_for('index'))
# Delete a Restaurant
@app.route('/deleteRestaurant/<id>')
def deleteRestaurant(id):
item = Restaurant.query.get(id)
db.session.delete(item)
db.session.commit()
return redirect(url_for('index'))
# Delete a Rater Item
@app.route('/deleteRater/<id>')
def deleteRater(id):
item = Rater.query.get(id)
db.session.delete(item)
db.session.commit()
return redirect(url_for('index'))
# Get e)
@app.route('/e', methods=['GET'])
def get_e():
data = db.engine.execute(text(queries['e'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get f)
@app.route('/f', methods=['GET'])
def get_f():
data = db.engine.execute(text(queries['f'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get g)
@app.route('/g', methods=['GET'])
def get_g():
data = db.engine.execute(text(queries['g'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get h)
@app.route('/h', methods=['GET'])
def get_h():
data = db.engine.execute(text(queries['h'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get i)
@app.route('/i', methods=['GET'])
def get_i():
data = db.engine.execute(text(queries['i'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get j)
@app.route('/j', methods=['GET'])
def get_j():
data = db.engine.execute(text(queries['j'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get k)
@app.route('/k', methods=['GET'])
def get_k():
data = db.engine.execute(text(queries['k'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get l)
@app.route('/l', methods=['GET'])
def get_l():
data = db.engine.execute(text(queries['l'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get m)
@app.route('/m', methods=['GET'])
def get_m():
data = db.engine.execute(text(queries['m'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get n)
@app.route('/n', methods=['GET'])
def get_n():
data = db.engine.execute(text(queries['n'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
# Get o)
@app.route('/o', methods=['GET'])
def get_o():
data = db.engine.execute(text(queries['o'])).fetchall()
print(data)
return json.dumps([dict(r) for r in data], default=alchemyencoder)
def alchemyencoder(obj):
if isinstance(obj, datetime.date):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return float(obj)
if __name__ == "__main__":
app.run(debug=True)