2024-01-28 11:59:04 +00:00
|
|
|
from .database import DatabaseController
|
|
|
|
from models.products.product import Product
|
|
|
|
|
|
|
|
|
|
|
|
class ProductController(DatabaseController):
|
|
|
|
FIELDS = ['id', 'name', 'image', 'description', 'cost',
|
|
|
|
'sellerID', 'category', 'postedDate', 'quantityAvailable']
|
2024-02-04 23:47:00 +00:00
|
|
|
TYPE = Product
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
2024-01-28 11:59:04 +00:00
|
|
|
INSERT INTO Products
|
|
|
|
(name, image, description, cost, categoryID,
|
|
|
|
sellerID, postedDate, quantityAvailable)
|
2024-02-04 23:47:00 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.do(query, params)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def read(self, name: str = "") -> list[Product] | None:
|
|
|
|
params = [
|
|
|
|
"%" + name + "%"
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = "SELECT * FROM Products WHERE name like ?"
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
return self.get_many(query, params)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def read_id(self, id: int) -> Product | None:
|
|
|
|
params = [
|
|
|
|
id
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = "SELECT * FROM Products WHERE id == ?"
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
return self.get_one(query, params)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def read_all(self, category: str = "",
|
|
|
|
search_term: str = "") -> list[Product] | None:
|
|
|
|
params = [
|
|
|
|
"%" + category + "%",
|
|
|
|
"%" + search_term + "%"
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
|
|
|
SELECT * FROM Products
|
2024-01-28 11:59:04 +00:00
|
|
|
INNER JOIN Categories ON Products.categoryID = Categories.id
|
|
|
|
WHERE Categories.name LIKE ?
|
|
|
|
AND Products.name LIKE ?
|
2024-02-04 23:47:00 +00:00
|
|
|
"""
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
return self.get_many(query, params)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def read_user(self, user_id: int) -> list[Product] | None:
|
|
|
|
params = [
|
|
|
|
user_id
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
|
|
|
SELECT * FROM Products
|
2024-01-28 11:59:04 +00:00
|
|
|
WHERE sellerID = ?
|
2024-02-04 23:47:00 +00:00
|
|
|
"""
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
return self.get_many(query, params)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def update(self, product: Product):
|
|
|
|
params = [
|
|
|
|
product.name,
|
|
|
|
product.description,
|
|
|
|
product.image,
|
|
|
|
product.cost,
|
|
|
|
product.quantityAvailable,
|
|
|
|
product.category,
|
|
|
|
product.id
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
|
|
|
UPDATE Products
|
2024-01-28 11:59:04 +00:00
|
|
|
SET name = ?,
|
|
|
|
description = ?,
|
|
|
|
image = ?,
|
|
|
|
cost = ?,
|
|
|
|
quantityAvailable = ?,
|
|
|
|
categoryID = ?
|
|
|
|
WHERE id = ?
|
2024-02-04 23:47:00 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
self.do(query, params)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-08 19:58:49 +00:00
|
|
|
def delete(self, id: int):
|
|
|
|
params = [
|
|
|
|
id
|
|
|
|
]
|
|
|
|
query = """
|
|
|
|
DELETE FROM Products
|
|
|
|
WHERE id = ?
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.do(query, params)
|