-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdummy_onlinestore.py
93 lines (68 loc) · 2.37 KB
/
dummy_onlinestore.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
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
stores = [
{
'name' : 'Online Shopping Store',
'items' : [
{
'name' : 'My items',
'price' : 20.78
}
]
}
]
@app.route('/')
def home():
return render_template('index.html')
#for server - (flask sever)
#POST - used to receive data
#GET - used to send data back only
#for browser
#POST - used to send us data
#GET - used to receive data
#endpoints-
#POST /store data: {name:} - creates a new store with a given name
@app.route('/store', methods=['POST'])
def create_store():
request_data = request.get_json() #req made to endpoint
new_store = {
'name': request_data['name'],
'items': []
}
stores.append(new_store)
return jsonify(new_store)
#GET /store/<string:name> - get a store for a given name and return some data about it
@app.route('/store/<string:name>', methods=['GET'])
def get_store(name):
#iterate over store
# if store name matches return it
#if not, return an error message
for store in stores:
if store['name'] == name:
return jsonify(store)
return jsonify({'message':'store not found!'})
#GET /store - returns the list of all the stores
@app.route('/store', methods=['GET'])
def get_stores():
return jsonify({'stores': stores})
#POST /store/<string:name>/item data: {name:, price:} - creates an item in a specific store with a given name
@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!'})
#GET /store/<string:name>/item - gets all the item in a specific store
@app.route('/store/<string:name>/item', methods=['GET'])
def get_item_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)