-
Notifications
You must be signed in to change notification settings - Fork 15
/
api.py
77 lines (62 loc) · 1.74 KB
/
api.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
from flask import Flask, jsonify, request
app = Flask(__name__)
stores = [
{
'name': 'beautiful store',
'items': [
{
'name': 'flowers',
'price': 100
}
]
},
{
'name': 'beautiful store 2',
'items': [
{
'name': 'books',
'price': 100
}
]
}
]
@app.route('/')
def home():
return "Hello to Api"
@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(name):
for store in stores:
if(store['name'] == name):
return jsonify(store)
return jsonify({'message': 'store not found'})
@app.route('/store')
def get_all_store_name():
return jsonify({'stores': stores})
@app.route('/store/<string:name>/item', methods=['POST'])
def create_store_item(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_store_item(name):
for store in stores:
if(store['name'] == name):
return jsonify(store['items'])
return jsonify({'message': 'store not found'})
app.run(port=5000)