-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
67 lines (60 loc) · 1.53 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
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')
# POST /store data:{name:}
@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)
# GET /store/<string:name>
@app.route('/store/<string:name>')
def get_store(name):
# Iterate over stores
for store in stores:
if store['name'] == name:
return jsonify(store)
return jsonify({'message': 'store not found.'})
# if the store name matches, return it
# If none match, return an error message
# GET /store
@app.route('/store')
def get_stores():
return jsonify({'stores': stores})
# POST /store/<string:name>/item
@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:
store['items'].append({
'name': request_data['name'],
'price': request_data['price']
})
return jsonify(store)
return jsonify({'message': 'store not found'})
# GET /store/<string:name>/item
@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)