Products can now be searched using search term

This commit is contained in:
Luke Else 2024-01-15 21:47:18 +00:00
parent 40d7469321
commit 36a04d58e1
4 changed files with 36 additions and 7 deletions

View File

@ -51,15 +51,18 @@ class ProductController(DatabaseController):
return products return products
def read_all(self, category: str = "") -> list[Product] | None: def read_all(self, category: str = "", search_term: str = "") -> list[Product] | None:
params = [ params = [
"%" + category + "%" "%" + category + "%",
"%" + search_term + "%"
] ]
cursor = self._conn.execute( cursor = self._conn.execute(
"""SELECT * FROM Products """SELECT * FROM Products
INNER JOIN Categories ON Products.categoryID = Categories.id INNER JOIN Categories ON Products.categoryID = Categories.id
WHERE Categories.name LIKE ? """, WHERE Categories.name LIKE ?
AND Products.name LIKE ?
""",
params params
) )
rows = cursor.fetchall() rows = cursor.fetchall()

View File

@ -1,6 +1,6 @@
from flask import Blueprint 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.product import ProductController
from controllers.database.category import CategoryController from controllers.database.category import CategoryController
@ -29,8 +29,14 @@ def index():
# Loads a given product category page # Loads a given product category page
@blueprint.route('/<string:category>') @blueprint.route('/<string:category>')
def category(category: str): def category(category: str):
print(category)
database = ProductController() database = ProductController()
# 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) products = database.read_all(category)
# No Products visible # No Products visible

View File

@ -1,7 +1,7 @@
<div class="navbar"> <div class="navbar">
<nav> <nav>
<a href="/"><img src="{{url_for('static', filename='assets/img/wmgzon.png')}}" id="logo" class="not-required" alt="WMGZON Logo"></a> <a href="/"><img src="{{url_for('static', filename='assets/img/wmgzon.png')}}" id="logo" class="not-required" alt="WMGZON Logo"></a>
<form action="test.html" method="get"> <form action="" method="get">
<input type="text" name="search" placeholder="Find your favourite products" class="search-bar"> <input type="text" name="search" placeholder="Find your favourite products" class="search-bar">
<input type="submit" class="search-button"> <input type="submit" class="search-button">
</form> </form>

View File

@ -27,6 +27,26 @@ def test_duplicate_product():
with pytest.raises(sqlite3.IntegrityError): with pytest.raises(sqlite3.IntegrityError):
db.create(product) db.create(product)
# Tests that products can be refined by category
def test_search_category():
db = ProductController()
# Check each category for correct amount of test products
assert len(db.read_all("Car Parts")) == 9 + 1 # Added in previous test
assert len(db.read_all("Books")) == 9
assert db.read_all("Phones") == None
# Tests that products can be refined by search term
def test_search_term():
db = ProductController()
# Check each search term for correct amount of test products
assert len(db.read_all(search_term="test")) == 33
assert len(db.read_all("Car Parts", "test")) == 9
assert len(db.read_all(search_term="product")) == 1
assert db.read_all(search_term="not_test") == None
# Test we the same product details get returned from the database # Test we the same product details get returned from the database
def test_read_product(): def test_read_product():
db = ProductController() db = ProductController()