forked from luciengeorge/FlaskRESTApi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestaurant_routes.py
40 lines (35 loc) · 1.04 KB
/
restaurant_routes.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
from flask_restful import Resource, reqparse
from restaurant import Restaurant
parser = reqparse.RequestParser()
parser.add_argument("name", type=str)
class Restaurants(Resource):
def post(self):
data = parser.parse_args()
restaurant = Restaurant(data["name"])
restaurant.save()
return restaurant.json(), 201
def get(self):
return [r.json() for r in Restaurant.query.all()]
class RestaurantMember(Resource):
def get(self, id):
restaurant = Restaurant.find_by_id(id)
if restaurant:
return restaurant.json()
else:
return { "error": "Not found" }, 404
def patch(self, id):
restaurant = Restaurant.find_by_id(id)
data = parser.parse_args()
if restaurant:
restaurant.name = data["name"]
restaurant.save()
return restaurant.json()
else:
return { "error": "Not found" }, 404
def delete(self, id):
restaurant = Restaurant.find_by_id(id)
if restaurant:
restaurant.destroy()
return restaurant.json()
else:
return { "error": "Not found" }, 404