-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
206 lines (177 loc) · 5.77 KB
/
app.py
File metadata and controls
206 lines (177 loc) · 5.77 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from flask import Flask,request
from flask import jsonify
import firebase_admin
from firebase_admin import credentials,firestore
from geopy.geocoders import Nominatim
from flask_cors import CORS,cross_origin
geolocator = Nominatim(user_agent="Headout-App")
import json
app = Flask(__name__)
CORS(app)
cred = credentials.Certificate("serviceAccount.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
product_ref = db.collection('products')
@app.route('/')
def index():
response = jsonify("Hello World")
response.headers.add("Access-Control-Allow-Origin", "*")
return response
'''
Sign Up API
1. This is to sign up the user.
2. Data it receives (json)
vendorName : name (string)
vendorId: id (int)
vendorGovRegsNumber: number(int)
vendorPassword: password(string)
vendorAddress: address(string)
vendorEmail: email(string)
vendorNumber: number(int)
3. Returns
Success/Error
'''
vendors = db.collection('vendors')
@app.route('/signup',methods=['POST'])
@cross_origin()
def sign_up():
data = request.json
print(data)
print("ji")
try:
print("hello")
vendorId = int(data['vendorId'])
print(vendorId)
location = geolocator.geocode(data['vendorAddress'])
print(location)
data['vendorLatitude'] = location.latitude
data['vendorLongitude'] = location.longitude
vendors.document(str(vendorId)).set(data)
response = jsonify({"success":True})
return response
except Exception as e:
return f"An Error Occured: {e}",404
'''
Log In API:
1. This is the LogIn API
2. Data it receives
vendorId : vendorId(string)
vendorPassword: password(string)
3. Returns (json)
success -> True/False
if success == True
returns "vendorId"
else:
returns "details"
'''
@app.route('/login',methods=['GET','POST'])
@cross_origin()
def login():
data = request.json
try:
vendorId = int(data['vendor_id'])
vendor = vendors.document(str(vendorId)).get().to_dict()
print(vendor)
if vendor == None:
return {"success":False,"details":"Vendor does not exist"}, 404
else:
actualPassword = vendor['vendorPassword']
sentPassword = data['password']
if actualPassword == sentPassword:
response = jsonify({"success":True,"vendorId":vendor['vendorId'],"vendorEmail":vendor['vendorEmail'],"vendorName":vendor['vendorName']})
return response
else:
response = jsonify({"success":False, "details":"Invalid password"})
return response
except Exception as e:
return f"An Error Occured: {e}"
'''
1.This API returns all the orders that a vendor has.
2. API receives as a query parameter
the vendorId
3. Based on the vendorId it receives all the orders in JSON format
{
"orders":[
{
"orderId":1234(integer)
"orderBy":name(string)
"orderAddress":address(string)
"orderPhoneNo":83848529439(int)
"orderProcessed":false(boolean)
},
{
"orderId":1235(integer)
"orderBy":name(string)
"orderAddress":address(string)
"orderPhoneNo":83848529439(int)
"orderProcessed":false(boolean)
},
{
"orderId":1236(integer)
"orderBy":name(string)
"orderAddress":address(string)
"orderPhoneNo":83848529439(int)
"orderProcessed":false(boolean)
}
]
}
'''
@app.route('/getAllOrders',methods=['GET'])
@cross_origin()
def getAllOrders():
args = request.args
orders = db.collection('orders')
try:
vendorOrderCollection = orders.document(args['vendorId']).collection('orders')
all_vendor_orders = [order.to_dict() for order in vendorOrderCollection.stream()]
response = jsonify(all_vendor_orders)
return response
except Exception as e:
return f"An Error Occured: {e}"
'''
1. API to mark dispatched
2. Receives
vendorId
orderId
3. returns response success or not
'''
@app.route('/markProcessed',methods=['GET'])
@cross_origin()
def markProcessed():
vendorId = request.args['vendorId']
orderId = request.args['orderId']
orders = db.collection('orders')
try:
allOrders = orders.document(vendorId).collection('orders')
allOrders.document(orderId).update({"orderProcessed":True})
response = jsonify({"success":True})
return response
except Exception as e:
return f"An Error Occured: {e}"
'''
1. An API to get details of a particular order.
2. receives
orderId -> orderId(int)
vendorId -> vendorId(int)
3. Retruns details fo particular order
{
"orderDetails":[
Array of orders -> This is the order
]
}
'''
@app.route('/specificOrder')
@cross_origin()
def specificOrder():
vendorId = request.args['vendorId']
orderId = request.args['orderId']
orders = db.collection('orders')
try:
allOrders = orders.document(vendorId).collection('orders')
order = allOrders.document(orderId).get().to_dict()
response = jsonify(order)
return response
except Exception as e:
return f"An Error Occured: {e}"
if __name__ == '__main__':
app.run(debug=True,port=8080)