-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
152 lines (127 loc) · 4.99 KB
/
main.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
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
from merchants import merchants
from products import products
from tracking import tracking
from mockTracking import mockTracking
# Get Data Routes
@app.route('/merchants')
def getmerchants():
# return jsonify(merchants)
return jsonify({'merchants': merchants})
@app.route('/merchants/<string:merchant_id>')
def getmerchant(merchant_id):
merchantsFound = [
merchant for merchant in merchants if merchant['id'] == merchant_id.lower()]
if (len(merchantsFound) > 0):
return jsonify({'merchant': merchantsFound[0]})
return jsonify({'message': 'merchant Not found'})
# Create Data Routes
@app.route('/merchants', methods=['POST'])
def addmerchant():
new_merchant = {
'id': request.json['id'],
'name': request.json['name'],
'lattitude': request.json['lattitude'],
'longitude': request.json['longitude'],
'address': request.json['address'],
}
merchants.append(new_merchant)
return jsonify({'merchants': merchants})
# Update Data Route
@app.route('/merchants/<string:merchant_id>', methods=['PUT'])
def editmerchant(merchant_name):
merchantsFound = [merchant for merchant in merchants if merchant['name'] == merchant_id]
if (len(merchantsFound) > 0):
merchantsFound[0]['id'] = request.json['id']
merchantsFound[0]['name'] = request.json['name']
merchantsFound[0]['lattitude'] = request.json['lattitude']
merchantsFound[0]['longitude'] = request.json['longitude']
merchantsFound[0]['address'] = request.json['address']
return jsonify({
'message': 'merchant Updated',
'merchant': merchantsFound[0]
})
return jsonify({'message': 'merchant Not found'})
# DELETE Data Route
@app.route('/merchants/<string:merchant_name>', methods=['DELETE'])
def deletemerchant(merchant_name):
merchantsFound = [merchant for merchant in merchants if merchant['name'] == merchant_name]
if len(merchantsFound) > 0:
merchants.remove(merchantsFound[0])
return jsonify({
'message': 'merchant Deleted',
'merchants': merchants
})
### product
@app.route('/products')
def getproducts():
# return jsonify(products)
return jsonify({'products': products})
@app.route('/products/<string:product_id>')
def getproduct(product_id):
productsFound = [
product for product in products if product['id'] == product_id.lower()]
if (len(productsFound) > 0):
return jsonify({'product': productsFound[0]})
return jsonify({'message': 'product Not found'})
# Create Data Routes
@app.route('/products', methods=['POST'])
def addproduct():
new_product = {
'id': request.json['id'],
'merchant': request.json['merchant'],
'name': request.json['name'],
'description': request.json['description'],
'price': request.json['price'],
'currency': request.json['currency'],
'shippingCosts': request.json['shippingCosts'],
'img': request.json['img'],
}
products.append(new_product)
return jsonify({'products': products})
# Update Data Route
@app.route('/products/<string:product_id>', methods=['PUT'])
def editproduct(product_name):
productsFound = [product for product in products if product['name'] == product_id]
if (len(productsFound) > 0):
productsFound[0]['id'] = request.json['id']
productsFound[0]['merchant'] = request.json['merchant']
productsFound[0]['name'] = request.json['name']
productsFound[0]['description'] = request.json['description']
productsFound[0]['price'] = request.json['price']
productsFound[0]['currency'] = request.json['currency']
productsFound[0]['store'] = request.json['store']
productsFound[0]['shippingCosts'] = request.json['shippingCosts']
return jsonify({
'message': 'product Updated',
'product': productsFound[0]
})
return jsonify({'message': 'product Not found'})
# DELETE Data Route
@app.route('/products/<string:product_name>', methods=['DELETE'])
def deleteproduct(product_name):
productsFound = [product for product in products if product['name'] == product_name]
if len(productsFound) > 0:
products.remove(productsFound[0])
return jsonify({
'message': 'product Deleted',
'products': products
})
## Tracking
@app.route('/tracking')
def getTracking():
# return jsonify(merchants)
return jsonify({'tracking': tracking})
## This might become a external adapter
# This function should return the list of all the producs that need funding
# that means products that delivered = true and paid = false
# Right now is just a mock
@app.route('/trackingNeedFunding')
def getFundingNeeded():
# return jsonify(merchants)
return jsonify({'mockTracking': mockTracking})
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))