from .database import DatabaseController from models.products.product import Product class ProductController(DatabaseController): FIELDS = ['id', 'name', 'image', 'description', 'cost', 'sellerID', 'category', 'postedDate', 'quantityAvailable'] TYPE = Product def __init__(self): super().__init__() def create(self, product: Product): params = [ product.name, product.image, product.description, product.cost, product.category, product.sellerID, product.postedDate, product.quantityAvailable ] query = """ INSERT INTO Products (name, image, description, cost, categoryID, sellerID, postedDate, quantityAvailable) VALUES (?, ?, ?, ?, ?, ?, ?, ?) """ self.do(query, params) def read(self, name: str = "", filter: str = "") -> list[Product] | None: params = [ "%" + name + "%" ] query = "SELECT * FROM Products WHERE name like ?" query += filter return self.get_many(query, params) def read_id(self, id: int) -> Product | None: params = [ id ] query = "SELECT * FROM Products WHERE id == ?" return self.get_one(query, params) def read_all(self, category: str = "", search_term: str = "", filter: str = "") -> list[Product] | None: params = [ "%" + category + "%", "%" + search_term + "%" ] query = """ SELECT * FROM Products INNER JOIN Categories ON Products.categoryID = Categories.id WHERE Categories.name LIKE ? AND Products.name LIKE ? """ query += filter return self.get_many(query, params) def read_user(self, user_id: int) -> list[Product] | None: params = [ user_id ] query = """ SELECT * FROM Products WHERE sellerID = ? """ return self.get_many(query, params) def update(self, product: Product): params = [ product.name, product.description, product.image, product.cost, product.quantityAvailable, product.category, product.id ] query = """ UPDATE Products SET name = ?, description = ?, image = ?, cost = ?, quantityAvailable = ?, categoryID = ? WHERE id = ? """ self.do(query, params) def delete(self, id: int): params = [ id ] query = """ DELETE FROM Products WHERE id = ? """ self.do(query, params)