Added page that shows a sellers own products

This commit is contained in:
Luke Else 2024-01-22 17:53:56 +00:00
parent dc6a3b3581
commit aaaf7e4829
2 changed files with 51 additions and 7 deletions

View File

@ -85,6 +85,32 @@ class ProductController(DatabaseController):
return products return products
def read_user(self, user_id: int) -> list[Product] | None:
params = [
user_id
]
cursor = self._conn.execute(
"""SELECT * FROM Products
WHERE sellerID = ?
""",
params
)
rows = cursor.fetchall()
if len(rows) == 0:
return None
products = list()
# Create an object for each row
for product in rows:
params = dict(zip(self.FIELDS, product))
obj = self.new_instance(Product, params)
products.append(obj)
return products
def update(self): def update(self):
print("Doing work") print("Doing work")

View File

@ -53,7 +53,8 @@ def category(category: str):
if products is None: if products is None:
flash(f"No Products available. Try expanding your search criteria.") flash(f"No Products available. Try expanding your search criteria.")
return render_template('index.html', return render_template(
'index.html',
content="content.html", content="content.html",
products=products, products=products,
category=category category=category
@ -108,7 +109,7 @@ def add_product():
request.form.get('description'), request.form.get('description'),
request.form.get('cost'), request.form.get('cost'),
request.form.get('category'), request.form.get('category'),
user.id, user_id,
datetime.now(), datetime.now(),
request.form.get('quantity') request.form.get('quantity')
) )
@ -120,4 +121,21 @@ def add_product():
@blueprint.route('/ownproducts') @blueprint.route('/ownproducts')
def display_own_products(): def display_own_products():
pass user_id = session.get('user_id')
# User must be logged in as seller to view page
if not is_role("Seller"):
flash("You must be logged in as a seller to view this page!")
return redirect("/", code=302)
db = ProductController()
products = db.read_user(user_id)
if products is None:
flash("You don't currently have any products for sale.")
return render_template(
'index.html',
content='content.html',
products=products
)