-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIRST.PY
61 lines (53 loc) · 1.51 KB
/
FIRST.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
from flask import Flask,jsonify,request,render_template
app = Flask(__name__)
stores=[
{
'name' : 'My wonderful store',
'items' : [
{
'name' : 'My item',
'price' : 15.99
}
]
}
]
@app.route('/')
def home():
return render_template('index.html')
@app.route('/store', methods=['POST'])
def create_store():
request_data = request.get_json()
new_store = {
'name' : request_data['name'],
'items' : []
}
stores.append(new_store)
return jsonify(new_store)
@app.route('/store/<string:name>')
def get_store(name):
for store in stores:
if store['name'] == name:
return jsonify(store)
return jsonify({'message' : 'store not found'})
@app.route('/store')
def get_stores():
return jsonify({'stores': stores})
@app.route('/store/<string:name>/item', methods=['POST'])
def create_item_in_store(name):
request_data = request.get_json()
for store in stores:
if store['name'] == name:
new_item = {
'name' : request_data['name'],
'price' : request_data['price']
}
store['items'].append(new_item)
return jsonify(new_item)
return jsonify({'message' : 'store not found'})
@app.route('/store/<string:name>/item')
def get_items_in_store(name):
for store in stores:
if store['name'] == name:
return jsonify({'items' : store['items']})
return jsonify({'message':'store not found'})
app.run(port=5000)