Added highlighting for selected category
This commit is contained in:
64
controllers/database/category.py
Normal file
64
controllers/database/category.py
Normal file
@ -0,0 +1,64 @@
|
||||
from .database import DatabaseController
|
||||
from models.category import Category
|
||||
|
||||
class CategoryController(DatabaseController):
|
||||
FIELDS = ['id', 'name']
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def create(self, category: Category):
|
||||
params = [
|
||||
category.name,
|
||||
]
|
||||
|
||||
self._conn.execute(
|
||||
"INSERT INTO Categories (name) VALUES (?)",
|
||||
params
|
||||
)
|
||||
self._conn.commit()
|
||||
|
||||
|
||||
def read(self, id: int = 0) -> Category | None:
|
||||
params = [
|
||||
id
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM Categories WHERE id = ?",
|
||||
params
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
|
||||
if row == None:
|
||||
return None
|
||||
|
||||
params = dict(zip(self.FIELDS, row))
|
||||
obj = self.new_instance(Category, params)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
def read_all(self) -> list[Category] | None:
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM Categories",
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
|
||||
if rows == None:
|
||||
return None
|
||||
|
||||
categories = list()
|
||||
|
||||
for category in rows:
|
||||
params = dict(zip(self.FIELDS, category))
|
||||
obj = self.new_instance(Category, params)
|
||||
categories.append(obj)
|
||||
|
||||
return categories
|
||||
|
||||
def update(self):
|
||||
print("Doing work")
|
||||
|
||||
def delete(self):
|
||||
print("Doing work")
|
@ -46,15 +46,21 @@ class ProductController(DatabaseController):
|
||||
for product in rows:
|
||||
params = dict(zip(self.FIELDS, product))
|
||||
obj = self.new_instance(Product, params)
|
||||
print(obj.__dict__)
|
||||
products.push(obj)
|
||||
|
||||
return products
|
||||
|
||||
|
||||
def read_all(self) -> list[Product] | None:
|
||||
def read_all(self, category: str = "") -> list[Product] | None:
|
||||
params = [
|
||||
"%" + category + "%"
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM Products",
|
||||
"""SELECT * FROM Products
|
||||
INNER JOIN Categories ON Products.categoryID = Categories.id
|
||||
WHERE Categories.name LIKE ? """,
|
||||
params
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
|
||||
@ -67,7 +73,6 @@ class ProductController(DatabaseController):
|
||||
for product in rows:
|
||||
params = dict(zip(self.FIELDS, product))
|
||||
obj = self.new_instance(Product, params)
|
||||
print(obj.__dict__)
|
||||
products.append(obj)
|
||||
|
||||
return products
|
||||
|
@ -39,7 +39,6 @@ class UserController(DatabaseController):
|
||||
if row != None:
|
||||
params = dict(zip(self.FIELDS, row))
|
||||
obj = self.new_instance(Customer, params)
|
||||
print(obj.__dict__)
|
||||
return obj
|
||||
|
||||
return None
|
||||
|
@ -2,9 +2,16 @@ from flask import Blueprint
|
||||
|
||||
from flask import render_template, session, flash
|
||||
from controllers.database.product import ProductController
|
||||
from controllers.database.category import CategoryController
|
||||
|
||||
blueprint = Blueprint("products", __name__, url_prefix="/products")
|
||||
|
||||
@blueprint.context_processor
|
||||
def category_list():
|
||||
database = CategoryController()
|
||||
categories = database.read_all()
|
||||
return dict(categories=categories)
|
||||
|
||||
# Loads the front product page
|
||||
@blueprint.route('/')
|
||||
def index():
|
||||
@ -20,7 +27,15 @@ def index():
|
||||
# Loads a given product category page
|
||||
@blueprint.route('/<string:category>')
|
||||
def category(category: str):
|
||||
return "Category: " + category
|
||||
print(category)
|
||||
database = ProductController()
|
||||
products = database.read_all(category)
|
||||
|
||||
# No Products visible
|
||||
if products == None:
|
||||
flash("No Products available in " + category)
|
||||
|
||||
return render_template('index.html', content="content.html", user = session.get('user'), products = products, category = category)
|
||||
|
||||
# Loads a given product based on ID
|
||||
@blueprint.route('/<int:id>')
|
||||
|
Reference in New Issue
Block a user