-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
264 lines (235 loc) · 8.41 KB
/
database.py
File metadata and controls
264 lines (235 loc) · 8.41 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
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
from ast import Raise
import uuid
from item import ProductInventory, OrderItem
import sqlite3
# connect to database
conn = sqlite3.connect("inv.db")
# create a cursor
c = conn.cursor()
class ProductDB:
"""Allows changes to the product database."""
def add_record(self, item_object: ProductInventory) -> None:
"""Adds a record to the products table."""
new_version = uuid.uuid4().hex
new_item = (
item_object.prod_id,
new_version,
item_object.name,
item_object.discript,
item_object.order_amount,
item_object.delivery
)
c.execute(
f"INSERT OR IGNORE INTO products Values(?,?,?,?,?,?)", new_item
)
print(f"{item_object.name} has been added to products db")
conn.commit()
def remove_record(self, prod_id: str) -> None:
"""Deletes a record using the product ID"""
primary_id = (prod_id,)
c.execute(f"DELETE FROM products WHERE Product_ID=?", primary_id)
conn.commit()
def update_record(self, item_object: ProductInventory) -> str:
"""
Checks the version_id has not changed and updates the
full record located from the primary key with a new_version_id. """
#create a new version_id
new_version_id = uuid.uuid4().hex
new_item = (
item_object.name,
item_object.discript,
item_object.order_amount,
item_object.delivery,
new_version_id,
item_object.prod_id,
item_object.prod_version_id,
)
print(item_object.prod_version_id)
print(f'{new_version_id}, new id')
# Update record if and only if product_id and version_id are true
c.execute("""
UPDATE products SET
Product_Name = ?,
Product_Discription = ?,
Product_Order_amount = ?,
Delivery = ?,
prod_version_ID = ?
WHERE
Product_ID = ? AND prod_version_ID = ?""",
new_item,
)
if c.rowcount == 0:
#raise RuntimeError("I'm sad :(")
return "Nothing to this record was changed, please try again"
# release the write access back to the world
conn.commit()
return "Your record has been updated!!"
def get_all_records(self) -> list[tuple[str]]:
"""@returns all the products held in the product db.
As a list of tuples."""
c.execute(
"""
SELECT
Product_ID,
Product_Name,
Product_Discription,
Product_Order_amount,
Delivery
FROM
products
"""
)
return c.fetchall()
def get_selection_of_records(self,prod_id_list:list[str]) -> list[tuple[str]]:
"""@returns a list of tuples containing all the product fields for the
record collection in the @arg"""
ids = tuple(prod_id_list)
expression = ','.join(['?']*len(ids))
sql = f"SELECT * FROM products WHERE Product_ID IN ({expression})"
c.execute(sql,ids)
return c.fetchall()
def get_header_names(self) -> list[str]:
"""@returns all the column names from the db products table."""
cursor = conn.execute(f"SELECT * FROM products")
names = [description[0] for description in cursor.description]
names.remove("prod_version_id")
return names
class InventoryDB:
"""Allows changes to the inventory database."""
def add_record(self, item_object: ProductInventory) -> None:
"""Adds a record to the inventory table."""
new_version = uuid.uuid4().hex
new_item = (
item_object.prod_id,
new_version,
item_object.qty,
item_object.limited_amount
)
c.execute(
f"INSERT OR IGNORE INTO inventory Values(?,?,?,?)", new_item
)
print(f"{item_object.name} has been added to inventory db")
conn.commit()
def remove_record(self, prod_id: str) -> None:
"""Deletes a record using the item product id"""
primary_id = (prod_id,)
c.execute(f"DELETE FROM inventory WHERE Product_ID=?", primary_id)
conn.commit()
def update_record(self, item_object: ProductInventory) -> str:
"""Updates the full record located form the primary key."""
#add version control here
new_version_id = uuid.uuid4().hex
item = (
new_version_id,
item_object.qty,
item_object.limited_amount,
item_object.prod_id,
item_object.inv_version_id
)
# Update record if and only if product_id and version_id are true
c.execute(
f"""UPDATE inventory SET
inv_version_id = ?,
Qty = ?,
Limited_amount = ?
WHERE
Product_ID = ? and inv_version_id = ?""",
item,
)
if c.rowcount == 0:
#raise RuntimeError("I'm sad :(")
return "Nothing to this record was changed, please try again"
# release the write access back to the world
conn.commit()
return "Your record has been updated!!!"
StockRecord = tuple[str, str, str, str, str, int, str, int, int]
class InStock:
"""Instock returns information from the inventory stock view from the database."""
def get_header_names(self) -> list[str]:
"""@returns all the columns from the stock inventory view from the db."""
cursor = conn.execute(f"SELECT * FROM stock_inventory")
names = [description[0] for description in cursor.description]
names.remove("prod_version_id")
names.remove("inv_version_id")
return names
def get_record(self, primary_key: str) -> list[StockRecord]:
"""@returns a product record as a tuple using the primary key."""
info = [(primary_key)]
c.execute(
f"""
SELECT
Product_ID,
prod_version_id,
inv_version_id,
Product_Name,
Product_Discription,
Product_Order_Amount,
Delivery,
Qty,
Limited_Amount
FROM
stock_inventory
WHERE
Product_ID = ?
""",
info,
)
return c.fetchall()
def need_ordering(self) -> list[tuple[str]]:
"""Checks each record in the table @returns all records where the qty
is less the allowed minimum."""
c.execute(
"""SELECT * FROM stock_inventory WHERE Qty < Limited_amount"""
)
return c.fetchall()
def get_all_records(self) -> list[StockRecord]:
"""@returns the list of all records held in the stock_inventory view
from the database."""
c.execute(
"""
SELECT
Product_ID,
prod_version_id,
inv_version_id,
Product_Name,
Product_Discription,
Product_Order_Amount,
Delivery,
Qty,
Limited_Amount
FROM
stock_inventory
"""
)
return c.fetchall()
class StaffDB:
"""Staff returns information from the staff database."""
def get_all_records(self) -> list[tuple[str]]:
"""@returns all the staff member details in a list of tuples"""
c.execute(
"""
SELECT
Staff_ID,
staff_name,
Staff_email,
unit_ID
FROM
staff
"""
)
return c.fetchall()
class CompletedOrders:
"""Carries out CRUD commands on the order table in the database."""
def add_record(self, item_object: OrderItem) -> None:
"""Adds a record to the orders table."""
new_item = (
item_object.order_id,
item_object.staff_id,
item_object.date_stamp,
item_object.file_path,
)
c.execute(
f"INSERT OR IGNORE INTO order Values(?,?,?,?)", new_item
)
print("Order added to order database")
conn.commit()