#7 Added basic set of product orderings.

This commit is contained in:
Luke Else 2024-02-12 18:27:04 +00:00
parent cafaf94a00
commit 7faca50b73
3 changed files with 43 additions and 12 deletions

View File

@ -30,11 +30,13 @@ class ProductController(DatabaseController):
self.do(query, params) self.do(query, params)
def read(self, name: str = "") -> list[Product] | None: def read(self, name: str = "", filter: str = "") -> list[Product] | None:
params = [ params = [
"%" + name + "%" "%" + name + "%"
] ]
query = "SELECT * FROM Products WHERE name like ?" query = "SELECT * FROM Products WHERE name like ?"
query += filter
return self.get_many(query, params) return self.get_many(query, params)
@ -46,8 +48,8 @@ class ProductController(DatabaseController):
return self.get_one(query, params) return self.get_one(query, params)
def read_all(self, category: str = "", def read_all(self, category: str = "", search_term: str = "",
search_term: str = "") -> list[Product] | None: filter: str = "") -> list[Product] | None:
params = [ params = [
"%" + category + "%", "%" + category + "%",
"%" + search_term + "%" "%" + search_term + "%"
@ -59,6 +61,8 @@ class ProductController(DatabaseController):
AND Products.name LIKE ? AND Products.name LIKE ?
""" """
query += filter
return self.get_many(query, params) return self.get_many(query, params)
def read_user(self, user_id: int) -> list[Product] | None: def read_user(self, user_id: int) -> list[Product] | None:

View File

@ -19,6 +19,33 @@ import os
blueprint = Blueprint("products", __name__, url_prefix="/products") blueprint = Blueprint("products", __name__, url_prefix="/products")
# List of available filters for the user to select
FILTERS = {
# ANY INFOMRATION PUT INTO THE VALUES HERE WILL BE INTERPRETED AS SQL!!!
'Relevance': 'ORDER BY quantityAvailable DESC',
'Price: High -> Low': 'ORDER BY cost DESC',
'Price: Low -> High': 'ORDER BY cost'
}
@blueprint.context_processor
def filter_list():
""" Places a list of all the available filters in the
products context
"""
return dict(filters=FILTERS)
def get_filter():
""" Return any filters that are currently active on the page """
filter = request.args.get('filter')
if filter is None:
filter = 'Relevance'
if filter in FILTERS:
return FILTERS[filter]
return FILTERS['Relevance']
@blueprint.context_processor @blueprint.context_processor
def category_list(): def category_list():
@ -44,9 +71,9 @@ def category(category: str):
# Check to see if there is a custome search term # Check to see if there is a custome search term
search_term = request.args.get("search", type=str) search_term = request.args.get("search", type=str)
if search_term is not None: if search_term is not None:
products = database.read_all(category, search_term) products = database.read_all(category, search_term, get_filter())
else: else:
products = database.read_all(category) products = database.read_all(category, "", get_filter())
# No Products visible # No Products visible
if products is None: if products is None:

View File

@ -10,13 +10,13 @@
<input type="text" class="vrn-text" placeholder="YOUR REG" name="vrn"> <input type="text" class="vrn-text" placeholder="YOUR REG" name="vrn">
</span> </span>
</div> </div>
{% if filters != None %}
<select class="product-filter not-required" name="filter"> <select class="product-filter not-required" name="filter">
<option value="relevance">Most Relevant</option> {% for filter in filters.keys() %}
<option value="price-lh">Price: Low -> High</option> <option value="{{filter}}">{{filter}}</option>
<option value="price-hl">Price: High -> Low</option> {% endfor %}
</select> </select>
{% endif %}
<input type="submit" class="search-button" value="Filter"> <input type="submit" class="search-button" value="Filter">
</form> </form>
</div> </div>