84 lines
2.2 KiB
Python
84 lines
2.2 KiB
Python
from .database import DatabaseController
|
|
from models.products.product import Product
|
|
|
|
class ProductController(DatabaseController):
|
|
FIELDS = ['id', 'name', 'image', 'description', 'cost', 'category', 'sellerID', 'postedDate', 'quantity']
|
|
|
|
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(
|
|
"INSERT INTO Products (name, cost, image, description, category, sellerID, postedDate, quantityAvailable) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
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()
|
|
|
|
if rows == None:
|
|
return None
|
|
|
|
products = list()
|
|
|
|
# Create an object for each row
|
|
for product in rows:
|
|
params = dict(zip(self.FIELDS, product))
|
|
obj = self.new_instance(Product, params)
|
|
products.push(obj)
|
|
|
|
return products
|
|
|
|
|
|
def read_all(self, category: str = "") -> list[Product] | None:
|
|
params = [
|
|
"%" + category + "%"
|
|
]
|
|
|
|
cursor = self._conn.execute(
|
|
"""SELECT * FROM Products
|
|
INNER JOIN Categories ON Products.categoryID = Categories.id
|
|
WHERE Categories.name LIKE ? """,
|
|
params
|
|
)
|
|
rows = cursor.fetchall()
|
|
|
|
if len(rows) == 0:
|
|
return None
|
|
|
|
products = list()
|
|
|
|
# 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)
|
|
|
|
return products
|
|
|
|
def update(self):
|
|
print("Doing work")
|
|
|
|
def delete(self):
|
|
print("Doing work") |