-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
40 lines (29 loc) · 1.29 KB
/
app.py
File metadata and controls
40 lines (29 loc) · 1.29 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
from flask import Flask, jsonify, request, abort
from inspector import Inspector
app = Flask(__name__)
@app.route("/search", methods=["GET"])
def search():
if not request.args:
abort(501)
restaurant_name = request.args.get('restaurant_name')
zipcode = request.args.get('zipcode')
cuisine = request.args.get('cuisine')
limit = request.args.get('limit', default=10, type=int)
try:
inspections = list(Inspector.get_inspections())
except Exception as e:
return jsonify({"error": "Failed to retrieve inspections", "details": str(e)}), 500
filtered_results = inspections
if restaurant_name:
filtered_results = [ins for ins in filtered_results if restaurant_name.lower() in ins.restaurant_name.lower()]
if zipcode:
filtered_results = [ins for ins in filtered_results if ins.zipcode == zipcode]
if cuisine:
filtered_results = [ins for ins in filtered_results if cuisine.lower() in ins.cuisine.lower()]
filtered_results.sort(key=lambda x: x.restaurant_id)
limited_results = filtered_results[:limit]
if not limited_results:
return jsonify({"data": []}), 404
return jsonify(format_response(limited_results))
if __name__ == "__main__":
app.run(host="localhost", debug=True, port=8080)