-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
186 lines (151 loc) · 5.27 KB
/
app.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
import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from sqlalchemy.sql.base import NO_ARG
from sqlalchemy.sql.functions import count
from database.models import create_db, City, Rest
from sqlalchemy import func, or_
from auth.auth import AuthError, requires_auth
import json
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__)
create_db(app)
CORS(app)
@app.route('/')
def home_page():
return 'Welcome. I hope you will have fun!'
@app.route('/cities', methods=['GET'])
def get_all_city():
all_city_query = City.query.all()
city_formated = [city.format() for city in all_city_query]
count = len(city_formated)
if count == 0:
abort(404)
return jsonify({
'cities': city_formated,
'count': count
})
@app.route('/restaurants', methods=["GET"])
def get_all_restaurants():
all_restaurants_query = Rest.query.all()
restaurant_formated = [rest.format() for rest in all_restaurants_query]
count = len(restaurant_formated)
if count == 0:
abort(404)
return jsonify({
'restaurants': restaurant_formated,
'count': count
})
@app.route('/restaurants/<int:id>', methods=['DELETE'])
@requires_auth('delete:restaurants')
def delete_restaurant(payload, id):
restaurant = Rest.query.filter(Rest.id == id).one_or_none()
if restaurant is None:
abort(404)
try:
restaurant.delete()
return jsonify({
'deleted': restaurant.id
})
except BaseException:
abort(422)
@app.route('/restaurants', methods=['POST'])
@requires_auth('post:restaurants')
def post_new_restaurant(payload):
data = request.get_json()
n_name = data.get('name', None)
n_description = data.get('description', None)
n_menu = data.get('menu', None)
n_city = data.get('city', None)
try:
new_city = City.query.filter(func.lower(
City.name) == func.lower(n_city)).one_or_none()
if new_city is None:
new_city = City(name=n_city)
new_city.insert()
n_menu = "[" + json.dumps(n_menu) + "]"
new_restaurant = Rest(
name=n_name,
description=n_description,
menu=n_menu,
city_id=new_city.id)
new_restaurant.insert()
new_restaurant_formated = [new_restaurant.format()]
return jsonify({
'restaurant': new_restaurant_formated
})
except BaseException:
abort(422)
@app.route('/restaurants/<int:id>', methods=["PATCH"])
@requires_auth('patch:restaurants')
def update_restaurant(payload, id):
rest = Rest.query.filter(Rest.id == id).one_or_none()
if rest is None:
abort(404)
try:
data = request.get_json()
n_name = data.get('name', None)
n_description = data.get('description', None)
n_menu = data.get('menu', None)
n_city = data.get('city', None)
if n_name is not None:
rest.name = n_name
if n_description is not None:
rest.description = n_description
if n_menu is not None:
n_menu = "[" + json.dumps(n_menu) + "]"
rest.menu = n_menu
if n_city is not None:
new_city = City.query.filter(func.lower(
City.name) == func.lower(n_city)).one_or_none()
if new_city is None:
new_city = City(name=n_city)
new_city.insert()
rest.city_id = new_city.id
rest.update()
restaurant_formated = [rest.format()]
return jsonify({
'restaurant': restaurant_formated
})
except BaseException:
abort(422)
@app.route('/cities/<int:id>/restaurants', methods=["GET"])
def restaurants_by_city(id):
restaurants_query = Rest.query.filter(Rest.id == id)
restaurants_formated = [rest.format() for rest in restaurants_query]
count = len(restaurants_formated)
if count == 0:
abort(404)
return jsonify({
'restaurants': restaurants_formated,
'count': count
})
@app.errorhandler(404)
def not_found(error):
return (jsonify({
'error': 404,
'message': 'not found'
}), 404)
@app.errorhandler(422)
def unprocessable(error):
return (jsonify({
'error': 422,
'message': 'unprocessable'
}), 422)
@app.errorhandler(400)
def bad_request(error):
return (jsonify({
'error': 400,
'message': 'bad request'
}), 400)
@app.errorhandler(AuthError)
def handle_auth_error(ex):
response = jsonify(ex.error)
response.status_code = ex.status_code
return response
return app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)