2024-01-05 18:20:56 +00:00
|
|
|
from .database import DatabaseController
|
|
|
|
from models.products.product import Product
|
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
class ProductController(DatabaseController):
|
2024-01-21 22:06:06 +00:00
|
|
|
FIELDS = ['id', 'name', 'image', 'description', 'cost',
|
|
|
|
'category', 'sellerID', 'postedDate', 'quantityAvailable']
|
2024-01-05 18:20:56 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def create(self, product: Product):
|
|
|
|
params = [
|
|
|
|
product.name,
|
|
|
|
product.image,
|
|
|
|
product.description,
|
|
|
|
product.cost,
|
|
|
|
product.category,
|
|
|
|
product.sellerID,
|
|
|
|
product.postedDate,
|
|
|
|
product.quantityAvailable
|
|
|
|
]
|
|
|
|
|
|
|
|
self._conn.execute(
|
2024-01-21 22:22:29 +00:00
|
|
|
"""
|
|
|
|
INSERT INTO Products
|
|
|
|
(name, image, description, cost, categoryID,
|
|
|
|
sellerID, postedDate, quantityAvailable)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
2024-01-05 18:20:56 +00:00
|
|
|
params
|
|
|
|
)
|
|
|
|
self._conn.commit()
|
|
|
|
|
|
|
|
def read(self, name: str = "") -> list[Product] | None:
|
|
|
|
params = [
|
|
|
|
"%" + name + "%"
|
|
|
|
]
|
|
|
|
|
|
|
|
cursor = self._conn.execute(
|
|
|
|
"SELECT * FROM Products WHERE name like ?",
|
|
|
|
params
|
|
|
|
)
|
|
|
|
rows = cursor.fetchmany()
|
|
|
|
|
2024-01-21 22:22:29 +00:00
|
|
|
if rows is None:
|
2024-01-05 18:20:56 +00:00
|
|
|
return None
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
products = list()
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
# Create an object for each row
|
|
|
|
for product in rows:
|
|
|
|
params = dict(zip(self.FIELDS, product))
|
|
|
|
obj = self.new_instance(Product, params)
|
2024-01-10 23:44:59 +00:00
|
|
|
products.append(obj)
|
2024-01-05 18:20:56 +00:00
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
return products
|
2024-01-05 18:20:56 +00:00
|
|
|
|
2024-01-21 22:22:29 +00:00
|
|
|
def read_all(self, category: str = "",
|
|
|
|
search_term: str = "") -> list[Product] | None:
|
2024-01-06 01:14:20 +00:00
|
|
|
params = [
|
2024-01-15 21:47:18 +00:00
|
|
|
"%" + category + "%",
|
|
|
|
"%" + search_term + "%"
|
2024-01-06 01:14:20 +00:00
|
|
|
]
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
cursor = self._conn.execute(
|
2024-01-21 22:22:29 +00:00
|
|
|
"""SELECT * FROM Products
|
|
|
|
INNER JOIN Categories ON Products.categoryID = Categories.id
|
|
|
|
WHERE Categories.name LIKE ?
|
2024-01-15 21:47:18 +00:00
|
|
|
AND Products.name LIKE ?
|
|
|
|
""",
|
2024-01-06 01:14:20 +00:00
|
|
|
params
|
2024-01-05 18:20:56 +00:00
|
|
|
)
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
if len(rows) == 0:
|
|
|
|
return None
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
products = list()
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
# Create an object for each row
|
|
|
|
for product in rows:
|
|
|
|
params = dict(zip(self.FIELDS, product))
|
|
|
|
obj = self.new_instance(Product, params)
|
|
|
|
products.append(obj)
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
return products
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
print("Doing work")
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
def delete(self):
|
2024-01-21 22:06:06 +00:00
|
|
|
print("Doing work")
|