From 95768f8a8cd04d6db6a0d116edad816bafea4212 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 23 Jan 2024 17:53:00 +0000 Subject: [PATCH] #6 Added Database functionality for user to click and load a single item --- controllers/database/product.py | 20 ++++++++++++++++++++ controllers/web/product.py | 10 ++++++++++ static/css/style.css | 2 +- templates/index.html | 4 ++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/controllers/database/product.py b/controllers/database/product.py index 3b0c5c9..c232aa9 100644 --- a/controllers/database/product.py +++ b/controllers/database/product.py @@ -55,6 +55,26 @@ class ProductController(DatabaseController): return products + def read_id(self, id: int) -> Product | None: + params = [ + 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 + def read_all(self, category: str = "", search_term: str = "") -> list[Product] | None: params = [ diff --git a/controllers/web/product.py b/controllers/web/product.py index 32d3482..7212088 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -64,6 +64,16 @@ def category(category: str): @blueprint.route('/') def id(id: int): """ Loads a given product based on ID """ + db = ProductController() + product = db.read_id(id) + + # Check that a valid product was returned + if product is None: + flash("No Product available here") + return redirect("/") + + print(product.name) + return render_template('index.html', content='content.html', products = [product]) return "ID: " + str(id) diff --git a/static/css/style.css b/static/css/style.css index 09a3b9e..dc69804 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -20,7 +20,7 @@ html { background-color: var(--bg); color: var(--fg); font-size: 160%; - font-family: 'Courier New', Courier, monospace; + font-family: 'Courier New', Courier, 'Inter'; height: 100%; } diff --git a/templates/index.html b/templates/index.html index e68507c..94b51cf 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,6 +7,10 @@ + + + + WMGZON