REFACTOR: Moved more functionality into database base type
This commit is contained in:
@@ -5,6 +5,7 @@ from models.products.product import Product
|
||||
class ProductController(DatabaseController):
|
||||
FIELDS = ['id', 'name', 'image', 'description', 'cost',
|
||||
'sellerID', 'category', 'postedDate', 'quantityAvailable']
|
||||
TYPE = Product
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@@ -20,60 +21,30 @@ class ProductController(DatabaseController):
|
||||
product.postedDate,
|
||||
product.quantityAvailable
|
||||
]
|
||||
|
||||
self._conn.execute(
|
||||
"""
|
||||
query = """
|
||||
INSERT INTO Products
|
||||
(name, image, description, cost, categoryID,
|
||||
sellerID, postedDate, quantityAvailable)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
params
|
||||
)
|
||||
self._conn.commit()
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
|
||||
self.do(query, params)
|
||||
|
||||
def read(self, name: str = "") -> list[Product] | None:
|
||||
params = [
|
||||
"%" + name + "%"
|
||||
]
|
||||
query = "SELECT * FROM Products WHERE name like ?"
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM Products WHERE name like ?",
|
||||
params
|
||||
)
|
||||
rows = cursor.fetchmany()
|
||||
|
||||
if rows is 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.append(obj)
|
||||
|
||||
return products
|
||||
return self.get_many(query, params)
|
||||
|
||||
def read_id(self, id: int) -> Product | None:
|
||||
params = [
|
||||
id
|
||||
]
|
||||
query = "SELECT * FROM Products WHERE id == ?"
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM Products WHERE id == ?",
|
||||
params
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
|
||||
if row is None:
|
||||
return None
|
||||
|
||||
# Create an object with the row
|
||||
params = dict(zip(self.FIELDS, row))
|
||||
obj = self.new_instance(Product, params)
|
||||
|
||||
return obj
|
||||
return self.get_one(query, params)
|
||||
|
||||
def read_all(self, category: str = "",
|
||||
search_term: str = "") -> list[Product] | None:
|
||||
@@ -81,55 +52,25 @@ class ProductController(DatabaseController):
|
||||
"%" + category + "%",
|
||||
"%" + search_term + "%"
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"""SELECT * FROM Products
|
||||
query = """
|
||||
SELECT * FROM Products
|
||||
INNER JOIN Categories ON Products.categoryID = Categories.id
|
||||
WHERE Categories.name LIKE ?
|
||||
AND Products.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
|
||||
return self.get_many(query, params)
|
||||
|
||||
def read_user(self, user_id: int) -> list[Product] | None:
|
||||
params = [
|
||||
user_id
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"""SELECT * FROM Products
|
||||
query = """
|
||||
SELECT * FROM Products
|
||||
WHERE sellerID = ?
|
||||
""",
|
||||
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
|
||||
return self.get_many(query, params)
|
||||
|
||||
def update(self, product: Product):
|
||||
params = [
|
||||
@@ -141,9 +82,8 @@ class ProductController(DatabaseController):
|
||||
product.category,
|
||||
product.id
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"""UPDATE Products
|
||||
query = """
|
||||
UPDATE Products
|
||||
SET name = ?,
|
||||
description = ?,
|
||||
image = ?,
|
||||
@@ -151,10 +91,9 @@ class ProductController(DatabaseController):
|
||||
quantityAvailable = ?,
|
||||
categoryID = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
params
|
||||
)
|
||||
self._conn.commit()
|
||||
"""
|
||||
|
||||
self.do(query, params)
|
||||
|
||||
def delete(self):
|
||||
print("Doing work")
|
||||
|
||||
Reference in New Issue
Block a user