From 36a04d58e12c71608fd4aa634abb0c51290e6b3f Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 15 Jan 2024 21:47:18 +0000 Subject: [PATCH] Products can now be searched using search term --- controllers/database/product.py | 9 ++++++--- controllers/web/product.py | 12 +++++++++--- templates/header.html | 2 +- tests/database/test_products.py | 20 ++++++++++++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/controllers/database/product.py b/controllers/database/product.py index 8a8c97e..08943d0 100644 --- a/controllers/database/product.py +++ b/controllers/database/product.py @@ -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() diff --git a/controllers/web/product.py b/controllers/web/product.py index 3d94aa9..9fcfdcc 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -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('/') 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: diff --git a/templates/header.html b/templates/header.html index 57eff9f..18efd97 100644 --- a/templates/header.html +++ b/templates/header.html @@ -1,7 +1,7 @@