-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbFunctions.py
414 lines (391 loc) · 17.1 KB
/
dbFunctions.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from dictFunctions import *
import json
import os
from initializeDB import *
#decide whether a username/password combo works or not
def login(username, password):
sql = 'SELECT userId FROM usersTable WHERE username=? AND password=?'
result = c.execute(sql, (username, password))
try:
result = c.fetchone()
except:
return 0
if(result != None):
return result['userId']
else:
return 0
def getUserId(username):
sql = "SELECT userId FROM usersTable WHERE username=?"
result = c.execute(sql, (username,))
result = c.fetchone()
result = result['userId']
return result #should be an integer
#update the user's cart
#=======================================================
# int list str int
def addToCart(userId, inventoryId, quantity):
#find out if we have any duplicates
sql = "SELECT * FROM inventoryTable WHERE inventoryId=?"
result = c.execute(sql, (inventoryId,))
result = c.fetchall()
if(result == 0):
print("FAILED TO ADD ITEM")
return false #failed to add the item
else:
#put the item into a dictionary list
newItem = returnRowDicts(sql, inventoryId)
newItem = newItem[0] #just grab the dictionary
#see if the user already has a cart
sql = "SELECT * FROM cartTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchall()
if not result:
#the user has no cart. Make them one
print("USER ID: ", userId)
sql = "INSERT INTO cartTable(userId, items, total) VALUES(?, null, 0)"
c.execute(sql, (userId,))
conn.commit() #gotta commit the changes
#now everyone has a table. Let's grab their variables
sql = "SELECT * FROM cartTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchall()
userTotal = []
userItems = []
if(result):
for row in result:
userTotal.append(row['total'])
userItems.append(row['items'])
#print(userItems)
#print("usertotal", userTotal[row])
#print("useritems", userItems[row])
#they don't have any items. Can't append to null so fix it
if (userItems[0] == None):
#print("IN IF")
#print("GOT HERE")
userItems = []
userTotal = 0.0
else:
#print("IN ELSE")
#print("useritems: ", result['items'])
userItems = fromJSON(userItems[0])
userTotal = userTotal[0]
#fix the total
#print(userItems[0]['item'])
newItemPrice = newItem['price']
newItemCost = newItemPrice*quantity
userTotal += newItemCost
newItem['quantity'] = quantity #decide the quantity
#if the item exists already
itemNotFound = 1 #assume the item isn't in there
for x in range(len(userItems)):
if(inventoryId == userItems[x]['inventoryId']):
#print("GOT HERE")
userItems[x]['quantity'] += quantity
itemNotFound = 0 #we found the item
if(itemNotFound == 1):
#print("DIDNT FIND THE ITEM")
#append the new item
userItems.append(newItem)
userItems = toJSON(userItems) #gotta save it as a string
#update everything due to new item
sql = "UPDATE cartTable SET items=?, total=? WHERE userId=?"
result = c.execute(sql, (userItems, userTotal, userId))
conn.commit()
#print("GOT HERE")
return True #yayyyyy we did it
#deletefromcart function
#=================================================================
def deleteFromCart(userId, inventoryId, quantity):
#grab the user's cart
sql = "SELECT * FROM cartTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchone()
#see if the item is in the cart at all
userItems = result['items']
userTotal = result['total']
index = -1
userItems = fromJSON(userItems)
for x in range(len(userItems)):
if(inventoryId == userItems[x]['inventoryId']):
#print("Found index")
index = x #grab the index where the item is
if(index == -1):
print('failed')
print("inventoryId: ", userItems[x]['inventoryId'])
return False #looks like the item wasn't there
userItemsQuantity = userItems[index]['quantity']
userItemsPrice = userItems[index]['price']
userItemsCost = 0 #we're gonna figure this later
#print(userItemsQuantity)
#check how many the user wants to take out of cart
if(quantity >= userItemsQuantity):
userItems.pop(index) #remove that section entirely
#we're taking the whole thing out, so take the total of this item out of the cart total
userItemsCost = userItemsQuantity*userItemsPrice #this way someone doesn't delete items and get back money somehow
else:
userItemsQuantity -= quantity
userItemsCost = userItemsPrice*quantity #this value wasn't above how many was in the cart. So we're good
#let's update the database now
userItems[index]['quantity'] = userItemsQuantity #can't have this outside the else or we might try and change a quantity that deson't exist
userTotal -= userItemsCost
if(len(userItems) != 0):
userItems = toJSON(userItems)
else:
userItems = None
sql = "UPDATE cartTable SET items=?, total=? WHERE userId=?"
result = c.execute(sql, (userItems, userTotal, userId))
conn.commit()
return True #YEEHAW we did it
#Gotta check the actual total because people can add more to the cart
#than is in inventory. So let's get a real total and update inventory at the same time
def updateFinalTotalandInventory(userId):
sql = "SELECT * FROM cartTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchone()
userItems = result['items']
userItems = fromJSON(userItems) #remember to always switch from json
items = []
itemQuantities = []
#let's get all the names of the items and their quantity
for x in range(len(userItems)):
items.append(userItems[x]['inventoryId'])
itemQuantities.append(userItems[x]['quantity'])
#we got the user's items, let's get the items and their quantity from the
#inventory table
sqlInventory = "SELECT * FROM inventoryTable"
results = c.execute(sqlInventory)
results = c.fetchall() #we gotta use a fetchall here. So a bit more logic involved
#first, let's get a dictionary of the all the items.
#then we'll get a list of all the quantities
inventoryItems = []
inventoryQuantities = []
correctInventoryQuantities = [] #this is just the quantities of the IDs that are held in items[]
for row in results:
inventoryItems.append(row['inventoryId'])
inventoryQuantities.append(row['quantity'])
#alright, now we gotta do comparisons
totalChanged = False #we'll check if the total changed later
for i in range(len(userItems)): #for how many items are in the cart
for x in range(len(inventoryItems)): #check them against how many items there are in inventory
if(items[i] == inventoryItems[x]):
correctInventoryQuantities.append(inventoryQuantities[x])
if(itemQuantities[i] > inventoryQuantities[x]):
totalChanged = True #if we got here, the total changed
itemQuantities[i] = inventoryQuantities[x] #simply change the value in the cart. The user will never know until the order is placed
#we've got the inventory updated, let's update the cart
#this takes a bit more since all of our stuff is in dictionaries. so
finalTotal = 0
print("")
print("")
print("Checkout: \n====================================") #using this to prettify our purchase print()
for x in range(len(userItems)):
userItems[x]['quantity'] = itemQuantities[x]
#now we got that solved. Let's fix the total
finalTotal += userItems[x]['price']*userItems[x]['quantity']
#let's print out what the user bought
print(userItems[x]['quantity'], userItems[x]['item'], "at", userItems[x]['price'], "per unit")
#booyah. Let's update the cart and call it a day.
print("------------------------------------")
print("For a grand total of: $", ( "%5.2f"% finalTotal))
print("")
print("")
if(totalChanged == True):
print("Your cart had a few items over the stock quantity.")
print("Your total is going to be $", (( "%5.2f"% finalTotal)))
check = input("Would you like to continue checking out[y/n]?")
if(check == 'n'):
return False
else:
#now update the totals and the inventory
sqlInventoryUpdate = "UPDATE inventoryTable SET quantity=? WHERE inventoryId=?"
#we're gonna do multiple updates, so
for x in range(len(items)):
#print("ITEMS[x]: ", items[x], "inventoryQ[x]: ", correctInventoryQuantities[x], "invId: ", inventoryItems[x], "ITEMQ: ", itemQuantities[x])
c.execute(sqlInventoryUpdate, ((correctInventoryQuantities[x]-itemQuantities[x]), items[x] ))
conn.commit()
userItems = toJSON(userItems) #put it back in json format
sqlUpdateCart = "UPDATE cartTable SET items=?, total=? WHERE userId=?"
c.execute(sqlUpdateCart, (userItems, finalTotal, userId,))
conn.commit()
return True
##Woohoo, almost done.
#here's the checkout function that walks the user through checkout
#==================================================================
def checkout(userId):
print("")
print("")
print("Welcome to checkout! We hope you found everything you wanted!")
sqlCheckCart = "SELECT total FROM cartTable WHERE userId=?"
checkTotal = c.execute(sqlCheckCart, (userId,))
checkTotal = c.fetchone()
if(checkTotal['total'] == 0):
print("Your cart is empty. Cannot checkout")
print("")
print("press enter to continue")
input("")
return
#check to see if the user has a shipping address
sql = "SELECT * FROM usersTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchone()
check = 'n'
#the user has a shipping address
if(result['shippingAddress'] != None):
print("The package will be sent to", result['shippingAddress'])
check = input("Is this correct[y/n]?")
#either the user wanted to change their shipping address or didn't have one
if(result['shippingAddress'] == None or check == 'n'):
#it's a multi spaced input sooooooo
shippingAddress = list(map(str,input("Please enter your shipping address and hit enter.. ").split()))
shipAddString = ""
#make a string out of the list
for x in range(len(shippingAddress)):
shipAddString = shipAddString + " " + shippingAddress[x]
#update the shipping address in the database
sqlShipping = "UPDATE usersTable SET shippingAddress=? WHERE userId=?"
result = c.execute(sqlShipping, (shipAddString, userId,))
conn.commit()
print("The package will be sent to", shipAddString)
#grab the user's cart information
sql = "SELECT * FROM cartTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchone()
print("")
print("")
print("")
#get the user's credit card
while(True):
creditcard = input("Please type your 10 digit credit-card number: ")
try:
if(len(creditcard) == 10):
creditcard = int(creditcard)
break
except:
print("please type a 10 digit credit-card number ")
continue
print("")
print("")
#check the user's final total
check = updateFinalTotalandInventory(userId)
if(check == False):
print("your order has been canceled.")
print("")
print("")
return
else:
sqlChange = "SELECT * FROM cartTable WHERE userId=?"
change = c.execute(sql, (userId,))
change = c.fetchone()
#gotta be extra sure with a final yes or no
#we're gonna update the final total and such in the cart for the correct amount
#grab the user's cart again because the fields have changed since then
sql = "SELECT * FROM cartTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchone()
#store the order in the ordersTable
sql = "INSERT INTO ordersTable(userId, items, total, date, creditcard) VALUES(?, ?, ?, CURRENT_TIMESTAMP, ?)"
result = c.execute(sql, (userId, result['items'], result['total'], creditcard ))
conn.commit()
sql = "UPDATE cartTable SET items=NULL, total=0 WHERE userId=?" #this is just emptying out the cart for later uses.
result = c.execute(sql, (userId,))
conn.commit()
print("")
print("Congratulations on your purchase! You can view this order on the orders screen. Have a nice day!")
input("press enter to continue back to the inventory screen.")
#show inventory
def showInventory():
sql = "SELECT * FROM inventoryTable"
result = c.execute(sql)
result = c.fetchall()
#print the legend
print("")
print("")
print("")
print("ALIBABA BOOK STORE")
print("=============================================================================================================================================================")
print ("%-5s%-40s%-60s%-5s%27s%10s\n" % ("ID", "Item Name", "Description", "In Stock","|", "Price"))
for row in result:
print ("%-5s%-40s%-60s%-5s%30s%10s\n" % (row['inventoryId'], row['item'], row['description'], row['quantity'],"$", ( "%8.2f"% row['price'])))
#Looks like a lot of logic, but it's just some print statements
def showCart(userId):
#let's get the user's username for good looks
sqlGetUsername = "SELECT username FROM usersTable WHERE userId=?"
username = c.execute(sqlGetUsername, (userId,))
username = c.fetchone()
username = username['username']
#now let's get the cart
sql = "SELECT * FROM cartTable WHERE userId=?"
result = c.execute(sql, (userId,))
result = c.fetchone()
if not result['items']:
print("Looks like your cart is empty!")
print("")
print("Press return to leave.")
return
#gotta do some logic so we don't print out a bunch of json files
userItems = result['items']
userTotal = result['total']
userItems = fromJSON(userItems)
IdList = []
itemsList = []
quantityList = []
priceList = []
descriptionList = []
for x in range(len(userItems)):
IdList.append(userItems[x]['inventoryId'])
itemsList.append(userItems[x]['item'])
quantityList.append(userItems[x]['quantity'])
priceList.append(userItems[x]['price'])
descriptionList.append(userItems[x]['description'])
#spacing like the inventory page
print(username + "'s Cart")
print("==================================================================================================================================================================")
print ("%-5s%-60s%-40s%-5s%24s%10s\n" % ("ID", "Item Name", "Description", "Quantity","|", "Price"))
for x in range(len(userItems)):
print ("%-5s%-60s%-40s%-5s%27s%10s\n" % (str(IdList[x]), str(itemsList[x]), str(descriptionList[x]), str(quantityList[x]),"$", str(( "%8.2f"% priceList[x]))))
print("Total in Cart: $", ( "%8.2f"% userTotal))
print("")
def showOrders(userId):
print("")
print("")
sql = "SELECT * FROM ordersTable WHERE userId=?"
results = c.execute(sql, (userId,))
results = c.fetchall()
#if we have any results
if(results):
print ("%-5s%-60s%24s%20s\n" % ("ID", "Total", "Date", "Credit Card"))
for row in results:
print ("%-5s%-60s%24s%20s\n" % (row['orderId'],("%8.2f"% row['total']), row['date'], row['creditcard']))
#if we don't
else:
print("looks like you don't have any orders.")
def showUniqOrder(userId, orderId):
print("")
print("")
sql = "SELECT * FROM ordersTable WHERE orderId=?"
result = c.execute(sql, (orderId,))
result = c.fetchone()
if not result:
print("No order of that ID")
return
userItems = result['items']
userTotal = result['total']
userItems = fromJSON(userItems) #change from json into list
IdList = []
itemsList = []
quantityList = []
priceList = []
descriptionList = []
for x in range(len(userItems)):
IdList.append(userItems[x]['inventoryId'])
itemsList.append(userItems[x]['item'])
quantityList.append(userItems[x]['quantity'])
priceList.append(userItems[x]['price'])
descriptionList.append(userItems[x]['description'])
print ("%-5s%-60s%-40s%-5s%24s%10s\n" % ("ID", "Item Name", "Description", "Quantity","|", "Price"))
for x in range(len(userItems)):
if(quantityList[x] > 0):
print ("%-5s%-60s%-40s%-5s%27s%10s\n" % (str(IdList[x]), str(itemsList[x]), str(descriptionList[x]), str(quantityList[x]),"$", str(( "%8.2f"% priceList[x]))))
print("Final Total: $", ( "%8.2f"% userTotal))
print("")
print("")