Products can now be searched using search term
This commit is contained in:
@ -51,15 +51,18 @@ class ProductController(DatabaseController):
|
||||
return products
|
||||
|
||||
|
||||
def read_all(self, category: str = "") -> list[Product] | None:
|
||||
def read_all(self, category: str = "", search_term: str = "") -> list[Product] | None:
|
||||
params = [
|
||||
"%" + category + "%"
|
||||
"%" + category + "%",
|
||||
"%" + search_term + "%"
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"""SELECT * FROM Products
|
||||
INNER JOIN Categories ON Products.categoryID = Categories.id
|
||||
WHERE Categories.name LIKE ? """,
|
||||
WHERE Categories.name LIKE ?
|
||||
AND Products.name LIKE ?
|
||||
""",
|
||||
params
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
|
@ -1,6 +1,6 @@
|
||||
from flask import Blueprint
|
||||
|
||||
from flask import render_template, session, flash
|
||||
from flask import render_template, session, flash, request
|
||||
from controllers.database.product import ProductController
|
||||
from controllers.database.category import CategoryController
|
||||
|
||||
@ -29,9 +29,15 @@ def index():
|
||||
# Loads a given product category page
|
||||
@blueprint.route('/<string:category>')
|
||||
def category(category: str):
|
||||
print(category)
|
||||
database = ProductController()
|
||||
products = database.read_all(category)
|
||||
|
||||
# Check to see if there is a custome search term
|
||||
search_term = request.args.get("search", type=str)
|
||||
if search_term != None:
|
||||
print(f"Search Term {search_term}")
|
||||
products = database.read_all(category, search_term)
|
||||
else:
|
||||
products = database.read_all(category)
|
||||
|
||||
# No Products visible
|
||||
if products == None:
|
||||
|
Reference in New Issue
Block a user