-
Notifications
You must be signed in to change notification settings - Fork 0
/
KaideeDB.py
341 lines (297 loc) · 10.8 KB
/
KaideeDB.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from flask import jsonify
from flask import request
import mysql.connector
from mysql.connector import errorcode
import mysql_config
import pprint as pp
#app = Flask(__name__)
# [IMPORTANT!!!!!!!]
# FIXME: @Deprecated, need to safely remove
def queryDB(query):
cnx = mysql.connector.connect(**mysql_config.config)
cursor = cnx.cursor(dictionary=True)
cursor.execute(query)
result = dict()
url = []
#row = dict(zip(cursor.column_names, cursor.fetchone()))
for row in cursor:
result.update(row)
url.append(row["url"])
result.update({"url":url})
cursor.close()
cnx.close()
return result
def queryDB2(query):
cnx = mysql.connector.connect(**mysql_config.config)
cursor = cnx.cursor(dictionary=True)
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
cnx.close()
return result
# FIXME: might be able to optimize this so no need to do another loop to extract the data
def resolveImg(result):
for product in result:
product['url'] = []
r = queryDB2('SELECT DISTINCT url FROM product_images_relation pir NATURAL JOIN product_images pi WHERE pir.iid = "{}"'.format(product['iid']))
for row in r:
product['url'].append(row['url'])
def resolveRating(products):
for product in products:
r = queryDB2('''
SELECT AVG(rating) as avg_rating, COUNT(rating) as count FROM feedbacks WHERE seller_uid = '{}';
'''.format(product['seller_uid']))
for row in r:
# only start showing rating info once 5
# or more rating is collected
if (row['count'] > 5):
product['rating'] = row['avg_rating']
product['rating_count'] = row['count']
else:
product['rating'] = 0
product['rating_count'] = 0
def resolveProducts(products):
resolveImg(products)
resolveRating(products)
def insertDB(query):
cnx = mysql.connector.connect(**mysql_config.config)
cursor = cnx.cursor()
cursor.execute(query)
result = []
cnx.commit()
cursor.close()
cnx.close()
return result
# example localhost:5000/ChatRooms/123
#@app.route('/ChatRooms', methods=['POST'])
def getChatRooms(uid):
# those that we are the buyer
query = ('''
SELECT
cr.buyer_uid,
cr.seller_uid,
cr.iid, s.sid as service_id,
name as service_name,
CONCAT(u.first_name, ' ', u.last_name) as sender_name,
u.profile_pic,
(SELECT message FROM chat_messages cm WHERE
cr.seller_uid = cm.seller_uid
AND cr.buyer_uid = cm.buyer_uid
AND cr.iid = cm.iid
ORDER BY cm.timestamp DESC LIMIT 1)
as msg
FROM chat_rooms cr
LEFT JOIN service_offering so ON cr.seller_uid = so.seller_uid AND cr.buyer_uid = so.buyer_uid AND cr.iid = so.iid
LEFT JOIN services s ON so.sid = s.sid
LEFT JOIN users u ON u.uid = cr.seller_uid
WHERE cr.buyer_uid = '{0}';
'''.format(uid))
result = queryDB2(query)
# those that we are the seller
query = ('''
SELECT
cr.buyer_uid,
cr.seller_uid,
cr.iid, s.sid as service_id,
name as service_name,
CONCAT(u.first_name, ' ', u.last_name) as sender_name,
u.profile_pic,
(SELECT message FROM chat_messages cm WHERE
cr.seller_uid = cm.seller_uid
AND cr.buyer_uid = cm.buyer_uid
AND cr.iid = cm.iid
ORDER BY cm.timestamp DESC LIMIT 1)
as msg
FROM chat_rooms cr
LEFT JOIN service_offering so ON cr.seller_uid = so.seller_uid AND cr.buyer_uid = so.buyer_uid AND cr.iid = so.iid
LEFT JOIN services s ON so.sid = s.sid
LEFT JOIN users u ON u.uid = cr.buyer_uid
WHERE cr.seller_uid = '{0}';
'''.format(uid))
result += queryDB2(query)
return result
# example localhost:5000/Messages/123
#@app.route('/ChatMessages', methods=['POST'])
def get_Messages(json):
query = ("SELECT * FROM chat_messages cm WHERE cm.buyer_uid = '{}' AND cm.seller_uid = '{}' AND cm.iid = '{}';".format(
json['buyer_uid'],json['seller_uid'],json['iid']
))
result = queryDB2(query)
# set sender uid for frontend
for msg in result:
if msg['from_buyer']:
msg['sender'] = msg['buyer_uid']
else:
msg['sender'] = msg['seller_uid']
return jsonify({'result' : result})
# will finish
# LOL
#@app.route('/Messages/', methods=['POST'])
def post_Messages():
query = ("INSERT INTO ChatMessages")
result = insertDB(query)
return jsonify({'result' : result})
# send uid return uid,first_name,last_name,phone,email,profile_pic
# example localhost:5000/Users/123
#@app.route('/Users/<uid>', methods=['GET'])
def get_Users(uid):
query = ("SELECT * FROM Users WHERE uid =\'" + uid + "\'")
result = queryDB(query)
return jsonify({'result' : result})
# will need to change to POST will make it by the end of the week
# will fix later don't know how to autogen fid
#@app.route('/Feedback/<buyer_uid>/<seller_uid>/<rating>/<comments>/<iid>', methods=['GET'])
# ALREADY DONE WAY AT THE BOTTOM!
def post_Feedback(buyer_uid,seller_uid,rating,comments):
query = ("INSERT INTO Feedback")
result = insertDB(query)
return jsonify({'result' : result})
#need to get seller_uid and do another query for more seller information possible for frontend to call getUsers again
#will return iid, name, description, price, cid, since, seller_uid, product_location
#might need another table to collect product location
#@app.route('/Product/<iid>', methods=['GET'])
def get_Product(iid):
query = ('''
SELECT i.iid, i.name, i.description,
i.price, c.cid, c.name as cname, a.address,
a.city, a.province, u.first_name, u.last_name,
u.uid as seller_uid, u.phone, i.since
FROM item_listing i
LEFT JOIN categories c ON i.cid=c.cid
LEFT JOIN addresses a ON i.seller_uid=a.uid
LEFT JOIN users u ON i.seller_uid=u.uid
WHERE i.iid="''' + iid +'"')
result = queryDB2(query)
resolveImg(result)
resolveRating(result)
if(len(result) > 0):
return jsonify({'result': result[0], 'status': 200})
return jsonify({'result': {}, 'status': 404})
#send iid return iid,img_id,url,name for all of images for that item
#@app.route('/Images/<iid>', methods=['GET'])
def get_Images(iid):
query = ("SELECT * FROM ProductImages WHERE iid = \'"+ iid +"\'")
result = queryDB(query)
return jsonify({'result' : result})
#FIXME: shitty performance code... need to optimize
# @app.route('/ProductForCategory/<cid>', methods=['GET'])
# FIXME: missing images
def get_product_category(cid):
query = ('''
SELECT i.iid, i.name, i.description,
i.price, c.cid, c.name as cname, a.address,
a.city, a.province, u.first_name, u.last_name,
u.uid as seller_uid, u.phone, i.since
FROM item_listing i
LEFT JOIN categories c ON i.cid=c.cid
LEFT JOIN addresses a ON i.seller_uid=a.uid
LEFT JOIN users u ON i.seller_uid=u.uid
WHERE i.cid= "{}" ORDER BY i.since DESC'''.format(cid))
result = queryDB2(query)
resolveImg(result)
resolveRating(result)
return result
#need to discuss later
#@app.route('/RecommendedProducts/<uid>', methods=['GET'])
def get_Recommended(uid):
query = ("")
result = queryDB(query)
return jsonify({'result' : result})
# @app.route('/Category', methods=['GET'])
def get_category_list():
query = ('SELECT * FROM `categories`')
result = queryDB2(query)
return jsonify({'result' : result})
# @app.route('/SubmitFeedback', methods=['POST'])
def post_submit_feedback(feedback):
query = '''
INSERT INTO `feedbacks`(`fid`, `timestamp`, `rating`, `comment`, `buyer_uid`, `seller_uid`, `iid`)
VALUES (UUID(), SYSDATE(), {}, "{}", "{}", "{}", "{}")
'''.format(feedback['rating'], feedback['comment'], feedback['buyer_uid'], feedback['seller_uid'], feedback['iid'])
result = insertDB(query)
return jsonify({'result': 'success'})
def get_avg_rating(sid):
query = 'SELECT AVG(rating) as avg_rating, COUNT(rating) as count FROM feedbacks WHERE seller_uid = "{}";'.format(sid)
result = queryDB2(query);
if(len(result) > 0):
return jsonify({'result': result[0], 'status': 200})
return jsonify({'result': {}, 'status': 404})
def post_track_user_data(json):
# Insert if not already exist, else update to increament count
query = ('''
INSERT INTO `views` (`cid`, `uid`, `count`)
VALUE ('{0}', '{1}', 1) ON DUPLICATE KEY
UPDATE `views`.`count` = `views`.`count` + 1
'''.format(json['cid'], json['uid']))
insertDB(query)
return jsonify({'result': 'success'})
def get_random_product():
query = ('''
SELECT i.iid, i.name, i.description,
i.price, c.cid, c.name as cname, a.address,
a.city, a.province, u.first_name, u.last_name,
u.uid as seller_uid, u.phone, i.since
FROM item_listing i
LEFT JOIN categories c ON i.cid=c.cid
LEFT JOIN addresses a ON i.seller_uid=a.uid
LEFT JOIN users u ON i.seller_uid=u.uid
ORDER BY RAND() LIMIT 10
''')
result = queryDB2(query)
resolveImg(result)
resolveRating(result)
return result
def post_create_chat_room(json):
query = ('''
INSERT IGNORE INTO `chat_rooms`(`buyer_uid`, `seller_uid`, `iid`) VALUES ('{}', '{}', '{}');
'''.format(json['bid'], json['sid'], json['iid']))
insertDB(query);
return jsonify({'result' : 'success'})
def get_top_five_categories(uid):
query = ('SELECT cid, count FROM views WHERE uid = "{}" ORDER BY count DESC LIMIT 5;'.format(uid))
result = queryDB2(query)
return result
def get_global_top_five():
query = ('SELECT cid, SUM(count) as count FROM views GROUP BY cid ORDER BY count DESC LIMIT 5;')
result = queryDB2(query)
return result
def get_product_count(cid):
query = ('SELECT COUNT(iid) as count FROM `item_listing` WHERE cid = "{}";'.format(cid))
result = queryDB2(query)
return result[0]
def get_category_info(cid):
query = ('SELECT * FROM `categories` WHERE cid = "{}";'.format(cid))
result = queryDB2(query)
return result[0]
def storeMessage(msg):
query = '''
INSERT INTO `chat_messages`(`buyer_uid`, `seller_uid`, `iid`, `message`, `flag`, `timestamp`, `from_buyer`)
VALUES ('{}', '{}', '{}', '{}', {}, SYSDATE(), {})
'''.format(msg['buyer_uid'],msg['seller_uid'],msg['iid'],msg['message'],msg['flag'],msg['from_buyer'])
insertDB(query)
def gen_sql_uuid():
query = 'SELECT UUID() as uuid'
result = queryDB2(query)
return result[0]['uuid']
def post_product(product):
# gen uuid
uuid = gen_sql_uuid();
# insert the product
query = '''
INSERT INTO `item_listing`(`iid`, `name`, `description`, `price`, `cid`, `since`, `seller_uid`, `product_location`) VALUES
('{}', '{}', '{}', '{}', '{}', SYSDATE(), '{}', '{}')
'''.format(
uuid,
product['name'],
product['description'],
product['price'],
product['category']['cid'],
product['seller']['sid'],
product['location']['province']
)
result = insertDB(query);
# # insert images
# for img_url in product['_img_urls']:
# qquery = 'INSERT INTO `product_images`(`img_id`, `url`, `name`) VALUES (UUID(), "{}", "images")'.format(img_url)
# insertDB(qquery)
return result;