#6 Added Database functionality for user to click and load a single item
This commit is contained in:
@ -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 = [
|
||||
|
@ -64,6 +64,16 @@ def category(category: str):
|
||||
@blueprint.route('/<int:id>')
|
||||
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)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user