-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
300 lines (259 loc) · 10.6 KB
/
db.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
import mysql.connector
from mysql.connector import errorcode
from datetime import datetime
MYSQL_HOST = 'localhost'
MYSQL_USER = 'root'
MYSQL_PASSWORD = 'test'
MYSQL_DB = 'mydb'
def get_categories() -> list[dict]:
conn = mysql.connector.connect(host=MYSQL_HOST, username=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DB)
my_cursor = conn.cursor()
# Fetch column names for the product_category table
table_name = 'Product_Category'
my_cursor.execute(f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{table_name}'")
column_names = my_cursor.fetchall()
column_names = [col[0] for col in column_names]
# Fetch records from the product_category table
my_cursor.execute(f"SELECT * FROM {table_name}")
records = my_cursor.fetchall()
# Construct dictionary with column names as keys and record values as values
result = []
for record in records:
record_dict = {}
for j in range(len(column_names)):
record_dict[column_names[j]]=record[j]
result.append(record_dict)
conn.close()
return result
def get_products(category_id: int) -> list[dict]:
# if category_id = -1, return all products.
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
table_name = "Product"
my_cursor.execute(f"SELECT * FROM {table_name}")
records=my_cursor.fetchall()
my_cursor.execute(f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{table_name}'")
column_names = my_cursor.fetchall()
column_names = [col[0] for col in column_names]
l=[]
if (category_id==0):
for i in records:
d={}
for j in range(len(column_names)):
d[column_names[j]]=i[j]
l.append(d)
else:
for i in records:
if (i[6]==category_id):
d={}
for j in range(len(column_names)):
d[column_names[j]]=i[j]
l.append(d)
return l
def get_product(product_id: int) -> dict:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
table_name='Product'
my_cursor.execute(f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{table_name}'")
column_names = my_cursor.fetchall()
column_names = [col[0] for col in column_names]
query = "SELECT * FROM Product WHERE ID = %s" # Use parameterized query to avoid SQL injection
my_cursor.execute(query, (product_id,))
record=my_cursor.fetchall()
l=[]
for i in record:
d={}
for j in range(len(column_names)):
d[column_names[j]]=i[j]
l.append(d)
conn.close()
return l
def get_user_details(username: str) -> dict:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
my_cursor.execute("SELECT * FROM customer WHERE PHONE_NUMBER=%s",(username,))
record=my_cursor.fetchall()
print(record)
table_name='customer'
my_cursor.execute(f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{table_name}'")
column_names = my_cursor.fetchall()
column_names = [col[0] for col in column_names]
d={}
print(column_names)
for i in record:
for j in range(len(column_names)):
d[column_names[j]]=i[j]
return d
def get_cart(username: str) -> list[dict]:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
table_name = 'Cart'
my_cursor.execute(f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{table_name}'")
column_names = my_cursor.fetchall()
column_names = [col[0] for col in column_names]
my_cursor.execute("SELECT * FROM Cart WHERE Phone_Number=%s", (username,))
record_=my_cursor.fetchall()
l=[]
for i in record_:
my_cursor.execute(f"SELECT Name, Price FROM Product WHERE Product.ID={i[3]}")
data = my_cursor.fetchall()[0]
name = data[0]
price = data[1]
d={}
d["product_id"]=i[3]
d["Name"] = name
d['Quantity']=i[2]
d['Price'] = price
l.append(d)
conn.close()
return l
def add_to_cart(username: str, product_id: int,quantity:int) -> bool:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
print(username)
my_cursor.execute("SELECT * FROM Cart WHERE Phone_Number=%s",(username,))
records=my_cursor.fetchall()
for i in records:
if (i[3] == product_id):
my_cursor.execute("UPDATE Cart SET Quantity=Quantity+%s Where Product_ID=%s",(quantity,product_id,))
return True
query="INSERT INTO Cart(`Phone_Number`,`Quantity`,`Product_ID`) VALUES (%s,%s,%s)"
values=(username, quantity, product_id)
my_cursor.execute(query,values)
conn.commit()
conn.close()
return True
def call_for_trial(username: str) -> bool:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
my_cursor.execute("SELECT * FROM Cart WHERE Phone_Number=%s",(username,))
records=my_cursor.fetchall()
for i in records:
query="INSERT INTO trial_history(`PHONE_NUMBER`,`Product_Id`) VALUES (%s,%s)"
values=(username,i[3])
my_cursor.execute(query,values)
conn.commit()
return True
def get_trial_history(username: str) -> list[dict]:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
my_cursor.execute("SELECT * FROM trial_history WHERE Phone_Number=%s",(username,))
record=my_cursor.fetchall()
l=[]
for i in record:
l.append(i[2])
conn.close()
return l
def search_product(prompt: str) -> list[dict]:
return
def get_order_history(username: str) -> list[dict]:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
query = "SELECT * FROM order_history WHERE PHONE_NUMBER = %s"
my_cursor.execute(query, (username,))
order_history = my_cursor.fetchall()
resp=[]
for i in order_history:
my_cursor.execute("SELECT Name FROM Product WHERE ID=%s",(i[2],))
name=my_cursor.fetchall()[0][0]
d = {}
d['name'] = name
d['qty'] = i[3]
resp.append(d)
my_cursor.close()
conn.close()
return resp
def checkout(username: str) -> str or list[str]:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
my_cursor.execute("SELECT * FROM Cart WHERE Phone_Number=%s",(username,))
records = my_cursor.fetchall()
flag = True
items = []
for i in records:
my_cursor.execute("SELECT Quantity, Name FROM Product WHERE ID=%s", (i[3],))
data = my_cursor.fetchall()[0]
available = data[0]; name = data[1]
if(available < i[2]):
items.append([name, available])
flag = False
if(flag):
resp = ""
try:
for i in records:
query="INSERT INTO order_history(`Phone_Number`,`Product_ID`, `Quantity`) VALUES (%s,%s,%s)"
values=(i[1], i[3], i[2])
my_cursor.execute(query,values)
resp = "SUCCESSFUL!!!"
my_cursor.execute("DELETE FROM Cart WHERE Phone_Number=%s", (username,))
print("Deleted from Cart for user:", username)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_SIGNAL_EXCEPTION:
resp = "Error: Insufficient Stocks for the requested product."
else:
resp = "Internal SQL Error"
finally:
conn.commit()
conn.close()
return resp
conn.commit()
conn.close()
return items
def validate_login(username: str, password: str) -> bool:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
my_cursor.execute("SELECT * FROM Customer")
record=my_cursor.fetchall()
for i in record:
if (i[0]==username):
if (i[1]==password):
return True
return False
def register_user(data: dict) -> bool:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
my_cursor.execute("SELECT * FROM customer")
record=my_cursor.fetchall()
for i in record:
if (i[0]==data['Phone Number']):
return False
age=calculate_age(data["DOB"])
query="INSERT INTO `mydb`.`Customer` (`Phone Number`,`User Password`,`Email`, `Sex`, `DOB`, `Name`) VALUES (%s,%s,%s,%s,%s,%s)"
values=(data['phone_number'],data['password'],data['email'],data['sex'],data['DOB'],data['Name'])
my_cursor.execute(query,values)
conn.commit()
conn.close()
return True
def delete_item_from_cart(username, product_id):
# Establish a database connection
conn = mysql.connector.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DB)
cursor = conn.cursor()
# try:
# SQL statement to delete the specific item from the cart
query = "DELETE FROM Cart WHERE Phone_Number = %s AND Product_ID = %s"
values = (username, product_id)
# Execute the query
cursor.execute(query, values)
# Commit the changes to the database
conn.commit()
cursor.execute("SELECT * FROM Cart Where Phone_Number = %s", (username, ))
print(cursor.fetchall())
return True
def buy_now(username:str, product:int, qty: int) -> bool:
conn=mysql.connector.connect(host=MYSQL_HOST,username=MYSQL_USER,password=MYSQL_PASSWORD,database=MYSQL_DB)
my_cursor=conn.cursor()
query="INSERT INTO order_history(`Phone_Number`,`Product_ID`, `Quantity`) VALUES (%s,%s,%s)"
values=(username, product, qty)
result = ""
try:
my_cursor.execute(query,values)
result = "SUCCESSFUL!!!"
except mysql.connector.Error as err:
if err.errno == errorcode.ER_SIGNAL_EXCEPTION:
result = "Error: Insufficient Stocks for the requested product."
else:
result = "Internal SQL Error"
finally:
conn.commit()
conn.close()
return result