-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
168 lines (141 loc) · 4.95 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
from flask import Flask
from flask import request, jsonify, make_response
from flask_httpauth import HTTPBasicAuth
from flask_sqlalchemy import SQLAlchemy
from authenticate import auth
import os
app = Flask(__name__)
db=SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dishes.sqlite3'
#create a table dishes in the database
class Dishes(db.Model):
__tablename__ = 'dishes'
id = db.Column(
db.Integer,
# UUID(as_uuid=True),
primary_key=True,
# default=uuid.uuid4,
unique=True
)
dish_name = db.Column(
db.String(100),
index=True,
unique=True,
nullable=False
)
dish_cost = db.Column(
db.String(100),
index=True,
nullable=False
)
dish_image=db.Column(
db.String(100),
index=True,
nullable=False)
def __init__(self,dish_name,dish_cost,dish_image):
self.dish_name=dish_name
self.dish_cost=dish_cost
self.dish_image=dish_image
#This error handler is used to display the
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
#This class is used to serialize all the dishes in our catalog
def menu(dish):
return {"dishID":dish.id,
"dishName":dish.dish_name,
"dishCost":dish.dish_cost,
"dishImage":dish.dish_image}
# A route to return all of the available dishes in our catalog
@app.route('/app/v1/resources/dishes', methods=['GET'])
@auth.login_required
def api_all():
all_dishes=Dishes.query.all()
all_dishes=list(map(menu,all_dishes))
return jsonify(all_dishes)
# A route to add new dish to the dishes in our catalog
@app.route('/app/v1/resources/dishes', methods=['POST'])
@auth.login_required
def add_dish():
dishName = request.form["dishName"]
dishCost = request.form["dishCost"]
if request.files:
dishImage = request.files["dishImage"]
dishImage.save(os.path.join(dishImage.filename))
dish=Dishes(dishName,dishCost,dishImage.filename)
db.session.add(dish)
db.session.commit()
return jsonify(dish.id)
# A route to delete all dishes in our catalog
@app.route('/app/v1/resources/dishes', methods=['DELETE'])
@auth.login_required
def api_delete_all():
dishes=Dishes.query.all()
if dishes == None:
return make_response(jsonify({'error': 'No dish'}), 400)
del_list=[]
for dish in dishes:
del_list.append(dish.id)
db.session.delete(dish)
db.session.commit()
return jsonify(del_list)
# A route to display a dish in our catalog with matching dish id
@app.route('/app/v1/resources/dishes/<int:dish_id>', methods=['GET'])
@auth.login_required
def get_dish(dish_id):
dish=Dishes.query.filter_by(id=dish_id).first()
if dish == None:
return make_response(jsonify({'error': 'No dish'}), 400)
else:
return jsonify(menu(dish))
# A route to update a dish in our catalog with matching dish id
@app.route('/app/v1/resources/dishes/<int:dish_id>', methods=['PUT'])
@auth.login_required
def update_dish(dish_id):
dish=Dishes.query.filter_by(id=dish_id).first()
if dish == None:
return make_response(jsonify({'error': 'No dish'}), 400)
else:
dishName = request.form["dishName"]
dish.dish_name=dishName
dishCost = request.form["dishCost"]
dish.dish_cost=dishCost
if "dishImage" in request.files:
dishImage = request.files["dishImage"]
dishImage.save(os.path.join(dishImage.filename))
dish.dish_image=dishImage.filename
db.session.commit()
return jsonify(dish_id)
# A route to delete a dish in our catalog with matching dish id
@app.route('/app/v1/resources/dishes/<int:dish_id>', methods=['DELETE'])
@auth.login_required
def delete_task(dish_id):
dish=Dishes.query.filter_by(id=dish_id).first()
if dish == None:
return make_response(jsonify({'error': 'No dish'}), 400)
else:
dish_id=dish.id
db.session.delete(dish)
db.session.commit()
return jsonify(dish_id)
# A route to update partial entries of a dish in our catalog with matching dish id
@app.route('/app/v1/resources/dishes/<int:dish_id>', methods=['PATCH'])
@auth.login_required
def patch_dish(dish_id):
dish=Dishes.query.filter_by(id=dish_id).first()
if dish == None:
return make_response(jsonify({'error': 'No dish'}), 400)
else:
if "dishName" in request.form:
dish.dish_name=request.form["dishName"]
if "dishCost" in request.form:
dish.dish_cost=request.form["dishCost"]
if "dishImage" in request.files:
dishImage = request.files["dishImage"]
dishImage.save(os.path.join(dishImage.filename))
dish.dish_image=dishImage.filename
db.session.commit()
return jsonify(dish_id)
if __name__ == "__main__":
db.create_all()
app.run()