-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.py
69 lines (43 loc) · 1.89 KB
/
product.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
from mysqlConn import mysqlConnection
## Function to show products
def productList():
"""This function returns a list of products dictionary.
ex - [{'prod_id': 1, 'prod_name': 'Biscuit', 'prod_uom': 'each', 'prod_price': 10.0}]"""
cnn = mysqlConnection()
mycursor = cnn.cursor()
query = "SELECT * FROM proj_grocery_store.product"
mycursor.execute(query)
productsList = []
for (prod_id , prod_name , prod_uom , prod_price) in mycursor.fetchall():
productsList.append(
{
"prod_id" : prod_id,
"prod_name" : prod_name,
"prod_uom" : prod_uom,
"prod_price" : prod_price
}
)
cnn.close()
return productsList
## this is pending task.
## fucntion that add product into product table, only can be done by website admin.
def addProduct(data):
""" This function takes product's data as a dictionary and add the product into products table and returns none.
ex- data = {"prod_name" : "Tomato" , "prod_uom" : "kg" , "prod_price" : 30} """
cnn = mysqlConnection()
mycursor = cnn.cursor()
query = ("INSERT INTO proj_grocery_store.products ( prod_name , prod_uom , prod_price) "
" VALUES ( %s , %s , %s) ") # (8 , 'Tomato' , ''kg'' , 30) , prod_id is auto_increment so don't need to pass
data = (data["prod_name"] , data["prod_uom"] , data["prod_price"])
mycursor.execute(query , data)
cnn.commit()
cnn.close()
## function that remove product from the cart table
def removeProduct(prod_id):
"""This function takes the product_id and remove it from the product table."""
cnn = mysqlConnection()
mycursor = cnn.cursor()
query = (f"DELETE FROM grocery_store.products WHERE prod_id = {prod_id}")
mycursor.execute(query)
cnn.commit()
cnn.close()